src/Form/UserManagement/EventSubscriber/UpdateRolesFieldSubscriber.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Form\UserManagement\EventSubscriber;
  3. use Doctrine\ORM\EntityRepository;
  4. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\Form\FormEvent;
  7. use Symfony\Component\Form\FormEvents;
  8. class UpdateRolesFieldSubscriber implements EventSubscriberInterface
  9. {
  10.     public static function getSubscribedEvents()
  11.     {
  12.         return [FormEvents::POST_SUBMIT => 'onRolesChanged'];
  13.     }
  14.     public function onRolesChanged(FormEvent $event): void
  15.     {
  16.         $form $event->getForm();
  17.         if ($form->getData()) {
  18.             $roles $form->getData();
  19.             if (\in_array('ROLE_CLIENT'$roles)) {
  20.                 $form->getParent()->add('clients'EntityType::class, [
  21.                     'label' => 'user.client',
  22.                     'class' => 'App\Entity\UserManagement\Client',
  23.                     'query_builder' => function (EntityRepository $er) {
  24.                         return $er->createQueryBuilder('c')
  25.                             ->where('c.isValid = true')
  26.                             ->orderBy('c.name''ASC');
  27.                     },
  28.                     'attr' => ['class' => 'js-select2'],
  29.                     'multiple' => true,
  30.                     'required' => true,
  31.                 ])                     
  32.                 ; 
  33.             }
  34.         }
  35.     }
  36. }