src/Form/EventListener/Franchise/AddDomainFieldSubscriber.php line 22

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Form\EventListener\Franchise;
  4. use App\Entity\Franchise\Domain;
  5. use App\Entity\Franchise\Franchise;
  6. use App\Form\Franchise\Domain\DomainType;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\Form\Extension\Core\Type\CollectionType;
  9. use Symfony\Component\Form\FormEvent;
  10. use Symfony\Component\Form\FormEvents;
  11. class AddDomainFieldSubscriber implements EventSubscriberInterface
  12. {
  13.     public static function getSubscribedEvents(): array
  14.     {
  15.         return [FormEvents::PRE_SET_DATA => 'preSetData'];
  16.     }
  17.     public function preSetData(FormEvent $event): void
  18.     {
  19.         /** @var Franchise $franchise */
  20.         $franchise $event->getData();
  21.         if (null !== $franchise && null !== $franchise->getId()) {
  22.             return;
  23.         }
  24.         if (=== $franchise->getDomains()->count()) {
  25.             $domain = new Domain();
  26.             $franchise->addDomain($domain);
  27.         }
  28.         $event->getForm()->add('domains'CollectionType::class, [
  29.             'entry_type' => DomainType::class,
  30.             'entry_options' => ['label' => false],
  31.         ]);
  32.     }
  33. }