Skip to content

Commit

Permalink
fix: store default metadata values only when set
Browse files Browse the repository at this point in the history
  • Loading branch information
lukmzig committed Nov 25, 2024
1 parent db7e52f commit cdf6184
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 35 deletions.
2 changes: 0 additions & 2 deletions src/SearchIndexAdapter/Asset/MappingProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@

interface MappingProviderInterface
{
public const DEFAULT_METADATA = ['title', 'alt', 'copyright'];

/**
* @return MappingProperty[]
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
);
}

Expand Down
30 changes: 1 addition & 29 deletions src/Service/SearchIndex/Asset/MetadataProviderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function getSearchableMetaDataForAsset(Asset $asset): array
}
}

return $this->addDefaultMetadata($result);
return $result;
}

/**
Expand Down Expand Up @@ -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;
}
}

0 comments on commit cdf6184

Please sign in to comment.