From b292e6c197afd873ff0a0b3d7ef97425537dc5ed Mon Sep 17 00:00:00 2001 From: Mark Jordan Date: Wed, 17 Jun 2020 11:23:27 -0700 Subject: [PATCH] Issue 1521: Provide a way for repo admins to see RDF mappings (#778) * Work on #1521. * Work on #1521; fixed function comment. * Work on #1521. * Work on #1521. * Added standard /taxonomy/term/x links to terms in report. --- islandora.links.menu.yml | 7 + islandora.routing.yml | 9 ++ .../RdfMappingsReportController.php | 139 ++++++++++++++++++ src/IslandoraUtils.php | 5 +- 4 files changed, 158 insertions(+), 2 deletions(-) create mode 100644 src/Controller/RdfMappingsReportController.php diff --git a/islandora.links.menu.yml b/islandora.links.menu.yml index 36024a19..7d69b3d6 100644 --- a/islandora.links.menu.yml +++ b/islandora.links.menu.yml @@ -11,3 +11,10 @@ system.islandora_settings: parent: system.admin_config_islandora route_name: system.islandora_settings description: 'Confgure core Islandora settings' + +# RDF property mappings +system.islandora_rdf_mappings: + title: 'Field and term RDF mappings' + parent: system.admin_reports + description: 'List of configured Drupal field to RDF property mappings and taxonomy term linked data URIs.' + route_name: system.islandora_rdf_mappings diff --git a/islandora.routing.yml b/islandora.routing.yml index 58dea1a2..310a702b 100644 --- a/islandora.routing.yml +++ b/islandora.routing.yml @@ -16,6 +16,15 @@ system.islandora_settings: requirements: _permission: 'administer site configuration' +# RDF properties report +system.islandora_rdf_mappings: + path: '/admin/reports/islandora/rdf_mappings' + defaults: + _controller: '\Drupal\islandora\Controller\RdfMappingsReportController::main' + _title: 'Field and term RDF mappings' + requirements: + _permission: 'administer site configuration' + islandora.add_member_to_node_page: path: '/node/{node}/members/add' defaults: diff --git a/src/Controller/RdfMappingsReportController.php b/src/Controller/RdfMappingsReportController.php new file mode 100644 index 00000000..280ccd0c --- /dev/null +++ b/src/Controller/RdfMappingsReportController.php @@ -0,0 +1,139 @@ + $namespace_uri) { + $namespaces_table_rows[] = [$alias, $namespace_uri]; + } + $namespaces_table_header = [t('Namespace alias'), t('Namespace URI')]; + $namespaces_table = [ + '#theme' => 'table', + '#header' => $namespaces_table_header, + '#rows' => $namespaces_table_rows, + ]; + $namespaces_table_markup = \Drupal::service('renderer')->render($namespaces_table); + + $markup .= '
' . t('RDF namespaces used in field mappings') . + '
' . $namespaces_table_markup . '
'; + + // Node and media field to RDF property mappings. + $entity_types = ['node', 'media']; + $markup .= '

' . t('Field mappings') . '

'; + foreach ($entity_types as $entity_type) { + $bundles = \Drupal::service('entity_type.bundle.info')->getBundleInfo($entity_type); + foreach ($bundles as $name => $attr) { + $markup .= '

' . $attr['label'] . ' (' . $entity_type . ')' . '

'; + $rdf_mappings = rdf_get_mapping($entity_type, $name); + $fields = \Drupal::entityManager()->getFieldDefinitions($entity_type, $name); + $mappings_table_rows = []; + foreach ($fields as $field_name => $field_object) { + $field_mappings = $rdf_mappings->getPreparedFieldMapping($field_name); + if (array_key_exists('properties', $field_mappings)) { + $mappings_table_rows[] = [$field_object->getLabel() . ' (' . $field_name . ')', $field_mappings['properties'][0]]; + } + } + + $mappings_header = [t('Drupal field'), t('RDF property')]; + + if (count($mappings_table_rows) == 0) { + $mappings_header = []; + $mappings_table_rows[] = [t('No RDF mappings configured for @bundle.', ['@bundle' => $attr['label']])]; + } + + $mappings_table = [ + '#theme' => 'table', + '#header' => $mappings_header, + '#rows' => $mappings_table_rows, + ]; + $mappings_table_markup = \Drupal::service('renderer')->render($mappings_table); + $markup .= $mappings_table_markup; + } + } + + // Taxonomy terms with external URIs or authority links. + $markup .= '

' . t('Taxonomy terms with external URIs or authority links') . '

'; + $utils = \Drupal::service('islandora.utils'); + $uri_fields = $utils->getUriFieldNamesForTerms(); + + $vocabs = Vocabulary::loadMultiple(); + foreach ($vocabs as $vid => $vocab) { + $vocab_table_header = []; + $vocab_table_rows = []; + $markup .= '

' . $vocab->label() . ' (' . $vid . ')

'; + $terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree($vid); + if (count($terms) == 0) { + $vocab_table_rows[] = [t('No terms in this vocabulary.')]; + $vocab_table = [ + '#theme' => 'table', + '#header' => $vocab_table_header, + '#rows' => $vocab_table_rows, + ]; + $vocab_table_markup = \Drupal::service('renderer')->render($vocab_table); + $markup .= $vocab_table_markup; + } + else { + $vocab_table_header = [ + t('Term'), + t('Term ID'), + t('External URI or Authority link'), + ]; + $vocab_table_rows = []; + foreach ($terms as $t) { + $ld_uri = NULL; + $term = Term::load($t->tid); + foreach ($uri_fields as $uri_field) { + if ($term->hasField($uri_field) && !$term->get($uri_field)->isEmpty()) { + $ld_uri = $term->get($uri_field)->first()->getValue(); + continue; + } + } + if (is_array($ld_uri) && array_key_exists('uri', $ld_uri)) { + $term_link = Link::fromTextAndUrl($term->getName(), Url::fromUri('internal:/taxonomy/term/' . $term->id())); + $vocab_table_rows[] = [ + $term_link, + $term->id(), + $ld_uri['uri'], + ]; + } + else { + $term_link = Link::fromTextAndUrl($term->getName(), Url::fromUri('internal:/taxonomy/term/' . $term->id())); + $vocab_table_rows[] = [$term_link, $term->id(), t('None')]; + } + } + $vocab_table = [ + '#theme' => 'table', + '#header' => $vocab_table_header, + '#rows' => $vocab_table_rows, + ]; + } + $vocab_table_markup = \Drupal::service('renderer')->render($vocab_table); + $markup .= $vocab_table_markup; + } + + return ['#markup' => $markup]; + } + +} diff --git a/src/IslandoraUtils.php b/src/IslandoraUtils.php index 1d337796..c4bfcb2f 100644 --- a/src/IslandoraUtils.php +++ b/src/IslandoraUtils.php @@ -265,7 +265,7 @@ class IslandoraUtils { } /** - * Gets the taxonomy term associated with an external uri. + * Gets the external uri associated with a taxonomy term. * * @param \Drupal\taxonomy\TermInterface $term * Taxonomy term. @@ -302,7 +302,8 @@ class IslandoraUtils { $field_map = $this->entityFieldManager->getFieldMap(); $fields = []; foreach ($field_map['taxonomy_term'] as $field_name => $field_data) { - if ($field_data['type'] == 'authority_link') { + $data_types = ['authority_link', 'field_external_authority_link']; + if (in_array($field_data['type'], $data_types)) { $fields[] = $field_name; } }