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