src/Form/EventListener/User/AddGenderFieldSubscriber.php line 22

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Form\EventListener\User;
  4. use App\Entity\Security\Customer;
  5. use App\Entity\Security\UserInterface;
  6. use App\Form\FormHelper;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  9. use Symfony\Component\Form\FormEvent;
  10. use Symfony\Component\Form\FormEvents;
  11. class AddGenderFieldSubscriber extends FormHelper implements EventSubscriberInterface
  12. {
  13.     public static function getSubscribedEvents(): array
  14.     {
  15.         return [FormEvents::PRE_SET_DATA => 'preSetData'];
  16.     }
  17.     public function preSetData(FormEvent $event): void
  18.     {
  19.         $entity $event->getData();
  20.         if (false === $entity instanceof Customer) {
  21.             return;
  22.         }
  23.         $choices = [];
  24.         foreach (UserInterface::GENDERS as $gender) {
  25.             $choices['app.global.user.form.gender.' $gender] = $gender;
  26.         }
  27.         $event->getForm()->add('gender'ChoiceType::class, $this->getChoiceParameters('app.global.user.form.gender.label'self::PLACEHOLDER_CUSTOMtruecompact('choices')));
  28.     }
  29. }