From cdf6184ef3bf9b2949889cb52360806ced7692c8 Mon Sep 17 00:00:00 2001 From: lukmzig Date: Mon, 25 Nov 2024 16:01:45 +0100 Subject: [PATCH] fix: store default metadata values only when set --- .../Asset/MappingProviderInterface.php | 2 -- .../Asset/PredefinedAssetMetadataProvider.php | 10 ++++--- .../Asset/MetadataProviderService.php | 30 +------------------ 3 files changed, 7 insertions(+), 35 deletions(-) diff --git a/src/SearchIndexAdapter/Asset/MappingProviderInterface.php b/src/SearchIndexAdapter/Asset/MappingProviderInterface.php index 344d13af..cc16de40 100644 --- a/src/SearchIndexAdapter/Asset/MappingProviderInterface.php +++ b/src/SearchIndexAdapter/Asset/MappingProviderInterface.php @@ -20,8 +20,6 @@ interface MappingProviderInterface { - public const DEFAULT_METADATA = ['title', 'alt', 'copyright']; - /** * @return MappingProperty[] */ diff --git a/src/SearchIndexAdapter/DefaultSearch/Asset/PredefinedAssetMetadataProvider.php b/src/SearchIndexAdapter/DefaultSearch/Asset/PredefinedAssetMetadataProvider.php index 5992e591..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 @@ -49,7 +51,7 @@ public function getMappingProperties(): array ); } - return array_merge($mappingProperties, $this->getDefaultMetadataMapping()); + return array_merge($mappingProperties, $this->getDefaultMetadataMapping($languages)); } private function getTypeMapping(string $type): ?array @@ -77,15 +79,15 @@ private function getLanguageMappingByType(array $languages, string $type): array /** * @return MappingProperty[] */ - private function getDefaultMetadataMapping(): array + private function getDefaultMetadataMapping(array $languages): array { $mappingProperties = []; foreach (self::DEFAULT_METADATA as $metadata) { $mappingProperties[] = new MappingProperty( $metadata, 'input', - $this->getLanguageMappingByType([MappingProperty::NOT_LOCALIZED_KEY], 'input'), - [MappingProperty::NOT_LOCALIZED_KEY] + $this->getLanguageMappingByType($languages, 'input'), + $languages ); } diff --git a/src/Service/SearchIndex/Asset/MetadataProviderService.php b/src/Service/SearchIndex/Asset/MetadataProviderService.php index 0f12408c..7499658e 100644 --- a/src/Service/SearchIndex/Asset/MetadataProviderService.php +++ b/src/Service/SearchIndex/Asset/MetadataProviderService.php @@ -62,7 +62,7 @@ public function getSearchableMetaDataForAsset(Asset $asset): array } } - return $this->addDefaultMetadata($result); + return $result; } /** @@ -90,32 +90,4 @@ private function getMappingProviders(): array return $mappingProviders; } - - private function addDefaultMetadata(array $assetMetadata): array - { - $indexedMetadata = array_flip(array_column($assetMetadata, 'name')); - foreach ($this->getDefaultMetadataValues() as $defaultEntry) { - if (!isset($indexedMetadata[$defaultEntry['name']])) { - $assetMetadata[] = $defaultEntry; - } - } - - return $assetMetadata; - } - - private function getDefaultMetadataValues(): array - { - $defaultMetadata = []; - foreach (MappingProviderInterface::DEFAULT_METADATA as $key) { - - $defaultMetadata[] = [ - 'name' => $key, - 'type' => 'input', - 'language' => MappingProperty::NOT_LOCALIZED_KEY, - 'data' => '', - ]; - } - - return $defaultMetadata; - } }