custom/plugins/S360MegaMenu/src/Storefront/Subscriber/MenuOffcanvasSubscriber.php line 34

Open in your IDE?
  1. <?php
  2. declare(strict_types 1);
  3. namespace S360\MegaMenu\Storefront\Subscriber;
  4. use S360\MegaMenu\Core\Content\Menu\MenuCollection;
  5. use S360\MegaMenu\Service\MenuLoader;
  6. use S360\MegaMenu\Storefront\Pagelet\OffcanvasNavigationPagelet;
  7. use Shopware\Storefront\Pagelet\Menu\Offcanvas\MenuOffcanvasPageletLoadedEvent;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. /**
  10.  * Add mobile (sub)menu to offcanvas menu.
  11.  * @package S360\MegaMenu\Storefront\Subscriber
  12.  */
  13. class MenuOffcanvasSubscriber extends AbstractMenuSubscriber implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @inheritDoc
  17.      */
  18.     public static function getSubscribedEvents()
  19.     {
  20.         return [
  21.             MenuOffcanvasPageletLoadedEvent::class => 'onPageletLoaded'
  22.         ];
  23.     }
  24.     /**
  25.      * Add mobile (sub)menu to offcanvas pagelet.
  26.      *
  27.      * @param MenuOffcanvasPageletLoadedEvent $event
  28.      * @return void
  29.      */
  30.     public function onPageletLoaded(MenuOffcanvasPageletLoadedEvent $event): void
  31.     {
  32.         $salesChannel $event->getSalesChannelContext()->getSalesChannel();
  33.         $navigationId $event->getRequest()->get(MenuLoader::ROOT_ID_QUERY_PARAM);
  34.         $activeProp MenuLoader::ACTIVE_PROPERTY_ITEM_ID;
  35.         if (empty($navigationId)) {
  36.             $navigationId $event->getRequest()->get('navigationId'$salesChannel->getNavigationCategoryId());
  37.             $activeProp MenuLoader::ACTIVE_PROPERTY_CATEGORY_ID;
  38.         }
  39.         $menu $this->menuLoader->load($event->getContext(), $event->getRequest(), $navigationId$activeProp);
  40.         $mobile $menu->getByType(MenuCollection::MOBILE_MENU_TYPE);
  41.         $activeCat null;
  42.         // Load Active Pseudo Category
  43.         if ($mobile) {
  44.             $tree $mobile->getTree();
  45.             $activeCat $this->menuLoader->loadActiveCategory($tree$navigationId);
  46.             // Sort Children
  47.             $this->menuLoader->sortCategories($activeCat);
  48.             if ($tree->getActive() && $tree->getActive()->getCategoryLink()) {
  49.                 $this->menuLoader->sortCategories($tree->getActive()->getCategoryLink());
  50.             }
  51.         }
  52.         // Pagelet
  53.         $pagelet = new OffcanvasNavigationPagelet($menu->getByType('desktop'), $menu->getByType('mobile'), $activeCat);
  54.         $pagelet->setSalesChannelContext($event->getSalesChannelContext());
  55.         $event->getPagelet()->addExtension(
  56.             's360_megamenu',
  57.             $pagelet
  58.         );
  59.     }
  60. }