vendor/sulu/form-bundle/Event/RequestListener.php line 61

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Sulu.
  4.  *
  5.  * (c) Sulu GmbH
  6.  *
  7.  * This source file is subject to the MIT license that is bundled
  8.  * with this source code in the file LICENSE.
  9.  */
  10. namespace Sulu\Bundle\FormBundle\Event;
  11. use Sulu\Bundle\FormBundle\Configuration\FormConfigurationFactory;
  12. use Sulu\Bundle\FormBundle\Entity\Dynamic;
  13. use Sulu\Bundle\FormBundle\Form\BuilderInterface;
  14. use Sulu\Bundle\FormBundle\Form\HandlerInterface;
  15. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  16. use Symfony\Component\HttpFoundation\RedirectResponse;
  17. use Symfony\Component\HttpKernel\Event\RequestEvent;
  18. class RequestListener
  19. {
  20.     /**
  21.      * @var BuilderInterface
  22.      */
  23.     protected $formBuilder;
  24.     /**
  25.      * @var HandlerInterface
  26.      */
  27.     protected $formHandler;
  28.     /**
  29.      * @var FormConfigurationFactory
  30.      */
  31.     protected $formConfigurationFactory;
  32.     /**
  33.      * @var EventDispatcherInterface
  34.      */
  35.     protected $eventDispatcher;
  36.     /**
  37.      * RequestListener constructor.
  38.      */
  39.     public function __construct(
  40.         BuilderInterface $formBuilder,
  41.         HandlerInterface $formHandler,
  42.         FormConfigurationFactory $formConfigurationFactory,
  43.         EventDispatcherInterface $eventDispatcher
  44.     ) {
  45.         $this->formBuilder $formBuilder;
  46.         $this->formHandler $formHandler;
  47.         $this->formConfigurationFactory $formConfigurationFactory;
  48.         $this->eventDispatcher $eventDispatcher;
  49.     }
  50.     public function onKernelRequest(RequestEvent $event): void
  51.     {
  52.         if (!$event->isMasterRequest()) {
  53.             // do nothing if it's not the master request
  54.             return;
  55.         }
  56.         $request $event->getRequest();
  57.         if (!$request->isMethod('post')) {
  58.             // do nothing if it's not a post request
  59.             return;
  60.         }
  61.         try {
  62.             $form $this->formBuilder->buildByRequest($request);
  63.             if (!$form || !$form->isSubmitted() || !$form->isValid()) {
  64.                 // do nothing when no form was found or not valid
  65.                 return;
  66.             }
  67.         } catch (\Exception $e) {
  68.             // Catch all exception on build form by request
  69.             return;
  70.         }
  71.         /** @var Dynamic $dynamic */
  72.         $dynamic $form->getData();
  73.         $configuration $this->formConfigurationFactory->buildByDynamic($dynamic);
  74.         $dynamic->setLocale($request->getLocale()); // Need to be set to request locale for shadow pages, configuraiton will hold the original locale
  75.         if ($this->formHandler->handle($form$configuration)) {
  76.             $serializedObject $dynamic->getForm()->serializeForLocale($dynamic->getLocale(), $dynamic);
  77.             $dynFormSavedEvent = new DynFormSavedEvent($serializedObject$dynamic);
  78.             $this->eventDispatcher->dispatch($dynFormSavedEventDynFormSavedEvent::NAME);
  79.             $response = new RedirectResponse('?send=true');
  80.             $event->setResponse($response);
  81.         }
  82.     }
  83. }