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

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Category\SalesChannel;
  3. use OpenApi\Annotations as OA;
  4. use Shopware\Core\Content\Category\CategoryCollection;
  5. use Shopware\Core\Content\Category\Exception\CategoryNotFoundException;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\Routing\Annotation\Entity;
  8. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  9. use Shopware\Core\Framework\Routing\Annotation\Since;
  10. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  11. use Shopware\Core\System\SalesChannel\SalesChannelEntity;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. /**
  15.  * @RouteScope(scopes={"store-api"})
  16.  */
  17. class TreeBuildingNavigationRoute extends AbstractNavigationRoute
  18. {
  19.     private AbstractNavigationRoute $decorated;
  20.     public function __construct(AbstractNavigationRoute $decorated)
  21.     {
  22.         $this->decorated $decorated;
  23.     }
  24.     public function getDecorated(): AbstractNavigationRoute
  25.     {
  26.         return $this->decorated;
  27.     }
  28.     /**
  29.      * @Since("6.2.0.0")
  30.      * @Entity("category")
  31.      * @OA\Post(
  32.      *      path="/navigation/{requestActiveId}/{requestRootId}",
  33.      *      summary="Fetch a navigation menu",
  34.      *      description="This endpoint returns categories that can be used as a page navigation. You can either return them as a tree or as a flat list. You can also control the depth of the tree.
  35.     Instead of passing uuids, you can also use one of the following aliases for the activeId and rootId parameters to get the respective navigations of your sales channel.
  36.      * main-navigation
  37.      * service-navigation
  38.      * footer-navigation",
  39.      *      operationId="readNavigation",
  40.      *      tags={"Store API", "Category"},
  41.      *      @OA\Parameter(name="Api-Basic-Parameters"),
  42.      *      @OA\Parameter(
  43.      *          name="sw-include-seo-urls",
  44.      *          description="Instructs Shopware to try and resolve SEO URLs for the given navigation item",
  45.      *          @OA\Schema(type="boolean"),
  46.      *          in="header",
  47.      *          required=false
  48.      *      ),
  49.      *      @OA\Parameter(
  50.      *          name="requestActiveId",
  51.      *          description="Identifier of the active category in the navigation tree (if not used, just set to the same as rootId).",
  52.      *          @OA\Schema(type="string", pattern="^[0-9a-f]{32}$"),
  53.      *          in="path",
  54.      *          required=true
  55.      *      ),
  56.      *      @OA\Parameter(
  57.      *          name="requestRootId",
  58.      *          description="Identifier of the root category for your desired navigation tree. You can use it to fetch sub-trees of your navigation tree.",
  59.      *          @OA\Schema(type="string", pattern="^[0-9a-f]{32}$"),
  60.      *          in="path",
  61.      *          required=true
  62.      *      ),
  63.      *      @OA\RequestBody(
  64.      *          required=true,
  65.      *          @OA\JsonContent(
  66.      *              @OA\Property(
  67.      *                  property="depth",
  68.      *                  description="Determines the depth of fetched navigation levels.",
  69.      *                  @OA\Schema(type="integer", default="2")
  70.      *              ),
  71.      *              @OA\Property(
  72.      *                  property="buildTree",
  73.      *                  description="Return the categories as a tree or as a flat list.",
  74.      *                  @OA\Schema(type="boolean", default="true")
  75.      *              )
  76.      *          )
  77.      *      ),
  78.      *      @OA\Response(
  79.      *          response="200",
  80.      *          description="All available navigations",
  81.      *          @OA\JsonContent(ref="#/components/schemas/NavigationRouteResponse")
  82.      *     )
  83.      * )
  84.      * @Route("/store-api/navigation/{activeId}/{rootId}", name="store-api.navigation", methods={"GET", "POST"})
  85.      */
  86.     public function load(string $activeIdstring $rootIdRequest $requestSalesChannelContext $contextCriteria $criteria): NavigationRouteResponse
  87.     {
  88.         $activeId $this->resolveAliasId($activeId$context->getSalesChannel());
  89.         $rootId $this->resolveAliasId($rootId$context->getSalesChannel());
  90.         if ($activeId === null) {
  91.             throw new CategoryNotFoundException($request->get('activeId'));
  92.         }
  93.         if ($rootId === null) {
  94.             throw new CategoryNotFoundException($request->get('rootId'));
  95.         }
  96.         $response $this->getDecorated()->load($activeId$rootId$request$context$criteria);
  97.         $buildTree $request->query->getBoolean('buildTree'$request->request->getBoolean('buildTree'true));
  98.         if (!$buildTree) {
  99.             return $response;
  100.         }
  101.         $categories $this->buildTree($rootId$response->getCategories()->getElements());
  102.         return new NavigationRouteResponse($categories);
  103.     }
  104.     private function buildTree(?string $parentId, array $categories): CategoryCollection
  105.     {
  106.         $children = new CategoryCollection();
  107.         foreach ($categories as $key => $category) {
  108.             if ($category->getParentId() !== $parentId) {
  109.                 continue;
  110.             }
  111.             unset($categories[$key]);
  112.             $children->add($category);
  113.         }
  114.         $children->sortByPosition();
  115.         $items = new CategoryCollection();
  116.         foreach ($children as $child) {
  117.             if (!$child->getActive() || !$child->getVisible()) {
  118.                 continue;
  119.             }
  120.             $child->setChildren($this->buildTree($child->getId(), $categories));
  121.             $items->add($child);
  122.         }
  123.         return $items;
  124.     }
  125.     private function resolveAliasId(string $idSalesChannelEntity $salesChannelEntity): ?string
  126.     {
  127.         switch ($id) {
  128.             case 'main-navigation':
  129.                 return $salesChannelEntity->getNavigationCategoryId();
  130.             case 'service-navigation':
  131.                 return $salesChannelEntity->getServiceCategoryId();
  132.             case 'footer-navigation':
  133.                 return $salesChannelEntity->getFooterCategoryId();
  134.             default:
  135.                 return $id;
  136.         }
  137.     }
  138. }