custom/static-plugins/NdTheme/src/Subscriber/AddMenuData.php line 36

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Nd\Theme\Subscriber;
  3. use Shopware\Core\Checkout\Cart\Event\AfterLineItemAddedEvent;
  4. use Shopware\Core\Content\Cms\SalesChannel\Struct\TextStruct;
  5. use Shopware\Core\Framework\Context;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  8. use Shopware\Core\Framework\Struct\ArrayStruct;
  9. use Shopware\Core\Framework\Uuid\Uuid;
  10. use Shopware\Storefront\Pagelet\Header\HeaderPageletLoadedEvent;
  11. use Shopware\Storefront\Pagelet\Menu\Offcanvas\MenuOffcanvasPageletLoadedEvent;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Shopware\Core\Checkout\Cart\Event\BeforeLineItemAddedEvent;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  15. class AddMenuData implements EventSubscriberInterface
  16. {
  17.     public function __construct(EntityRepository $categoryRepository, array $additionalMenus)
  18.     {
  19.         $this->categoryRepository $categoryRepository;
  20.         $this->additionalMenus $additionalMenus;
  21.     }
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         // Return the events to listen to as array like this:  <event to listen to> => <method to execute>
  25.         return [
  26.             HeaderPageletLoadedEvent::class => 'addData',
  27.             MenuOffcanvasPageletLoadedEvent::class => 'addDataOffcanvas'
  28.         ];
  29.     }
  30.     public function addData(HeaderPageletLoadedEvent $event)
  31.     {
  32.         if(!$this->additionalMenus){
  33.             return;
  34.         }
  35.         $pagelet $event->getPagelet();
  36.         $additionalMenus $this->loadAdditionalMenus($event->getContext());
  37.         $pagelet->addExtension('additional_menus'$additionalMenus);
  38.     }
  39.     public function addDataOffcanvas(MenuOffcanvasPageletLoadedEvent $event)
  40.     {
  41.         if (!$this->additionalMenus) {
  42.             return;
  43.         }
  44.         $pagelet $event->getPagelet();
  45.         $additionalMenus $this->loadAdditionalMenus($event->getContext());
  46.         $pagelet->addExtension('additional_menus'$additionalMenus);
  47.     }
  48.     public function getMenu($id$context)
  49.     {
  50.         $criteria = new Criteria();
  51.         $criteria->addAssociation('seoUrls.urls');
  52.         $criteria->addFilter(new EqualsFilter('active'true));
  53.         $criteria->addFilter(new EqualsFilter('parentId'$id));
  54.         $categories $this->categoryRepository->search($criteria$context);
  55.         if ($categories->count() == 0) {
  56.             return [];
  57.         }
  58.         return $categories->getEntities()->sortByPosition();
  59.     }
  60.     private function loadAdditionalMenus(Context $context): ArrayStruct
  61.     {
  62.         $additionalMenus = new ArrayStruct();
  63.         foreach($this->additionalMenus as $topMenuKey => $topMenuId){
  64.             $additionalMenus->set($topMenuKey$this->getMenu($topMenuId$context));
  65.         }
  66.         return $additionalMenus;
  67.     }
  68. }