<?php
declare(strict_types=1);
namespace App\Form\EventListener\Franchise\Application;
use App\Entity\Admin\ApplicationName;
use App\Entity\Franchise\Application;
use App\Form\FormHelper;
use App\Repository\Admin\ApplicationNameRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
class AddApplicationNameFieldSubscriber extends FormHelper implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [FormEvents::PRE_SET_DATA => 'preSetData'];
}
public function preSetData(FormEvent $event): void
{
/** @var Application $application */
$application = $event->getData();
$queryBuilder = function (ApplicationNameRepository $repository) use ($application) {
$applicationName = $application?->getApplicationName() ?? null;
return $repository->getRemainingApplicationsNamesInFranchise($application->getFranchise(), $applicationName);
};
$event->getForm()->add('applicationName', EntityType::class, $this->getEntityParameters(
ApplicationName::class,
'app.manager.pages.applications.form.name.label',
self::PLACEHOLDER_CUSTOM,
true,
['query_builder' => $queryBuilder]
));
}
}