diff --git a/src/Event/GraphQL/AssetMetadataEvents.php b/src/Event/GraphQL/AssetMetadataEvents.php new file mode 100644 index 00000000..d4b1e896 --- /dev/null +++ b/src/Event/GraphQL/AssetMetadataEvents.php @@ -0,0 +1,26 @@ +getAssetFromValue($value, $context); + $metadata = $asset?->getMetadata(raw: true); + if (!$metadata) { + return null; + } - if ($asset) { - $metadata = $asset->getObjectVar('metadata'); - if ($metadata) { - if (isset($args['ignore_language']) && $args['ignore_language']) { - return $metadata; - } + //Relational data would return as a comma separated list. each element type and id by `_`, eg. "object_154,asset_489" + $event = new AssetEvent($asset, [ + 'metadata' => $metadata, + 'context' => $context + ]); + /** @var EventDispatcher $eventDispatcher */ + $eventDispatcher = \Pimcore::getContainer()->get('event_dispatcher'); + $eventDispatcher->dispatch($event, AssetMetadataEvents::PRE_RESOLVE); + $metadata = $event->getArgument('metadata'); + + if (isset($args['ignore_language']) && $args['ignore_language']) { + return $metadata; + } - $map = []; - $keys = []; - $language = isset($args['language']) ? $args['language'] : $this->getGraphQlService()->getLocaleService()->findLocale(); + $map = []; + $keys = []; + $language = $args['language'] ?? $this->getGraphQlService()->getLocaleService()->findLocale(); - foreach ($metadata as $item) { - $keys[$item['name']] = 1; - $l = $item['language'] ? $item['language'] : 'default'; - $map[$l][$item['name']] = $item; - } - $result = []; - - foreach ($keys as $key => $found) { - if (isset($map[$language][$key])) { - $result[] = $map[$language][$key]; - } elseif (isset($map['default'][$key])) { - $result[] = $map['default'][$key]; - } - } + foreach ($metadata as $item) { + $keys[$item['name']] = 1; + $l = $item['language'] ?: 'default'; + $map[$l][$item['name']] = $item; + } + $result = []; - if ($result) { - return $result; - } + foreach ($keys as $key => $found) { + if (isset($map[$language][$key])) { + $result[] = $map[$language][$key]; + } elseif (isset($map['default'][$key])) { + $result[] = $map['default'][$key]; } } + if ($result) { + return $result; + } + return null; }