src/Form/EventListener/AddEctFieldListener.php line 32

Open in your IDE?
  1. <?php
  2. namespace App\Form\EventListener;
  3. use App\Entity\AnneeAcademique;
  4. use App\Entity\Ue;
  5. use App\Entity\UeEct;
  6. use App\Repository\UeEctRepository;
  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 AddEctFieldListener implements EventSubscriberInterface
  12. {
  13.     private UeEctRepository $ueEctRepository;
  14.     public function __construct(UeEctRepository $ueEctRepository)
  15.     {
  16.         $this->ueEctRepository $ueEctRepository;
  17.     }
  18.     public static function getSubscribedEvents(): array
  19.     {
  20.         return [
  21.             FormEvents::POST_SUBMIT => 'onPostSubmit',
  22.         ];
  23.     }
  24.     public function onPostSubmit(FormEvent $event)
  25.     {
  26.         $ects = [];
  27.         $form $event->getForm();
  28.         $parent $form->getParent();
  29.         /** @var AnneeAcademique $academique */
  30.         $academique $parent->get('academique')->getData();
  31.         /** @var Ue $ue */
  32.         $ue $parent->get('ue')->getData();
  33.         if ($academique !== null) {
  34.             $ects $this->ueEctRepository->findEctsByUe($ue$academique);
  35.         } else {
  36.             $ects $this->ueEctRepository->findEctsByUe($uenull);
  37.         }
  38.         $parent->add('ueEct'EntityType::class, [
  39.             'class' => UeEct::class,
  40.             'attr' => [
  41.                 'data-controller' => 'tom-select',
  42.                 'required' => true
  43.             ],
  44.             'label' => 'entity.ecue.long',
  45.             'placeholder' => '-- choisir une option --',
  46.             'choices' => $ects,
  47.             'auto_initialize' => false,
  48.             'choice_label' => function (UeEct $ect) {
  49.                 return $ect->getEcue();
  50.             }
  51.         ]);
  52.     }
  53. }