custom/plugins/AcrisFaqCS/src/Storefront/Subscriber/ContentSearchSubscriber.php line 46

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\Faq\Storefront\Subscriber;
  3. use Acris\Faq\Components\ContentSearch\FaqSearchService;
  4. use Acris\Faq\Components\ContentSearch\Struct\ContentSearchSynonyms;
  5. use Shopware\Core\System\SystemConfig\SystemConfigService;
  6. use Shopware\Storefront\Page\PageLoadedEvent;
  7. use Shopware\Storefront\Page\Search\SearchPageLoadedEvent;
  8. use Shopware\Storefront\Page\Suggest\SuggestPageLoadedEvent;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class ContentSearchSubscriber implements EventSubscriberInterface
  11. {
  12.     /**
  13.      * @var SystemConfigService
  14.      */
  15.     private $configService;
  16.     /**
  17.      * @var FaqSearchService
  18.      */
  19.     private FaqSearchService $faqSearchService;
  20.     public function __construct(
  21.         SystemConfigService $configService,
  22.         FaqSearchService $faqSearchService
  23.     )
  24.     {
  25.         $this->configService $configService;
  26.         $this->faqSearchService $faqSearchService;
  27.     }
  28.     public static function getSubscribedEvents()
  29.     {
  30.         return [
  31.             SuggestPageLoadedEvent::class => [
  32.                 ['onSearchPage'200]
  33.             ],
  34.             SearchPageLoadedEvent::class => [
  35.                 ['onSearchPage'200]
  36.             ]
  37.         ];
  38.     }
  39.     public function onSearchPage(PageLoadedEvent $event)
  40.     {
  41.         $page $event->getPage();
  42.         $request $event->getRequest();
  43.         $term $request->get('search');
  44.         $maxResults 16;
  45.         if ($event->getContext()->hasExtension(ContentSearchSynonyms::EXTENSION_KEY)) {
  46.             $tokens $event->getContext()->getExtension(ContentSearchSynonyms::EXTENSION_KEY)->getSynonyms();
  47.             $tokensString \implode(" "$tokens);
  48.         } else {
  49.             $tokensString $term;
  50.         }
  51.         if($this->configService->get('AcrisFaqCS.config.faq'$event->getSalesChannelContext()->getSalesChannel()->getId()) === true) {
  52.             if($event instanceof SuggestPageLoadedEvent) {
  53.                 $maxResults $this->configService->get('AcrisFaqCS.config.faqSuggestMaxResults'$event->getSalesChannelContext()->getSalesChannel()->getId());
  54.             }
  55.             $faqSearchResults $this->faqSearchService->search($tokensString$event->getSalesChannelContext(), $maxResults);
  56.             $page->addExtension('acrisFaqSearchResult'$faqSearchResults);
  57.         }
  58.     }
  59. }