Skip to content

Commit

Permalink
GetDKAN#4115: Expose dataset properties as pseudo/extra fields
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-korn committed Feb 9, 2024
1 parent c7eb1ac commit 711d5a4
Showing 1 changed file with 60 additions and 0 deletions.
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()
];
}
}
}

}
}
}
}

0 comments on commit 711d5a4

Please sign in to comment.