<?php
declare(strict_types=1);
namespace App\Form\EventListener\Admin\Locale;
use App\Entity\Admin\Locale;
use App\Entity\Admin\LocaleInterface;
use App\Form\FormHelper;
use App\Repository\Admin\LocaleRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
class AddNameFieldSubscriber extends FormHelper implements EventSubscriberInterface
{
public function __construct(
private LocaleRepository $localeRepository
) {
}
public static function getSubscribedEvents(): array
{
return [FormEvents::PRE_SET_DATA => 'preSetData'];
}
public function preSetData(FormEvent $event): void
{
/** @var Locale $locale */
$locale = $event->getData();
$locales = array_diff(LocaleInterface::LOCALES, $this->localeRepository->findCodesExcept($locale->getCode()));
$event->getForm()->add('name', ChoiceType::class, $this->getChoiceParameters('app.global.fields.name', self::PLACEHOLDER_SAME_AS_LABEL, true, [
'choices' => $locales,
'data' => $locale->getCode(),
]));
}
}