Skip to content

Commit

Permalink
[Improvement] Default Asset Metadata should be in the index (#259)
Browse files Browse the repository at this point in the history
* add default asset metadata to the index

* fix: store default metadata values only when set
  • Loading branch information
lukmzig authored Nov 25, 2024
1 parent 6ff7c7e commit f5611e4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
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 @@ -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
Expand All @@ -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;
}
}
4 changes: 2 additions & 2 deletions src/Service/SearchIndex/Asset/MetadataProviderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit f5611e4

Please sign in to comment.