<?php
declare(strict_types=1);
namespace App\Form\EventListener\User;
use App\Entity\Security\Customer;
use App\Entity\Security\UserInterface;
use App\Form\FormHelper;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
class AddGenderFieldSubscriber extends FormHelper implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [FormEvents::PRE_SET_DATA => 'preSetData'];
}
public function preSetData(FormEvent $event): void
{
$entity = $event->getData();
if (false === $entity instanceof Customer) {
return;
}
$choices = [];
foreach (UserInterface::GENDERS as $gender) {
$choices['app.global.user.form.gender.' . $gender] = $gender;
}
$event->getForm()->add('gender', ChoiceType::class, $this->getChoiceParameters('app.global.user.form.gender.label', self::PLACEHOLDER_CUSTOM, true, compact('choices')));
}
}