From f5611e496a56cf1a996d185e4e249feabcc9de6c Mon Sep 17 00:00:00 2001 From: lukmzig <30526586+lukmzig@users.noreply.github.com> Date: Mon, 25 Nov 2024 16:13:43 +0100 Subject: [PATCH] [Improvement] Default Asset Metadata should be in the index (#259) * add default asset metadata to the index * fix: store default metadata values only when set --- .../Asset/PredefinedAssetMetadataProvider.php | 49 ++++++++++++++----- .../Asset/MetadataProviderService.php | 4 +- 2 files changed, 39 insertions(+), 14 deletions(-) diff --git a/src/SearchIndexAdapter/DefaultSearch/Asset/PredefinedAssetMetadataProvider.php b/src/SearchIndexAdapter/DefaultSearch/Asset/PredefinedAssetMetadataProvider.php index 74305e62..a19f8df7 100644 --- a/src/SearchIndexAdapter/DefaultSearch/Asset/PredefinedAssetMetadataProvider.php +++ b/src/SearchIndexAdapter/DefaultSearch/Asset/PredefinedAssetMetadataProvider.php @@ -27,6 +27,8 @@ */ final readonly class PredefinedAssetMetadataProvider implements MappingProviderInterface { + private const DEFAULT_METADATA = ['title', 'alt', 'copyright']; + public function __construct( private LanguageServiceInterface $languageService, private FieldDefinitionServiceInterface $fieldDefinitionService @@ -41,25 +43,15 @@ public function getMappingProperties(): array $languages = array_merge([MappingProperty::NOT_LOCALIZED_KEY], $this->languageService->getValidLanguages()); foreach ($predefinedMetaDataList as $predefinedMetaData) { - $languageMapping = [ - 'properties' => [], - ]; - - if ($typeMapping = $this->getTypeMapping($predefinedMetaData->getType())) { - foreach ($languages as $language) { - $languageMapping['properties'][$language] = $typeMapping; - } - } - $mappingProperties[] = new MappingProperty( $predefinedMetaData->getName(), $predefinedMetaData->getType(), - $languageMapping, + $this->getLanguageMappingByType($languages, $predefinedMetaData->getType()), $languages ); } - return $mappingProperties; + return array_merge($mappingProperties, $this->getDefaultMetadataMapping($languages)); } private function getTypeMapping(string $type): ?array @@ -68,4 +60,37 @@ private function getTypeMapping(string $type): ?array ->getFieldDefinitionAdapter($type) ?->getIndexMapping(); } + + private function getLanguageMappingByType(array $languages, string $type): array + { + $languageMapping = [ + 'properties' => [], + ]; + + if ($typeMapping = $this->getTypeMapping($type)) { + foreach ($languages as $language) { + $languageMapping['properties'][$language] = $typeMapping; + } + } + + return $languageMapping; + } + + /** + * @return MappingProperty[] + */ + private function getDefaultMetadataMapping(array $languages): array + { + $mappingProperties = []; + foreach (self::DEFAULT_METADATA as $metadata) { + $mappingProperties[] = new MappingProperty( + $metadata, + 'input', + $this->getLanguageMappingByType($languages, 'input'), + $languages + ); + } + + return $mappingProperties; + } } diff --git a/src/Service/SearchIndex/Asset/MetadataProviderService.php b/src/Service/SearchIndex/Asset/MetadataProviderService.php index 06618fa5..7499658e 100644 --- a/src/Service/SearchIndex/Asset/MetadataProviderService.php +++ b/src/Service/SearchIndex/Asset/MetadataProviderService.php @@ -52,10 +52,10 @@ public function getSearchableMetaDataForAsset(Asset $asset): array if (is_array($metadata) && isset($metadata['data'], $metadata['name'], $metadata['type'])) { $mappingProperty = $metaDataMap[$metadata['name']] ?? null; $language = $metadata['language'] ?? null; - $language = $language ?: MappingProperty::NOT_LOCALIZED_KEY; + $metadata['language'] = $language ?: MappingProperty::NOT_LOCALIZED_KEY; if ($mappingProperty && $mappingProperty->getType() === $metadata['type'] - && in_array($language, $mappingProperty->getLanguages(), true) + && in_array($metadata['language'], $mappingProperty->getLanguages(), true) ) { $result[] = $metadata; }