From 614797039b2bdd44a37344dce0d460627054f2b9 Mon Sep 17 00:00:00 2001 From: Rosie Le Faive Date: Wed, 19 Nov 2025 10:31:34 -0400 Subject: [PATCH] Edit field for base field override, starting refactor. --- metadata_profile.routing.yml | 8 + src/Controller/MetadataProfileController.php | 169 ++++++++++++++++--- 2 files changed, 156 insertions(+), 21 deletions(-) diff --git a/metadata_profile.routing.yml b/metadata_profile.routing.yml index a069f2f..768c615 100644 --- a/metadata_profile.routing.yml +++ b/metadata_profile.routing.yml @@ -10,4 +10,12 @@ metadata_profile.node_profile: parameters: content_type: type: string +metadata_profile.test: + path: '/test' + defaults: + _controller: '\Drupal\metadata_profile\Controller\MetadataProfileController::test' + _title: 'Metadata Profile Test' + requirements: + _access: 'TRUE' + diff --git a/src/Controller/MetadataProfileController.php b/src/Controller/MetadataProfileController.php index af88dab..81e6a16 100644 --- a/src/Controller/MetadataProfileController.php +++ b/src/Controller/MetadataProfileController.php @@ -36,11 +36,16 @@ class MetadataProfileController extends ControllerBase { $container->get('entity_field.manager'), $container->get('plugin.manager.field.field_type'), $container->get('config.factory') - ); } - public function profile( $content_type) { + /** + * Returns a render array displaying the metadata profile for $content_type. + * + * @param $content_type + * @return array + */ + public function profile($content_type) { // Get core content type information. $node_type = $this->config('node.type.' . $content_type); @@ -49,6 +54,9 @@ class MetadataProfileController extends ControllerBase { } $build = $this->format_bundle($node_type); + $metadata_profile = $this->getMetadataProfile($node_type); + + $build['children'] = [ '#type' => 'container', '#weight' => '9', @@ -89,6 +97,45 @@ class MetadataProfileController extends ControllerBase { return $build; } + public function getMetadataProfile($bundle) { + // $metadata_profile is an array of arrays. Its keys are the "long" machine names of the fields + // such as node.islandora_object.field_abstract. The values are arrays with the following keys: + // * 'label' - text. the bundle's human-readable label. + // * 'machine_name' - text. The short machine name such as 'field_abstract'. + // * 'id' - text. The long machine name such as 'node.islandora_object.field_abstract'. (same as this array's key) + // * 'edit_url' - Url object. The link to the edit page for that field (or to the base field override page). + // * 'type' - text. The human readable type of the field. (e.g. "Entity Reference") + // * 'type_machine_name' - text. The machine name of the type (e.g. 'entity_reference') + // * 'required' - True|False. Whether the field is required. + // * 'cardinality' - int. -1 for no limit, positive integer for a limited number. + // * 'auto_create' - True|False (Optional). Whether a relationship field allows new entity creation on the fly. + // * 'auto_create_bundle' - string. The bundle new entities are created into. + // * 'target_bundles' - array. List of bundle names that are the target of this field. + // * 'search_api' - associative array. Has the following keys: + // * 'in_search_api' - True|False. True if some field matches. + // * 'fields' - array. The 'fields' array is keyed by the machine names of the search api fields, + // which are arrays with the following fields: + // * 'search_api_field_name' - the machine name of the search api field. + // * 'search_api_field_label' - the human readable label of the search api field. + // * 'property_path' - the method for getting the values. + // * 'index_name' - the name of the Search API index (in case there are more than one) + // * 'fields_included' - for aggregate and EDTF processor fields, which fields are in that field. + // * 'facets' - associative array. has the following keys + // * 'has_facet' - True|False. True if there is a facet, even if not showing up (can use with url alias) + // * 'facets' - array. the 'facets' array is keyed by the machine name of the facet. each facet is an + // array with keys: + // * 'facet_name' + // * 'facet_machine_name' + // * 'facet_source' + // * 'url_alias' + // * 'block_visible' + + $metadata_profile = []; + $field_definitions = $this->entityFieldManager->getFieldDefinitions('node', $bundle); + foreach ($field_definitions as $field_name => $field_definition) { + $metadata_profile[$field_name] = $this->format_field($field_name, $field_definition); + } + } protected function format_field(string $field_name, FieldDefinitionInterface $field_definition) { $render_array = []; if (str_starts_with($field_name, 'field_') or $field_name == 'title') { @@ -106,16 +153,7 @@ class MetadataProfileController extends ControllerBase { '#prefix' => '(', '#suffix' => ') ', ], - 'edit_link' => [ - '#type' => 'link', - '#url' => Url::fromRoute('entity.field_config.node_field_edit_form', [ - 'node_type' => $field_definition->getTargetBundle(), - 'field_config' => $field_definition->id(), - 'destination' => Url::fromRoute('metadata_profile.node_profile', ['content_type' => $field_definition->getTargetBundle()], ['fragment' => $field_name])->toString(), - ]), - '#title' => $this->t('Edit'), - '#attributes' => ['class' => ['edit-link']], - ], + 'edit_link' => $this->formatFieldEditLink($field_definition), 'table' => [ '#type' => 'table', '#header' => ['Field Configuration',''], @@ -140,6 +178,34 @@ class MetadataProfileController extends ControllerBase { return $render_array; } + protected function formatFieldEditLink(FieldDefinitionInterface $field_definition) { + if ($field_definition->getFieldStorageDefinition() instanceof BaseFieldDefinition) { + if ($this->moduleHandler()->moduleExists('base_field_override_ui')) { + $edit_url = Url::fromRoute('entity.base_field_override.node_base_field_override_edit_form', [ + 'node_type' => $field_definition->getTargetBundle(), + 'base_field_override' => $field_definition->id(), + 'destination' => Url::fromRoute('metadata_profile.node_profile', ['content_type' => $field_definition->getTargetBundle()], ['fragment' => $field_definition->getName()])->toString(), + ]); + } + else { + return []; + } + } + else { + $edit_url = Url::fromRoute('entity.field_config.node_field_edit_form', [ + 'node_type' => $field_definition->getTargetBundle(), + 'field_config' => $field_definition->id(), + 'destination' => Url::fromRoute('metadata_profile.node_profile', ['content_type' => $field_definition->getTargetBundle()], ['fragment' => $field_definition->getName()])->toString(), + ]); + } + return [ + '#type' => 'link', + '#url' => $edit_url, + '#title' => $this->t('Edit'), + '#attributes' => ['class' => ['edit-link']], + ]; + } + protected function getFieldTableRows(FieldDefinitionInterface $field_definition) { $rows = [ [$this->t('Type:'), $this->formatType($field_definition)], @@ -240,7 +306,6 @@ class MetadataProfileController extends ControllerBase { 'col0' => $this->t('Search API index'), ], '#rows' => [], - '#caption' => $this->t('Search API') ]; $storage = $field_definition->getFieldStorageDefinition(); if ($storage instanceof BaseFieldDefinition) { @@ -251,27 +316,40 @@ class MetadataProfileController extends ControllerBase { $field_id = 'field.storage.' . $storage->get('id'); } - $indexes = $this->configFactory->listAll('search_api.index'); - foreach ($indexes as $index) { + $search_api_indexes = $this->configFactory->listAll('search_api.index'); + foreach ($search_api_indexes as $index) { $index_config = $this->config($index); // Loop over fields. foreach ($index_config->get('field_settings') as $field_setting_name => $field_setting) { // Check dependencies. $field_dependencies = $field_setting['dependencies']['config'] ?: []; if (in_array($field_id, $field_dependencies)) { - $build['#rows'][$field_setting_name] = $this->format_solr_field($field_setting_name, $field_setting, $index_config); + $build['#rows'][$field_setting_name] = $this->format_search_api_field($field_setting_name, $field_setting, $index_config); } // Check by property path. if ($field_setting['datasource_id'] == 'entity:node' and $field_setting['property_path'] == $field_definition->getName()) { - $build['#rows'][$field_setting_name] = $this->format_solr_field($field_setting_name, $field_setting, $index_config); + $build['#rows'][$field_setting_name] = $this->format_search_api_field($field_setting_name, $field_setting, $index_config); } // Get Aggregated Fields. if ($field_setting['property_path'] == 'aggregated_field') { if (in_array('entity:node/' . $field_definition->getName(), $field_setting['configuration']['fields'])) { - $build['#rows'][$field_setting_name] = $this->format_solr_field($field_setting_name, $field_setting, $index_config); + $build['#rows'][$field_setting_name] = $this->format_search_api_field($field_setting_name, $field_setting, $index_config); + } + } + // Get EDTF year. + if ($field_setting['property_path'] == 'edtf_year') { + $edtf_year_fields = $index_config->get('processor_settings')['edtf_year_only']['fields'] ?: []; + if (in_array(str_replace('.', '|', $field_definition->id()), $edtf_year_fields)) { + $build['#rows'][$field_setting_name] = $this->format_search_api_field($field_setting_name, $field_setting, $index_config); + } + } + // Get EDTF Date + if ($field_setting['property_path'] == 'edtf_dates') { + $edtf_date_fields = $index_config->get('processor_settings')['edtf_date_processor']['fields'] ?: []; + if (in_array(str_replace('.','|',$field_definition->id()), $edtf_date_fields)) { + $build['#rows'][$field_setting_name] = $this->format_search_api_field($field_setting_name, $field_setting, $index_config); } } - // TODO: Get EDTFyear. } if (count($build['#rows']) == 0) { @@ -285,7 +363,7 @@ class MetadataProfileController extends ControllerBase { } } - protected function format_solr_field($field_setting_name, $field_setting, $index_config) { + protected function format_search_api_field($field_setting_name, $field_setting, $index_config) { $build = [ 'data' => [ $field_setting['label'], @@ -296,9 +374,58 @@ class MetadataProfileController extends ControllerBase { ] ]; if ($field_setting['property_path'] == 'aggregated_field') { - $build['data'][2] .= ' -' . implode(', -', $field_setting['configuration']['fields']); + $list_render_array = [ + '#theme' => 'item_list', + '#items' => $field_setting['configuration']['fields'] + ]; + $cell_element = [ 'data' => [ + '#type' => 'container', + 'label' => ['#markup' => $build['data'][2]], + 'list' => $list_render_array, + ]]; + + $build['data'][2] = $cell_element; + } + if (in_array($field_setting['property_path'], ['edtf_year', 'edtf_dates'])) { + $list_render_array = [ + '#theme' => 'item_list', + '#items' => $index_config->get('processor_settings')['edtf_year_only']['fields'] ?: $this->t('No fields avaailable') + ]; + $cell_element = [ 'data' => [ + '#type' => 'container', + 'label' => ['#markup' => $build['data'][2]], + 'list' => $list_render_array, + ]]; + + $build['data'][2] = $cell_element; } return $build; } + public function test() { + $list = [ + 'one', + 'two', + 'three' + ]; + $list_render_array = [ + '#theme' => 'item_list', + '#items' => $list, + ]; + $test = [ + 'data' => $list_render_array + ]; + $rows = [ + ['bar', 'xyz'], + ['baz', $test ], + + ]; + $build = [ + '#type' => 'table', + '#rows' => $rows, + + ]; + return $build; + + } }