src/Form/UserManagement/EventSubscriber/AddRolesFieldSubscriber.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 AddRolesFieldSubscriber implements EventSubscriberInterface
  9. {
  10.     public static function getSubscribedEvents()
  11.     {
  12.         return [FormEvents::PRE_SET_DATA => 'onRolesSelected'];
  13.     }
  14.     public function onRolesSelected(FormEvent $event): void
  15.     {
  16.         $form $event->getForm();
  17.         $data $event->getData();
  18.         $roles $data->getRoles();
  19.         if (\in_array('ROLE_CLIENT'$roles)) {
  20.             $form->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. }