vendor/roothirsch/core-bundle/Behaviors/Translatable/TranslatableFormSubscriber.php line 80

Open in your IDE?
  1. <?php /** @noinspection PhpUnused */
  2. namespace Roothirsch\CoreBundle\Behaviors\Translatable;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Gedmo\Translatable\Entity\Repository\TranslationRepository;
  5. use Gedmo\Translatable\Entity\Translation;
  6. use Roothirsch\CoreBundle\Translation\Repository\LanguageRepository;
  7. use Symfony\Component\Asset\Packages;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  10. use Symfony\Component\Form\FormEvent;
  11. use Symfony\Component\Form\FormEvents;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Symfony\Component\PropertyAccess\PropertyAccess;
  15. use Symfony\Component\Validator\Constraints\NotBlank;
  16. use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupCollectionInterface;
  17. use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupInterface;
  18. class TranslatableFormSubscriber implements EventSubscriberInterface
  19. {
  20.     /**
  21.      * @var array
  22.      */
  23.     protected $locales = [];
  24.     /**
  25.      * @var string
  26.      */
  27.     protected $defaultLocale;
  28.     /**
  29.      * @var Request
  30.      */
  31.     protected $request;
  32.     /**
  33.      * @var TranslationRepository
  34.      */
  35.     protected $translationRepository;
  36.     /**
  37.      * @var Packages
  38.      */
  39.     private $manager;
  40.     /**
  41.      * @param RequestStack           $request
  42.      * @param EntityManagerInterface $entityManager
  43.      * @param LanguageRepository     $languageRepository
  44.      */
  45.     public function __construct(RequestStack $requestEntityManagerInterface $entityManagerLanguageRepository $languageRepositoryPackages $manager)
  46.     {
  47.         foreach($languageRepository->findAll() as $language) {
  48.             $this->locales[] = $language;
  49.             if ($language->isDefaultLanguage() && empty($this->defaultLocale)) {
  50.                 $this->defaultLocale $language->getLanguageKey();
  51.             }
  52.         }
  53.         $this->request $request->getCurrentRequest();
  54.         $this->translationRepository $entityManager->getRepository(Translation::class);
  55.         $this->manager $manager;
  56.     }
  57.     /**
  58.      * {@inheritdoc}
  59.      */
  60.     public static function getSubscribedEvents()
  61.     {
  62.         return [
  63.             FormEvents::PRE_SET_DATA => 'preSetData',
  64.             FormEvents::POST_SUBMIT => 'postSubmit',
  65.         ];
  66.     }
  67.     /**
  68.      * @param FormEvent $event
  69.      */
  70.     public function preSetData(FormEvent $event)
  71.     {
  72.         $form $event->getForm();
  73.         $options $form->getConfig()->getOptions();
  74.         $requiredLocales $options[TranslatableType::REUIRED_LOCALES];
  75.         $entity $form->getRoot()->getData();
  76.         if (empty($entity)) {
  77.             return false;
  78.         }
  79.         $propertyAccessor PropertyAccess::createPropertyAccessor();
  80.         $propertyName $form->getPropertyPath()->__toString();
  81.         $translations $this->translationRepository->findTranslations($entity);
  82.         foreach ($this->locales as $locale) {
  83.             if ($locale->isDefaultLanguage()) {
  84.                 $emptyData $translations[$locale->getLanguageKey()][$propertyName] ?? $propertyAccessor->getValue($entity$propertyName);
  85.             } else {
  86.                 $emptyData $translations[$locale->getLanguageKey()][$propertyName] ?? '';
  87.             }
  88.             $attribute $options[TranslatableType::FIELD_OPTIONS]['attr'] ?? [];
  89.             $attribute['is_active_tab'] = $locale->getLanguageKey() === $this->defaultLocale;
  90.             $attribute['unique'] = uniqid($locale->getLanguageKey());
  91.             $attribute['is_bool'] = false;
  92.             $attribute['image'] = $this->manager->getUrl('main_theme/media/language_flags/' $locale->getLanguageKey() . '.png'"main");
  93.             if (in_array($options[TranslatableType::TYPE], [CheckboxType::class])) {
  94.                 $emptyData boolval($emptyData);
  95.                 $attribute['is_bool'] = true;
  96.             }
  97.             $constraints = [];
  98.             if (in_array($locale->getLanguageKey(), $requiredLocales)) {
  99.                 $constraints[] = new NotBlank(['message' => 'Field can\'t be blank!']);
  100.             }
  101.             $form->add(
  102.                 $locale->getLanguageKey() . '_content',
  103.                 $options[TranslatableType::TYPE],
  104.                 array_merge(
  105.                     [
  106.                         'label' => $locale->getLanguageName(),
  107.                         'data' => $emptyData
  108.                     ],
  109.                     $options[TranslatableType::FIELD_OPTIONS],
  110.                     [
  111.                         'attr' => $attribute
  112.                     ],
  113.                     ['constraints' => $constraints]
  114.                 )
  115.             );
  116.         }
  117.     }
  118.     /**
  119.      * @param FormEvent $event
  120.      *
  121.      * @return false|void
  122.      */
  123.     public function postSubmit(FormEvent $event)
  124.     {
  125.         $form $event->getForm();
  126.         $entity $form->getRoot()->getData();
  127.         if (empty($entity)) {
  128.             return false;
  129.         }
  130.         $propertyName $form->getPropertyPath()->__toString();
  131.         foreach ($this->locales as $locale) {
  132.             $data $form->get($locale->getLanguageKey() . '_content')->getData();
  133.             $this->translationRepository->translate($entity$propertyName$locale->getLanguageKey(), $data);
  134.         }
  135.     }
  136. }