src/Form/EventListener/Franchise/Application/AddApplicationNameFieldSubscriber.php line 23

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Form\EventListener\Franchise\Application;
  4. use App\Entity\Admin\ApplicationName;
  5. use App\Entity\Franchise\Application;
  6. use App\Form\FormHelper;
  7. use App\Repository\Admin\ApplicationNameRepository;
  8. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\Form\FormEvent;
  11. use Symfony\Component\Form\FormEvents;
  12. class AddApplicationNameFieldSubscriber extends FormHelper implements EventSubscriberInterface
  13. {
  14.     public static function getSubscribedEvents(): array
  15.     {
  16.         return [FormEvents::PRE_SET_DATA => 'preSetData'];
  17.     }
  18.     public function preSetData(FormEvent $event): void
  19.     {
  20.         /** @var Application $application */
  21.         $application $event->getData();
  22.         $queryBuilder = function (ApplicationNameRepository $repository) use ($application) {
  23.             $applicationName $application?->getApplicationName() ?? null;
  24.             return $repository->getRemainingApplicationsNamesInFranchise($application->getFranchise(), $applicationName);
  25.         };
  26.         $event->getForm()->add('applicationName'EntityType::class, $this->getEntityParameters(
  27.             ApplicationName::class,
  28.             'app.manager.pages.applications.form.name.label',
  29.             self::PLACEHOLDER_CUSTOM,
  30.             true,
  31.             ['query_builder' => $queryBuilder]
  32.         ));
  33.     }
  34. }