src/EventSubscriber/Form/AddReseauFieldByDotation.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\Form;
  3. use App\Entity\Reseau;
  4. use App\Entity\User;
  5. use Doctrine\ORM\EntityRepository;
  6. use Lexik\Bundle\FormFilterBundle\Filter\Form\Type\EntityFilterType;
  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 AddReseauFieldByDotation implements EventSubscriberInterface
  12. {
  13.     public static function getSubscribedEvents(): array
  14.     {
  15.         return [
  16.             FormEvents::PRE_SET_DATA => 'preSetData',
  17.             FormEvents::PRE_SUBMIT => 'preSubmit',
  18.         ];
  19.     }
  20.     public function preSetData(FormEvent $event)
  21.     {
  22.         $form $event->getForm();
  23.         $dotation $form->get('dotation')->getData();
  24.         $this->addField($form$dotation);
  25.     }
  26.     public function preSubmit(FormEvent $event)
  27.     {
  28.         $data $event->getData();
  29.         $form $event->getForm();
  30.         if (array_key_exists("dotation"$data)) {
  31.             $dotation $data["dotation"];
  32.             $this->addField($form$dotation);
  33.         }
  34.     }
  35.     private function addField(
  36.         $form,
  37.         $dotation
  38.     )
  39.     {
  40.         $form->add('reseau'EntityType::class,
  41.             array(
  42.                 'class' => Reseau::class,
  43.                 'choice_label' => 'libelle',
  44.                 'multiple' => true,
  45.                 'placeholder' => 'app.tous',
  46.                 'empty_data' => null,
  47.                 'query_builder' => function (
  48.                     EntityRepository $er
  49.                 ) use (
  50.                     $dotation
  51.                 ) {
  52.                     return $er->createQueryBuilder('r')
  53.                         ->innerJoin('r.dotations''dots')
  54.                         ->where('dots.id in (:dotation)')
  55.                         ->setParameter('dotation'$dotation)
  56.                         ->orderBy('r.libelle''ASC');
  57.                 },
  58.                 'required' => false,
  59.                 'attr' =>
  60.                     array(
  61.                         'label' => 'RĂ©seau',
  62.                     ),
  63.             )
  64.         );
  65.     }
  66. }