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

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