Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#4115: Expose dataset properties as pseudo/extra fields #4116

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions modules/metastore/modules/metastore_search/metastore_search.module
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,63 @@ function metastore_search_entity_delete(EntityInterface $entity): void {
$index = $storage->load('dkan');
$index->trackItemsDeleted('dkan_dataset', [$entity->uuid()]);
}

/**
* Implements hook_entity_extra_field_info().
*
* Expose Dataset properties as pseudo fields
*/
function metastore_search_entity_extra_field_info()
{
$extra = [];

$dataset = new \Drupal\metastore_search\ComplexData\Dataset('');
foreach ($dataset->getProperties() as $property_key => $property) {
if ($property instanceof \Drupal\Core\TypedData\TypedData) {
$extra['node']['data']['display']['dataset_' . $property_key] = [
'label' => t('Dataset property: @property', ['@property' => $property->getName()]),
'weight' => 0,
'visible' => FALSE
];
}
}

return $extra;
}

/**
* Implements hook_ENTITY_TYPE_view().
*
* output exposed dataset properties
*/
function metastore_search_node_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode)
{
if ($entity instanceof \Drupal\node\Entity\Node && $entity->bundle() === 'data'
&& $entity->hasField('field_data_type')
&& !$entity->get('field_data_type')->isEmpty()
&& $entity->get('field_data_type')->value === 'dataset'
) {
$dataset = new \Drupal\metastore_search\ComplexData\Dataset($entity->get('field_json_metadata')->value);
foreach ($dataset->getProperties() as $property_key => $property) {
if ($property instanceof \Drupal\Core\TypedData\TypedData) {
if ($display->getComponent('dataset_' . $property_key)) {
if ($property instanceof \Drupal\Core\TypedData\Plugin\DataType\ItemList) {
foreach ($property->getValue() as $value) {
$build['dataset_' . $property_key][] = [
'#markup' => $value
];
}
}
else {
if (is_string($property->getValue())) {
$build['dataset_' . $property_key] = [
'#markup' => $property->getValue()
];
}
}
}

}
}
}
}