custom/plugins/GrimmTheme/src/Storefront/Subscriber/TopBarSubscriber.php line 55

Open in your IDE?
  1. <?php declare(strict_types 1);
  2. namespace GrimmTheme\Storefront\Subscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  5. use Shopware\Core\Content\Category\CategoryCollection;
  6. use Shopware\Core\Content\Category\Service\NavigationLoaderInterface;
  7. use Shopware\Core\Content\Category\Tree\Tree;
  8. use Shopware\Core\Content\Category\Tree\TreeItem;
  9. use Shopware\Core\System\SystemConfig\SystemConfigService;
  10. use Shopware\Core\System\Language\SalesChannel\AbstractLanguageRoute;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  14. use Shopware\Storefront\Event\RouteRequest\LanguageRouteRequestEvent;
  15. use Shopware\Core\System\Language\LanguageCollection;
  16. use Shopware\Storefront\Page\GenericPageLoadedEvent;
  17. class TopBarSubscriber implements EventSubscriberInterface {
  18.     /**
  19.      * @var SystemConfigService
  20.      */
  21.     private $systemConfigService;
  22.     /**
  23.      * @var NavigationLoaderInterface
  24.      */
  25.     private $navigationLoader;
  26.     /**
  27.      * @var AbstractLanguageRoute
  28.      */
  29.     private $languageRoute;
  30.     public function __construct(
  31.         SystemConfigService $systemConfigService,
  32.         NavigationLoaderInterface $navigationLoader,
  33.         AbstractLanguageRoute $languageRoute
  34.     ) {
  35.         $this->systemConfigService $systemConfigService;
  36.         $this->navigationLoader $navigationLoader;
  37.         $this->languageRoute $languageRoute;
  38.     }
  39.     public static function getSubscribedEvents()
  40.     {
  41.         return [
  42.             GenericPageLoadedEvent::class => 'onHeaderPageletLoadedEvent'
  43.         ];
  44.     }
  45.     public function onHeaderPageletLoadedEvent(GenericPageLoadedEvent $event)
  46.     {
  47.         $topBarParentCategoryId $this->systemConfigService->get('GrimmTheme.config.topBarLinks');
  48.         if(!$topBarParentCategoryId){
  49.             return;
  50.         }
  51.         $navigationTree $this->getTree($event->getSalesChannelContext(), $topBarParentCategoryId);
  52.         $event->getPage()->addExtension(
  53.             'grimmTopbar',
  54.             $navigationTree
  55.         );
  56.     }
  57.     private function getTree(SalesChannelContext $contextstring $topBarParentCategoryId): Tree
  58.     {
  59.         return $this->navigationLoader->load($topBarParentCategoryId$context$topBarParentCategoryId1);
  60.     }
  61. }