<?php
declare(strict_types=1);
namespace App\Form\EventListener\Search\Order;
use App\Entity\Order\CheckoutInterface;
use App\Form\FormHelper;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
class AddStatusSubscriber extends FormHelper implements EventSubscriberInterface
{
private ?Request $request;
public function __construct(
RequestStack $requestStack,
) {
$this->request = $requestStack->getCurrentRequest();
}
public static function getSubscribedEvents(): array
{
return [FormEvents::PRE_SET_DATA => 'preSetData'];
}
public function preSetData(FormEvent $event): void
{
$searchForm = $this->request->query->get('search_form');
if (null === $searchForm || '2' !== $searchForm['type']) {
return;
}
$choices = [];
foreach (CheckoutInterface::STATES as $state) {
$choices['app.order.state.' . $state] = $state;
}
$event->getForm()->add('status', ChoiceType::class, $this->getChoiceParameters(
'app.global.search.label.status',
self::PLACEHOLDER_NONE,
false,
[
'choices' => $choices,
'multiple' => true,
]
));
}
}