src/Form/EventListener/Franchise/AddFranchiseFieldSubscriber.php line 23

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Form\EventListener\Franchise;
  4. use App\Entity\Franchise\Franchise;
  5. use App\Entity\Shop\Shop;
  6. use App\Form\FormHelper;
  7. use App\Form\Types\FranchiseType;
  8. use App\Repository\Franchise\FranchiseRepository;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\Form\FormEvent;
  11. use Symfony\Component\Form\FormEvents;
  12. class AddFranchiseFieldSubscriber 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.         $entity $event->getData();
  21.         if ($entity instanceof Shop && null !== $entity->getFranchise()) {
  22.             return;
  23.         }
  24.         $queryBuilder = function (FranchiseRepository $repository) {
  25.             return $repository->getMultipleFranchisesQB();
  26.         };
  27.         $event->getForm()->add('franchise'FranchiseType::class, $this->getEntityParameters(
  28.             Franchise::class,
  29.             'app.admin.pages.shops.form.franchise.franchise',
  30.             null,
  31.             true,
  32.             ['query_builder' => $queryBuilder'multiple' => false]
  33.         ));
  34.     }
  35. }