src/Form/EventListener/Franchise/Social/AddSocialNameFieldSubscriber.php line 23

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Form\EventListener\Franchise\Social;
  4. use App\Entity\Admin\SocialName;
  5. use App\Entity\Franchise\Social;
  6. use App\Form\FormHelper;
  7. use App\Repository\Admin\SocialNameRepository;
  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 AddSocialNameFieldSubscriber 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 Social $social */
  21.         $social $event->getData();
  22.         $queryBuilder = function (SocialNameRepository $repository) use ($social) {
  23.             $socialName $social?->getSocialName() ?? null;
  24.             return $repository->getRemainingSocialsNamesInFranchise($social->getFranchise(), $socialName);
  25.         };
  26.         $event->getForm()->add('socialName'EntityType::class, $this->getEntityParameters(
  27.             SocialName::class,
  28.             'app.manager.pages.socials.label.name',
  29.             self::PLACEHOLDER_CUSTOM,
  30.             true,
  31.             ['query_builder' => $queryBuilder]
  32.         ));
  33.     }
  34. }