src/EventListener/AddDiffusionFieldListener.php line 58

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: frup64362
  5.  * Date: 09/08/2016
  6.  * Time: 09:39
  7.  */
  8. namespace App\EventListener;
  9. use Doctrine\ORM\EntityRepository;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\Form\FormEvent;
  12. use Symfony\Component\Form\FormEvents;
  13. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  14. use Symfony\Component\PropertyAccess\PropertyAccess;
  15. class AddDiffusionFieldListener implements EventSubscriberInterface
  16. {
  17.     public function __construct(private readonly string $propertyPathToAction "")
  18.     {
  19.     }
  20.     public static function getSubscribedEvents()
  21.     {
  22.         return [FormEvents::PRE_SET_DATA => 'preSetData'FormEvents::PRE_SUBMIT => 'preSubmit'];
  23.     }
  24.     private function addSousActionForm($form$action_id)
  25.     {
  26.         $formOptions = ['class' => \App\Entity\RefSousAction::class, 'multiple' => true'required' => false'empty_data' => null'query_builder' => fn(EntityRepository $er) => $er->createQueryBuilder('u')
  27.             ->innerJoin('u.idAction''id_action')
  28.             ->where('id_action.idAction IN (:idAction)')
  29.             ->setParameter(':idAction'$action_id)
  30.             ->orderBy('u.idSousAction''ASC'), 'choice_label' => 'libelleSousActionLong'];
  31.         $form->add('SousActions'EntityType::class, $formOptions);
  32.     }
  33.     public function preSetData(FormEvent $event)
  34.     {
  35.         $data $event->getData();
  36.         $form $event->getForm();
  37.         if (null === $data) {
  38.             return;
  39.         }
  40.         $accessor PropertyAccess::createPropertyAccessor();
  41.         $action $accessor->getValue($data$this->propertyPathToAction);
  42.         $action_id = ($action) ? $action->getIdAction() : null;
  43.         $this->addSousActionForm($form$action_id);
  44.     }
  45.     public function preSubmit(FormEvent $event)
  46.     {
  47.         $data $event->getData();
  48.         $form $event->getForm();
  49.         $idActon $data['Action'];
  50.         if ($idActon)
  51.             $this->addSousActionForm($form$idActon);
  52.     }
  53. }