Skip to content

Commit

Permalink
add default asset metadata to the index
Browse files Browse the repository at this point in the history
  • Loading branch information
lukmzig committed Nov 25, 2024
1 parent 7c4a2f2 commit f2a2ad9
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 15 deletions.
2 changes: 2 additions & 0 deletions src/SearchIndexAdapter/Asset/MappingProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

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 @@ -41,25 +41,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());
}

private function getTypeMapping(string $type): ?array
Expand All @@ -68,4 +58,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
{
$mappingProperties = [];
foreach (self::DEFAULT_METADATA as $metadata) {
$mappingProperties[] = new MappingProperty(
$metadata,
'input',
$this->getLanguageMappingByType([MappingProperty::NOT_LOCALIZED_KEY], 'input'),
[MappingProperty::NOT_LOCALIZED_KEY]
);
}

return $mappingProperties;
}
}
34 changes: 31 additions & 3 deletions src/Service/SearchIndex/Asset/MetadataProviderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,17 @@ 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;
}
}
}

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

/**
Expand Down Expand Up @@ -90,4 +90,32 @@ 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 f2a2ad9

Please sign in to comment.