custom/plugins/ChespackTheme/src/Service/AddDataToPage.php line 43

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace ChespackTheme\Service;
  3. use Shopware\Core\Content\Category\CategoryCollection;
  4. use Shopware\Core\Content\Category\Service\NavigationLoaderInterface;
  5. use Shopware\Core\Content\Category\Tree\TreeItem;
  6. use Shopware\Core\Framework\Struct\ArrayEntity;
  7. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  8. use Shopware\Core\System\SystemConfig\SystemConfigService;
  9. use Shopware\Storefront\Event\StorefrontRenderEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. /**
  12.  * Add additional data to the page
  13.  */
  14. class AddDataToPage implements EventSubscriberInterface
  15. {
  16.     protected SystemConfigService $systemConfig;
  17.     private NavigationLoaderInterface $navigationLoader;
  18.     public function __construct(
  19.         SystemConfigService $systemConfig,
  20.         NavigationLoaderInterface $navigationLoader
  21.     ) {
  22.         $this->systemConfig $systemConfig;
  23.         $this->navigationLoader $navigationLoader;
  24.     }
  25.     public static function getSubscribedEvents(): array
  26.     {
  27.         return [
  28.             StorefrontRenderEvent::class => 'addContentCategories'
  29.         ];
  30.     }
  31.     /**
  32.      * Add content menu
  33.      *
  34.      * @param StorefrontRenderEvent $event
  35.      */
  36.     public function addContentCategories(StorefrontRenderEvent $event): void
  37.     {
  38.         $context $event->getSalesChannelContext();
  39.         $entity = new ArrayEntity($this->getContentMenu($context));
  40.         $context->addExtension('content_categories'$entity);
  41.     }
  42.     /**
  43.      * Get content menu by category ID
  44.      *
  45.      * @param SalesChannelContext $context
  46.      * @return array
  47.      */
  48.     private function getContentMenu(SalesChannelContext $context): array
  49.     {
  50.         $contentMenu = [];
  51.         $categoryId $this->systemConfig->get('ChespackTheme.config.contentCategory'$context->getSalesChannelId());
  52.         if ($categoryId === null) {
  53.             return [];
  54.         }
  55.         $navigation $this->navigationLoader->load($categoryId$context$categoryId4);
  56.         $mainCats = new CategoryCollection(array_map(static function (TreeItem $treeItem) {
  57.             return $treeItem->getCategory();
  58.         }, $navigation->getTree()));
  59.         foreach ($mainCats as $mainCat) {
  60.             $mainCatNav $this->navigationLoader->load($mainCat->get('id'), $context$mainCat->get('id'), 4);
  61.             $mainCatChildren = new CategoryCollection(array_map(static function (TreeItem $treeItem) {
  62.                 return $treeItem->getCategory();
  63.             }, $mainCatNav->getTree()));
  64.             $mainCat->children $mainCatChildren;
  65.             array_push($contentMenu$mainCat);
  66.         }
  67.         return $contentMenu;
  68.     }
  69. }