Du kan inte välja fler än 25 ämnen
Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
|
- <?php
-
- namespace Lc\SovBundle\EventSubscriber\Translation;
-
- use Doctrine\ORM\PersistentCollection;
- use Doctrine\ORM\Query;
- use EasyCorp\Bundle\EasyAdminBundle\Event\AfterCrudActionEvent;
- use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeCrudActionEvent;
- use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
- use Gedmo\Translatable\TranslatableListener;
- use Lc\SovBundle\Doctrine\Extension\TranslatableInterface;
- use Lc\SovBundle\Doctrine\EntityManager;
- use Symfony\Component\EventDispatcher\EventSubscriberInterface;
- use function Symfony\Component\Translation\t;
-
- class TranslationEasyAdminSubscriber implements EventSubscriberInterface
- {
- protected $em;
-
- public function __construct(EntityManager $entityManager)
- {
- $this->em = $entityManager;
- }
-
- public static function getSubscribedEvents()
- {
- return [
- BeforeCrudActionEvent::class => ['setTranslatableLocale']
- ];
- }
-
- public function setTranslatableLocale(BeforeCrudActionEvent $event)
- {
-
- $entity = $event->getAdminContext()->getEntity()->getInstance();
- $_locale = $event->getAdminContext()->getRequest()->get('_locale');
-
- if($entity instanceof TranslatableInterface){
-
- //parcours les champs association pour détecter un champ de association qui est traduisible
- foreach ($this->em->getClassMetadata(get_class($entity))->getAssociationMappings() as $associationMapping){
- if (in_array(TranslatableInterface::class, class_implements($associationMapping["targetEntity"]))){
- if($entity->__get($associationMapping['fieldName']) instanceof PersistentCollection){
- foreach ($entity->__get($associationMapping['fieldName']) as $item){
- $item->setTranslatableLocale($_locale);
- $this->em->refresh($item);
- }
- }else{
- if($entity->__get($associationMapping['fieldName'])) {
- $entity->__get($associationMapping['fieldName'])->setTranslatableLocale($_locale);
- $this->em->refresh($entity->__get($associationMapping['fieldName']));
- }
- }
-
- }
- }
- $entity->setTranslatableLocale($_locale);
- $this->em->refresh($entity);
- }
- }
-
-
- }
|