src/Form/EventListener/Admin/Locale/AddNameFieldSubscriber.php line 28

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Form\EventListener\Admin\Locale;
  4. use App\Entity\Admin\Locale;
  5. use App\Entity\Admin\LocaleInterface;
  6. use App\Form\FormHelper;
  7. use App\Repository\Admin\LocaleRepository;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  10. use Symfony\Component\Form\FormEvent;
  11. use Symfony\Component\Form\FormEvents;
  12. class AddNameFieldSubscriber extends FormHelper implements EventSubscriberInterface
  13. {
  14.     public function __construct(
  15.         private LocaleRepository $localeRepository
  16.     ) {
  17.     }
  18.     public static function getSubscribedEvents(): array
  19.     {
  20.         return [FormEvents::PRE_SET_DATA => 'preSetData'];
  21.     }
  22.     public function preSetData(FormEvent $event): void
  23.     {
  24.         /** @var Locale $locale */
  25.         $locale $event->getData();
  26.         $locales array_diff(LocaleInterface::LOCALES$this->localeRepository->findCodesExcept($locale->getCode()));
  27.         $event->getForm()->add('name'ChoiceType::class, $this->getChoiceParameters('app.global.fields.name'self::PLACEHOLDER_SAME_AS_LABELtrue, [
  28.             'choices' => $locales,
  29.             'data' => $locale->getCode(),
  30.         ]));
  31.     }
  32. }