vendor/shopware/core/Content/Category/Service/NavigationLoader.php line 75

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Category\Service;
  3. use Shopware\Core\Content\Category\CategoryCollection;
  4. use Shopware\Core\Content\Category\CategoryEntity;
  5. use Shopware\Core\Content\Category\Event\NavigationLoadedEvent;
  6. use Shopware\Core\Content\Category\Exception\CategoryNotFoundException;
  7. use Shopware\Core\Content\Category\SalesChannel\AbstractNavigationRoute;
  8. use Shopware\Core\Content\Category\Tree\Tree;
  9. use Shopware\Core\Content\Category\Tree\TreeItem;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Util\AfterSort;
  12. use Shopware\Core\Framework\Log\Package;
  13. use Shopware\Core\System\Annotation\Concept\ExtensionPattern\Decoratable;
  14. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  17. /**
  18.  * @Decoratable()
  19.  */
  20. #[Package('content')]
  21. class NavigationLoader implements NavigationLoaderInterface
  22. {
  23.     /**
  24.      * @var TreeItem
  25.      */
  26.     private $treeItem;
  27.     /**
  28.      * @var EventDispatcherInterface
  29.      */
  30.     private $eventDispatcher;
  31.     /**
  32.      * @var AbstractNavigationRoute
  33.      */
  34.     private $navigationRoute;
  35.     /**
  36.      * @internal
  37.      */
  38.     public function __construct(
  39.         EventDispatcherInterface $eventDispatcher,
  40.         AbstractNavigationRoute $navigationRoute
  41.     ) {
  42.         $this->treeItem = new TreeItem(null, []);
  43.         $this->eventDispatcher $eventDispatcher;
  44.         $this->navigationRoute $navigationRoute;
  45.     }
  46.     /**
  47.      * {@inheritdoc}
  48.      *
  49.      * @throws CategoryNotFoundException
  50.      */
  51.     public function load(string $activeIdSalesChannelContext $contextstring $rootIdint $depth 2): Tree
  52.     {
  53.         $request = new Request();
  54.         $request->query->set('buildTree''false');
  55.         $request->query->set('depth', (string) $depth);
  56.         $criteria = new Criteria();
  57.         $criteria->setTitle('header::navigation');
  58.         $categories $this->navigationRoute
  59.             ->load($activeId$rootId$request$context$criteria)
  60.             ->getCategories();
  61.         $navigation $this->getTree($rootId$categories$categories->get($activeId));
  62.         $event = new NavigationLoadedEvent($navigation$context);
  63.         $this->eventDispatcher->dispatch($event);
  64.         return $event->getNavigation();
  65.     }
  66.     private function getTree(?string $rootIdCategoryCollection $categories, ?CategoryEntity $active): Tree
  67.     {
  68.         $parents = [];
  69.         $items = [];
  70.         foreach ($categories as $category) {
  71.             $item = clone $this->treeItem;
  72.             $item->setCategory($category);
  73.             $parents[$category->getParentId()][$category->getId()] = $item;
  74.             $items[$category->getId()] = $item;
  75.         }
  76.         foreach ($parents as $parentId => $children) {
  77.             if (empty($parentId)) {
  78.                 continue;
  79.             }
  80.             $sorted AfterSort::sort($children);
  81.             $filtered \array_filter($sorted, static function (TreeItem $filter) {
  82.                 return $filter->getCategory()->getActive() && $filter->getCategory()->getVisible();
  83.             });
  84.             if (!isset($items[$parentId])) {
  85.                 continue;
  86.             }
  87.             $item $items[$parentId];
  88.             $item->setChildren($filtered);
  89.         }
  90.         $root $parents[$rootId] ?? [];
  91.         $root AfterSort::sort($root);
  92.         $filtered = [];
  93.         /** @var TreeItem $item */
  94.         foreach ($root as $key => $item) {
  95.             if (!$item->getCategory()->getActive() || !$item->getCategory()->getVisible()) {
  96.                 continue;
  97.             }
  98.             $filtered[$key] = $item;
  99.         }
  100.         return new Tree($active$filtered);
  101.     }
  102. }