custom/plugins/EWHeaderFooter/src/Subscriber/NavigationLoadedSubscriber.php line 62

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace EWHeaderFooter\Subscriber;
  3. use Shopware\Core\Content\Category\CategoryCollection;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  9. use Shopware\Core\System\SystemConfig\SystemConfigService;
  10. use Shopware\Storefront\Page\Navigation\NavigationPageLoadedEvent;
  11. use Shopware\Storefront\Page\PageLoadedEvent;
  12. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class NavigationLoadedSubscriber implements EventSubscriberInterface
  15. {
  16.     public EntityRepository $categoryRepository;
  17.     public SystemConfigService $systemConfigService;
  18.     public function __construct(
  19.         EntityRepository $categoryRepository,
  20.         SystemConfigService $systemConfigService)
  21.     {
  22.         $this->categoryRepository $categoryRepository;
  23.         $this->systemConfigService $systemConfigService;
  24.     }
  25.     public static function getSubscribedEvents()
  26.     {
  27.         return [
  28.             NavigationPageLoadedEvent::class => 'onNavigationPageLoaded',
  29.             ProductPageLoadedEvent::class => 'onProductPageLoaded',
  30.         ];
  31.     }
  32.     public function onNavigationPageLoaded(PageLoadedEvent $event)
  33.     {
  34.         $minimalFooterCategoryId $this->systemConfigService->get('EWHeaderFooter.config.minimalFooterCategoryId');
  35.         if ($minimalFooterCategoryId) {
  36.             $minimalFooterCategory $this->getCategory($minimalFooterCategoryId$event->getContext());
  37.             $event->getPage()->getHeader()->getNavigation()->getActive()->setExtensions(['minimalFooterNavigation' => $minimalFooterCategory->getElements()]);
  38.         }
  39.     }
  40.     private function getCategory(string $categoryIdContext $context): CategoryCollection
  41.     {
  42.         $criteria = new Criteria();
  43.         $criteria->addAssociation('translation');
  44.         $criteria->addFilter(new EqualsFilter('parentId'$categoryId));
  45.         $criteria->addSorting(new FieldSorting('autoIncrement'));
  46.         $criteria->addAssociation("media");
  47.         /** @var CategoryCollection $category */
  48.         $category $this->categoryRepository->search($criteria$context)->getEntities();
  49.         return $category;
  50.     }
  51.     public function onProductPageLoaded($event)
  52.     {
  53.         $minimalFooterCategoryId $this->systemConfigService->get('EWHeaderFooter.config.minimalFooterCategoryId');
  54.         if ($minimalFooterCategoryId) {
  55.             $minimalFooterCategory $this->getCategory($minimalFooterCategoryId$event->getContext());
  56.             $event->getPage()->getHeader()->getNavigation()->getActive()->setExtensions(['minimalFooterNavigation' => $minimalFooterCategory->getElements()]);
  57.         }
  58.     }
  59. }