src/Form/Subscriber/CountrySubscriber.php line 49

Open in your IDE?
  1. <?php
  2. namespace App\Form\Subscriber;
  3. use App\Entity\Common\Country;
  4. use App\Entity\Common\County;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Doctrine\ORM\EntityRepository;
  7. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\Form\FormEvent;
  10. use Symfony\Component\Form\FormEvents;
  11. class CountrySubscriber implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var EntityManagerInterface
  15.      */
  16.     private EntityManagerInterface $em;
  17.     public function __construct(EntityManagerInterface $em)
  18.     {
  19.         $this->em $em;
  20.     }
  21.     /**
  22.      * @inheritDoc
  23.      */
  24.     public static function getSubscribedEvents()
  25.     {
  26.         return [
  27.             FormEvents::PRE_SET_DATA => 'onPreSetData',
  28.             FormEvents::POST_SUBMIT => 'onPostSubmit'
  29.         ];
  30.     }
  31.     /**
  32.      * Adds *county* field to checkout form
  33.      *
  34.      * Available options of this field depend on the value of
  35.      * *country* field. If a valid country value exists,
  36.      * then only counties of that country will be loaded, otherwise
  37.      * an empty dropdown should be presented to end user.
  38.      *
  39.      * @param FormEvent $event
  40.      */
  41.     public function onPreSetData(FormEvent $event)
  42.     {
  43.         $country $event->getData();
  44.         $form $event->getForm()->getParent();
  45.         if (null !== $country) {
  46.             $form->add('county'EntityType::class, [
  47.                 'required' => false,
  48.                 'class' => County::class,
  49.                 'choice_label' => 'name',
  50.                 'placeholder' => 'Επιλέξτε νομό',
  51.                 'query_builder' => function (EntityRepository $er) use ($country) {
  52.                     return $er->createQueryBuilder('c')
  53.                         ->where('c.country = :country')
  54.                         ->setParameter('country'$country)
  55.                         ->orderBy('c.name');
  56.                 },
  57.                 'label' => '* Νομός'
  58.             ]);
  59.         } else {
  60.             $country $this->em->getRepository(Country::class)->find(86);
  61.             $form->add('county'EntityType::class, [
  62.                 'required' => false,
  63.                 'class' => County::class,
  64.                 'choice_label' => 'name',
  65.                 'placeholder' => 'Επιλέξτε νομό',
  66.                 'label' => '* Νομός'
  67.             ]);
  68.         }
  69.     }
  70.     /**
  71.      * Populates *county* form field according to selected country
  72.      *
  73.      * @param FormEvent $event
  74.      */
  75.     public function onPostSubmit(FormEvent $event)
  76.     {
  77.         $countryId $event->getData();
  78.         $country $this->em->getRepository(Country::class)->find($countryId);
  79.         if (null !== $country) {
  80.             $form $event->getForm()->getParent();
  81.             $form->add('county'EntityType::class, [
  82.                 'class' => County::class,
  83.                 'choice_label' => 'name',
  84.                 'placeholder' => 'Επιλέξτε νομό',
  85.                 'query_builder' => function (EntityRepository $er) use ($country) {
  86.                     return $er->createQueryBuilder('c')
  87.                         ->where('c.country = :country')
  88.                         ->setParameter('country'$country)
  89.                         ->orderBy('c.name');
  90.                 },
  91.                 'label' => '* Νομός'
  92.             ]);
  93.         }
  94.     }
  95. }