custom/plugins/S360MegaMenu/src/Storefront/Subscriber/HeaderSubscriber.php line 37

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace S360\MegaMenu\Storefront\Subscriber;
  3. use S360\MegaMenu\Core\Content\Menu\MenuCollection;
  4. use S360\MegaMenu\Core\Content\Menu\MenuEntity;
  5. use S360\MegaMenu\Core\Content\MenuItem\MenuItemCollection;
  6. use S360\MegaMenu\Service\MenuLoader;
  7. use S360\MegaMenu\Storefront\Pagelet\NavigationPagelet;
  8. use Shopware\Core\Content\Category\CategoryEntity;
  9. use Shopware\Storefront\Pagelet\Header\HeaderPageletLoadedEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. /**
  12.  * Adds the MegaMenus to the header Pagelet.
  13.  *
  14.  * @package S360\MegaMenu\Storefront\Subscriber
  15.  */
  16. class HeaderSubscriber extends AbstractMenuSubscriber implements EventSubscriberInterface
  17. {
  18.     /**
  19.      * @inheritDoc
  20.      */
  21.     public static function getSubscribedEvents()
  22.     {
  23.         return [
  24.             HeaderPageletLoadedEvent::class => 'onPageLoaded'
  25.         ];
  26.     }
  27.     /**
  28.      * Adds the MegaMenus to the header Pagelet.
  29.      *
  30.      * @param HeaderPageletLoadedEvent $event
  31.      * @return void
  32.      */
  33.     public function onPageLoaded(HeaderPageletLoadedEvent $event): void
  34.     {
  35.         $salesChannel $event->getSalesChannelContext()->getSalesChannel();
  36.         $navigationId $event->getRequest()->get('navigationId'$salesChannel->getNavigationCategoryId());
  37.         $menues $this->menuLoader->load(
  38.             $event->getContext(),
  39.             $event->getRequest(),
  40.             (string) $navigationId,
  41.             MenuLoader::ACTIVE_PROPERTY_CATEGORY_ID
  42.         );
  43.         // Load Pseudo-Category Content
  44.         $pseudoItem $this->loadPseudoCategoryContent(
  45.             $menues->getByType(MenuCollection::MOBILE_MENU_TYPE),
  46.             $navigationId
  47.         );
  48.         $this->menuLoader->sortCategories($pseudoItem);
  49.         $event->getPagelet()->addExtension(
  50.             's360_megamenu',
  51.             new NavigationPagelet(
  52.                 $menues->getByType(MenuCollection::DESKTOP_MENU_TYPE),
  53.                 $menues->getByType(MenuCollection::MOBILE_MENU_TYPE),
  54.                 $pseudoItem,
  55.                 $this->loadActiveCategory($menues->getByType(MenuCollection::MOBILE_MENU_TYPE), $navigationId)
  56.             )
  57.         );
  58.     }
  59.     /**
  60.      * Load the active category
  61.      *
  62.      * @param MenuEntity|null $menu
  63.      * @param string $navigationId
  64.      * @return CategoryEntity|null
  65.      */
  66.     private function loadActiveCategory(?MenuEntity $menustring $navigationId): ?CategoryEntity
  67.     {
  68.         if ($menu) {
  69.             return $this->menuLoader->loadActiveCategory($menu->getTree(), $navigationId);
  70.         }
  71.         return null;
  72.     }
  73.     /**
  74.      * Load Pseudo Category Content, ie the active category entity.
  75.      *
  76.      * @param MenuEntity|null $menu
  77.      * @param string $navigationId
  78.      * @return CategoryEntity|null
  79.      */
  80.     private function loadPseudoCategoryContent(?MenuEntity $menustring $navigationId): ?CategoryEntity
  81.     {
  82.         if (empty($menu)) {
  83.             return null;
  84.         }
  85.         $activeCategory $this->loadActiveCategory($menu$navigationId);
  86.         // Get the active category parent if the active category is a leaf node (ie has no children),
  87.         // as then we want to display the active cats siblings instead!
  88.         if ($activeCategory && empty($activeCategory->getChildren())) {
  89.             return $this->getPseudoCategoryParent($activeCategory$menu->getItems()) ?? $activeCategory;
  90.         }
  91.         return $activeCategory;
  92.     }
  93.     /**
  94.      * Get Category Parent
  95.      *
  96.      * @param CategoryEntity $category
  97.      * @param MenuItemCollection|null $items
  98.      * @return CategoryEntity|null
  99.      */
  100.     private function getPseudoCategoryParent(CategoryEntity $category, ?MenuItemCollection $items): ?CategoryEntity
  101.     {
  102.         if (empty($items)) {
  103.             return null;
  104.         }
  105.         $parentId $category->getParentId();
  106.         foreach ($items as $item) {
  107.             if ($item->getCategoryLinkId() == $parentId) {
  108.                 return $item->getCategoryLink();
  109.             }
  110.             if ($item->getCategoryLink() && $item->getCategoryLink()->getChildren()) {
  111.                 $parent $this->menuLoader->getDeepCategory($item->getCategoryLink(), $parentId);
  112.                 if ($parent) {
  113.                     return $parent;
  114.                 }
  115.             }
  116.         }
  117.         return null;
  118.     }
  119. }