vendor/bitbag/elasticsearch-plugin/src/Block/SearchFormEventListener.php line 43

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file has been created by developers from BitBag.
  4.  * Feel free to contact us once you face any issues or want to start
  5.  * another great project.
  6.  * You can find more information about us on https://bitbag.io and write us
  7.  * an email on hello@bitbag.io.
  8.  */
  9. declare(strict_types=1);
  10. namespace BitBag\SyliusElasticsearchPlugin\Block;
  11. use BitBag\SyliusElasticsearchPlugin\Form\Type\SearchType;
  12. use BitBag\SyliusElasticsearchPlugin\Model\Search;
  13. use Sonata\BlockBundle\Event\BlockEvent;
  14. use Sonata\BlockBundle\Model\Block;
  15. use Symfony\Component\Form\FormFactoryInterface;
  16. use Symfony\Component\Form\FormInterface;
  17. use Symfony\Component\Routing\RouterInterface;
  18. final class SearchFormEventListener
  19. {
  20.     private string $template;
  21.     private FormFactoryInterface $formFactory;
  22.     private RouterInterface $router;
  23.     private ?FormInterface $form null;
  24.     public function __construct(
  25.         string $template,
  26.         FormFactoryInterface $formFactory,
  27.         RouterInterface $router
  28.     ) {
  29.         $this->template $template;
  30.         $this->formFactory $formFactory;
  31.         $this->router $router;
  32.     }
  33.     public function onBlockEvent(BlockEvent $event): void
  34.     {
  35.         $block = new Block();
  36.         $block->setId(uniqid(''true));
  37.         $block->setSettings(
  38.             array_replace(
  39.                 $event->getSettings(),
  40.                 [
  41.                     'template' => $this->template,
  42.                     'form' => $this->getForm()->createView(),
  43.                 ]
  44.             )
  45.         );
  46.         $block->setType('sonata.block.service.template');
  47.         $event->addBlock($block);
  48.     }
  49.     public function getForm(Search $search null): FormInterface
  50.     {
  51.         if (!$this->form) {
  52.             if (!$search) {
  53.                 $search = new Search();
  54.             }
  55.             $this->form $this->formFactory
  56.                 ->create(
  57.                     SearchType::class,
  58.                     $search,
  59.                     ['action' => $this->router->generate('bitbag_sylius_elasticsearch_plugin_shop_search')]
  60.                 );
  61.         }
  62.         return $this->form;
  63.     }
  64. }