diff --git a/src/Controller/MetadataProfileController.php b/src/Controller/MetadataProfileController.php index 18a8154..1a1135e 100644 --- a/src/Controller/MetadataProfileController.php +++ b/src/Controller/MetadataProfileController.php @@ -194,11 +194,13 @@ class MetadataProfileController extends ControllerBase { $container->get('entity_type.manager'), ); } + /** * Builds a reverse map of field name → indexed field entries across all * Search API indexes. * * Map structure: + * * @code * [ * 'field_abstract' => [ @@ -241,12 +243,12 @@ class MetadataProfileController extends ControllerBase { $facetsByFieldId = []; foreach ($this->allFacets as $facet_id => $facet) { $facetsByFieldId[$facet->get('field_identifier')][] = [ - 'field_identifier' => $facet->get('field_identifier'), - 'facet_name' => $facet->getName(), - 'facet_machine_name' => $facet_id, - 'facet_source' => $facet->getFacetSourceId(), - 'url_alias' => $facet->getUrlAlias(), - 'block_visible' => $this->getBlockVisible($facet), + 'field_identifier' => $facet->get('field_identifier'), + 'facet_name' => $facet->getName(), + 'facet_machine_name' => $facet_id, + 'facet_source' => $facet->getFacetSourceId(), + 'url_alias' => $facet->getUrlAlias(), + 'block_visible' => $this->getBlockVisible($facet), ]; } @@ -329,7 +331,8 @@ class MetadataProfileController extends ControllerBase { * The bundle configuration object. * * @return array - * A render array containing the bundle label, machine name, and description. + * A render array containing the bundle label, machine name, and + * description. */ protected function formatBundle(Config $bundle) { if (str_starts_with($bundle->getName(), 'node.type')) { @@ -490,8 +493,8 @@ class MetadataProfileController extends ControllerBase { return [ 'in_search_api' => !empty($indexed_fields), - 'has_facet' => $has_facet, - 'fields' => $indexed_fields, + 'has_facet' => $has_facet, + 'fields' => $indexed_fields, ]; } @@ -621,24 +624,24 @@ class MetadataProfileController extends ControllerBase { // Resolve included fields for aggregate / EDTF paths. $fields_included = match ($property_path) { 'aggregated_field' => $field_setting['configuration']['fields'] ?? [], - 'edtf_year' => $index_config->get('processor_settings.edtf_year_only.fields') ?? [], + 'edtf_year' => $index_config->get('processor_settings.edtf_year_only.fields') ?? [], 'edtf_dates' => $index_config->get('processor_settings.edtf_date_processor.fields') ?? [], default => [], }; // O(1) facet lookup from the pre-built map. - $facets = $facets_by_field_id[$field_setting_name] ?? []; + $facets = $facets_by_field_id[$field_setting_name] ?? []; $has_facet = !empty($facets); return [ - 'search_api_field_name' => $field_setting_name, + 'search_api_field_name' => $field_setting_name, 'search_api_field_label' => $field_setting['label'] ?? '', - 'property_path' => $property_path, - 'type' => $field_setting['type'] ?? '', - 'index_name' => $index_config->get('name'), - 'fields_included' => $fields_included, - 'has_facet' => $has_facet, - 'facets' => $facets, + 'property_path' => $property_path, + 'type' => $field_setting['type'] ?? '', + 'index_name' => $index_config->get('name'), + 'fields_included' => $fields_included, + 'has_facet' => $has_facet, + 'facets' => $facets, ]; } @@ -810,7 +813,7 @@ class MetadataProfileController extends ControllerBase { // Default sort if no column has been clicked yet. if (!$order) { - usort($rows, function ($a, $b) { + usort($rows, function($a, $b) { return ($b[11] ?? 0) <=> ($a[11] ?? 0); }); return; @@ -829,7 +832,7 @@ class MetadataProfileController extends ControllerBase { return; } - usort($rows, function ($a, $b) use ($column_index, $sort) { + usort($rows, function($a, $b) use ($column_index, $sort) { $a_value = $this->extractSortableValue($a[$column_index] ?? ''); $b_value = $this->extractSortableValue($b[$column_index] ?? ''); @@ -1397,7 +1400,7 @@ class MetadataProfileController extends ControllerBase { if (is_array($element)) { if (isset($element['data']) && array_key_exists('#markup', $element['data'])) { preg_match('/title=["\']([^"\']*)["\']/', $element['data']['#markup'], $matches); - $rows[$row_key][$element_key] = $matches[1] ?? null; + $rows[$row_key][$element_key] = $matches[1] ?? NULL; } else { $rows[$row_key][$element_key] = implode('|', $element); @@ -1420,22 +1423,22 @@ class MetadataProfileController extends ControllerBase { * @return \Symfony\Component\HttpFoundation\BinaryFileResponse * The file download response. */ - public function download(NodeTypeInterface $node_type) { + public function download(?NodeTypeInterface $node_type = NULL) { // Rebuild your state from the entity - $this->entityBundle = $node_type->id(); - $this->entityTypeBundleOf = $node_type->getEntityType()->getBundleOf(); + if ($node_type) { + $this->entityBundle = $node_type->id(); + $this->entityTypeBundleOf = $node_type->getEntityType()->getBundleOf(); + } - $contentType = $this->entityBundle; $entityKey = $this->entityTypeBundleOf; - - $headers = array_map(function ($header) { + $headers = array_map(function($header) { return is_array($header) && isset($header['data']) ? (string) $header['data'] : (string) $header; }, $this->getHeaders()); $metadata_profile = $this->getMetadataProfile(); $rows = $this->getRows($metadata_profile); $rows = $this->sanitizeRowsForCSV($rows); - $filename_slug = "{$entityKey}__{$contentType}.csv"; + $filename_slug = "{$entityKey}__{$this->entityBundle}.csv"; $filename = $this->fileSystem->getTempDirectory() . '/' . $filename_slug; if (file_exists($filename)) { @@ -1613,7 +1616,6 @@ class MetadataProfileController extends ControllerBase { } foreach ($field_definitions as $field_name => $field_definition) { - // Only configurable fields (skip base fields like title) if ($field_definition->getFieldStorageDefinition() instanceof \Drupal\Core\Field\BaseFieldDefinition) { continue;