vendor/shopware/core/Content/Category/SalesChannel/TreeBuildingNavigationRoute.php line 42

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Category\SalesChannel;
  3. use Shopware\Core\Content\Category\CategoryCollection;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  5. use Shopware\Core\Framework\Log\Package;
  6. use Shopware\Core\Framework\Routing\Annotation\Entity;
  7. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  8. use Shopware\Core\Framework\Routing\Annotation\Since;
  9. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  10. use Shopware\Core\System\SalesChannel\SalesChannelEntity;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. /**
  14.  * @Route(defaults={"_routeScope"={"store-api"}})
  15.  */
  16. #[Package('content')]
  17. class TreeBuildingNavigationRoute extends AbstractNavigationRoute
  18. {
  19.     private AbstractNavigationRoute $decorated;
  20.     /**
  21.      * @internal
  22.      */
  23.     public function __construct(AbstractNavigationRoute $decorated)
  24.     {
  25.         $this->decorated $decorated;
  26.     }
  27.     public function getDecorated(): AbstractNavigationRoute
  28.     {
  29.         return $this->decorated;
  30.     }
  31.     /**
  32.      * @Since("6.2.0.0")
  33.      * @Entity("category")
  34.      * @Route("/store-api/navigation/{activeId}/{rootId}", name="store-api.navigation", methods={"GET", "POST"})
  35.      */
  36.     public function load(string $activeIdstring $rootIdRequest $requestSalesChannelContext $contextCriteria $criteria): NavigationRouteResponse
  37.     {
  38.         $activeId $this->resolveAliasId($activeId$context->getSalesChannel());
  39.         $rootId $this->resolveAliasId($rootId$context->getSalesChannel());
  40.         $response $this->getDecorated()->load($activeId$rootId$request$context$criteria);
  41.         $buildTree $request->query->getBoolean('buildTree'$request->request->getBoolean('buildTree'true));
  42.         if (!$buildTree) {
  43.             return $response;
  44.         }
  45.         $categories $this->buildTree($rootId$response->getCategories()->getElements());
  46.         return new NavigationRouteResponse($categories);
  47.     }
  48.     private function buildTree(?string $parentId, array $categories): CategoryCollection
  49.     {
  50.         $children = new CategoryCollection();
  51.         foreach ($categories as $key => $category) {
  52.             if ($category->getParentId() !== $parentId) {
  53.                 continue;
  54.             }
  55.             unset($categories[$key]);
  56.             $children->add($category);
  57.         }
  58.         $children->sortByPosition();
  59.         $items = new CategoryCollection();
  60.         foreach ($children as $child) {
  61.             if (!$child->getActive() || !$child->getVisible()) {
  62.                 continue;
  63.             }
  64.             $child->setChildren($this->buildTree($child->getId(), $categories));
  65.             $items->add($child);
  66.         }
  67.         return $items;
  68.     }
  69.     private function resolveAliasId(string $idSalesChannelEntity $salesChannelEntity): string
  70.     {
  71.         switch ($id) {
  72.             case 'main-navigation':
  73.                 return $salesChannelEntity->getNavigationCategoryId();
  74.             case 'service-navigation':
  75.                 if ($salesChannelEntity->getServiceCategoryId() === null) {
  76.                     throw new \RuntimeException(\sprintf('Service category, for sales channel %s, is not set'$salesChannelEntity->getTranslation('name')));
  77.                 }
  78.                 return $salesChannelEntity->getServiceCategoryId();
  79.             case 'footer-navigation':
  80.                 if ($salesChannelEntity->getFooterCategoryId() === null) {
  81.                     throw new \RuntimeException(\sprintf('Footer category, for sales channel %s, is not set'$salesChannelEntity->getTranslation('name')));
  82.                 }
  83.                 return $salesChannelEntity->getFooterCategoryId();
  84.             default:
  85.                 return $id;
  86.         }
  87.     }
  88. }