Skip to content

Commit

Permalink
Fix getting translation repository
Browse files Browse the repository at this point in the history
  • Loading branch information
loic425 committed Sep 11, 2024
1 parent 028de50 commit 1d9faa2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/Bundle/EventListener/ORMTranslatableListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
use Doctrine\ORM\Event\OnClassMetadataNotFoundEventArgs;
use Doctrine\ORM\Event\PostLoadEventArgs;
use Doctrine\ORM\Events;
use Doctrine\ORM\Mapping\ClassMetadata;
Expand Down Expand Up @@ -72,6 +73,21 @@ public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs): void
}
}

public function onClassMetadataNotFound(OnClassMetadataNotFoundEventArgs $eventArgs): void
{
$resourceMetadata = $this->getResourceMetadataByInterface($eventArgs->getClassName());

if (null === $resourceMetadata) {
return;
}

$eventArgs->setFoundMetadata(
$eventArgs
->getObjectManager()
->getClassMetadata($resourceMetadata->getClass('model')),
);
}

public function postLoad(PostLoadEventArgs $args): void
{
$entity = $args->getObject();
Expand Down Expand Up @@ -216,4 +232,19 @@ private function processTranslatableEntityLocaleAssigner(object $translatableEnt

return $translatableEntityLocaleAssigner;
}

private function getResourceMetadataByInterface(string $interface): ?MetadataInterface
{
foreach ($this->resourceMetadataRegistry->getAll() as $metadata) {
if (false === $metadata->hasClass('interface')) {
continue;
}

if ($interface === $metadata->getClass('interface')) {
return $metadata;
}
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
<argument type="service" id="sylius.translatable_entity_locale_assigner" />
<tag name="doctrine.event_listener" connection="default" event="loadClassMetadata" priority="99" />
<tag name="doctrine.event_listener" connection="default" event="postLoad" priority="99" />
<!-- <tag name="doctrine.event_subscriber" connection="default" priority="99" />-->
<tag name="doctrine.event_listener" connection="default" event="onClassMetadataNotFound" priority="99" />
</service>
<service id="Sylius\Bundle\ResourceBundle\EventListener\ORMTranslatableListener" alias="sylius.translation.translatable_listener.doctrine.orm" />

Expand Down

0 comments on commit 1d9faa2

Please sign in to comment.