Skip to content

Commit

Permalink
Working solution
Browse files Browse the repository at this point in the history
  • Loading branch information
kisztof committed Aug 9, 2023
1 parent c7aa419 commit 48b6411
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 57 deletions.
85 changes: 29 additions & 56 deletions src/lib/Repository/EventSubscriber/NameSchemaSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -96,55 +74,50 @@ 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<string, string> $tokens
* @param array<string> $languageCodes
* @param array<int, mixed> $attributes
* @param array<string, mixed> $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) {
continue;
}
$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,
Expand Down
5 changes: 4 additions & 1 deletion src/lib/Repository/NameSchema/NameSchemaService.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,11 @@ public function resolveNameSchema(

$event = $this->eventDispatcher->dispatch(
new ResolveContentNameSchemaEvent(
$content,
$schemaIdentifiers,
$content
$contentType,
$fieldMap,
$languageCodes
)
);

Expand Down

0 comments on commit 48b6411

Please sign in to comment.