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

  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.         $name $salesChannelEntity->getTranslation('name') ?? '';
  65.         \assert(\is_string($name));
  66.         switch ($id) {
  67.             case 'main-navigation':
  68.                 return $salesChannelEntity->getNavigationCategoryId();
  69.             case 'service-navigation':
  70.                 if ($salesChannelEntity->getServiceCategoryId() === null) {
  71.                     throw new \RuntimeException(\sprintf('Service category, for sales channel %s, is not set'$name));
  72.                 }
  73.                 return $salesChannelEntity->getServiceCategoryId();
  74.             case 'footer-navigation':
  75.                 if ($salesChannelEntity->getFooterCategoryId() === null) {
  76.                     throw new \RuntimeException(\sprintf('Footer category, for sales channel %s, is not set'$name));
  77.                 }
  78.                 return $salesChannelEntity->getFooterCategoryId();
  79.             default:
  80.                 return $id;
  81.         }
  82.     }
  83. }