<?php
declare(strict_types=1);
namespace App\Form\EventListener\Franchise\Parameter;
use App\Entity\Admin\Locale;
use App\Entity\Franchise\Franchise;
use App\Form\FormHelper;
use App\Repository\Admin\LocaleRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
class AddLocaleFieldSubscriber extends FormHelper implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [FormEvents::PRE_SET_DATA => 'preSetData'];
}
public function preSetData(FormEvent $event): void
{
/** @var Franchise $franchise */
$franchise = $event->getData()->getFranchise();
$queryBuilder = function (LocaleRepository $repository) use ($franchise) {
return $repository->getLocalesByFranchiseQB($franchise);
};
$event->getForm()->add('locale', EntityType::class, $this->getEntityParameters(
Locale::class,
'app.global.fields.locale.default_label',
null,
true,
['query_builder' => $queryBuilder]
));
}
}