diff --git a/src/lib/Repository/EventSubscriber/NameSchemaSubscriber.php b/src/lib/Repository/EventSubscriber/NameSchemaSubscriber.php index 192a398a18..befa7e238d 100644 --- a/src/lib/Repository/EventSubscriber/NameSchemaSubscriber.php +++ b/src/lib/Repository/EventSubscriber/NameSchemaSubscriber.php @@ -9,6 +9,7 @@ namespace Ibexa\Core\Repository\EventSubscriber; use Ibexa\Contracts\Core\Event\NameSchema\AbstractNameSchemaEvent; +use Ibexa\Contracts\Core\Event\NameSchema\AbstractSchemaEvent; use Ibexa\Contracts\Core\Event\NameSchema\ResolveContentNameSchemaEvent; use Ibexa\Contracts\Core\Event\NameSchema\ResolveNameSchemaEvent; use Ibexa\Contracts\Core\Event\NameSchema\ResolveUrlAliasSchemaEvent; @@ -46,38 +47,15 @@ public static function getSubscribedEvents(): array public function onResolveNameSchema(ResolveNameSchemaEvent $event): void { - if (!$this->isValid($event)) { - return; - } - - $identifiers = $event->getSchemaIdentifiers()['field']; - $tokenValues = $event->getTokenValues(); - $fieldMap = $event->getFieldMap(); - - $contentType = $event->getContentType(); - foreach ($event->getLanguageCodes() as $languageCode) { - foreach ($identifiers as $identifier) { - $fieldDefinition = $contentType->getFieldDefinition($identifier); - if (null === $fieldDefinition) { - continue; - } - $persistenceFieldType = $this->fieldTypeRegistry->getFieldType($fieldDefinition->fieldTypeIdentifier); - - $fieldValue = $fieldMap[$identifier][$languageCode] ?? ''; - $fieldValue = $persistenceFieldType->getName( - $fieldValue, - $fieldDefinition, - $languageCode - ); - - $tokenValues[$languageCode][$identifier] = $fieldValue; - } - } - - $event->setTokenValues($tokenValues); + $this->processNameSchemaEvent($event); } public function onResolveContentNameSchema(ResolveContentNameSchemaEvent $event): void + { + $this->processNameSchemaEvent($event); + } + + public function onResolveUrlAliasSchema(ResolveUrlAliasSchemaEvent $event): void { if (!$this->isValid($event)) { return; @@ -96,47 +74,39 @@ public function onResolveContentNameSchema(ResolveContentNameSchemaEvent $event) $event->setTokenValues($tokenValues); } - public function onResolveUrlAliasSchema(ResolveUrlAliasSchemaEvent $event): void + public function isValid(AbstractSchemaEvent $event): bool + { + return array_key_exists('field', $event->getSchemaIdentifiers()); + } + + private function processNameSchemaEvent(AbstractNameSchemaEvent $event): void { if (!$this->isValid($event)) { return; } - $content = $event->getContent(); - $contentType = $content->getContentType(); $tokenValues = $this->processEvent( - $event->getContent()->getVersionInfo()->getLanguages(), + $event->getLanguageCodes(), $event->getSchemaIdentifiers()['field'], - $contentType, - $content, - $event->getTokenValues() + $event->getContentType(), + null, + $event->getTokenValues(), + $event->getFieldMap() ); $event->setTokenValues($tokenValues); } - public function isValid(AbstractNameSchemaEvent $event): bool - { - return array_key_exists('field', $event->getSchemaIdentifiers()); - } - - /** - * @param array $tokens - * @param array $languageCodes - * @param array $attributes - * @param array $tokenValues - * - * @return array - */ - public function processEvent( - $languages, - $identifiers, + private function processEvent( + array $languages, + array $identifiers, ContentType $contentType, - Content $content, - array $tokenValues + ?Content $content, + array $tokenValues, + ?array $fieldMap = null ): array { foreach ($languages as $language) { - $languageCode = $language->getLanguageCode(); + $languageCode = is_string($language) ? $language : $language->getLanguageCode(); foreach ($identifiers as $identifier) { $fieldDefinition = $contentType->getFieldDefinition($identifier); if (null === $fieldDefinition) { @@ -144,7 +114,10 @@ public function processEvent( } $persistenceFieldType = $this->fieldTypeRegistry->getFieldType($fieldDefinition->fieldTypeIdentifier); - $fieldValue = $content->getFieldValue($identifier, $languageCode); + $fieldValue = $fieldMap + ? $fieldMap[$identifier][$languageCode] ?? '' + : $content->getFieldValue($identifier, $languageCode); + $fieldValue = $persistenceFieldType->getName( $fieldValue, $fieldDefinition, diff --git a/src/lib/Repository/NameSchema/NameSchemaService.php b/src/lib/Repository/NameSchema/NameSchemaService.php index 7fc39c0d16..918842904d 100644 --- a/src/lib/Repository/NameSchema/NameSchemaService.php +++ b/src/lib/Repository/NameSchema/NameSchemaService.php @@ -108,8 +108,11 @@ public function resolveNameSchema( $event = $this->eventDispatcher->dispatch( new ResolveContentNameSchemaEvent( + $content, $schemaIdentifiers, - $content + $contentType, + $fieldMap, + $languageCodes ) );