vendor/shopware/core/Content/Category/SalesChannel/CategoryRoute.php line 52

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Category\SalesChannel;
  3. use Shopware\Core\Content\Category\CategoryDefinition;
  4. use Shopware\Core\Content\Category\CategoryEntity;
  5. use Shopware\Core\Content\Category\Exception\CategoryNotFoundException;
  6. use Shopware\Core\Content\Cms\DataResolver\ResolverContext\EntityResolverContext;
  7. use Shopware\Core\Content\Cms\Exception\PageNotFoundException;
  8. use Shopware\Core\Content\Cms\SalesChannel\SalesChannelCmsPageLoaderInterface;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  11. use Shopware\Core\Framework\Log\Package;
  12. use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
  13. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepository;
  14. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. #[Route(defaults: ['_routeScope' => ['store-api']])]
  18. #[Package('content')]
  19. class CategoryRoute extends AbstractCategoryRoute
  20. {
  21.     final public const HOME 'home';
  22.     /**
  23.      * @internal
  24.      */
  25.     public function __construct(
  26.         private readonly SalesChannelRepository $categoryRepository,
  27.         private readonly SalesChannelCmsPageLoaderInterface $cmsPageLoader,
  28.         private readonly CategoryDefinition $categoryDefinition
  29.     ) {
  30.     }
  31.     public function getDecorated(): AbstractCategoryRoute
  32.     {
  33.         throw new DecorationPatternException(self::class);
  34.     }
  35.     #[Route(path'/store-api/category/{navigationId}'name'store-api.category.detail'methods: ['GET''POST'])]
  36.     public function load(string $navigationIdRequest $requestSalesChannelContext $context): CategoryRouteResponse
  37.     {
  38.         if ($navigationId === self::HOME) {
  39.             $navigationId $context->getSalesChannel()->getNavigationCategoryId();
  40.             $request->attributes->set('navigationId'$navigationId);
  41.             $routeParams $request->attributes->get('_route_params', []);
  42.             $routeParams['navigationId'] = $navigationId;
  43.             $request->attributes->set('_route_params'$routeParams);
  44.         }
  45.         $category $this->loadCategory($navigationId$context);
  46.         if (($category->getType() === CategoryDefinition::TYPE_FOLDER
  47.                 || $category->getType() === CategoryDefinition::TYPE_LINK)
  48.             && $context->getSalesChannel()->getNavigationCategoryId() !== $navigationId
  49.         ) {
  50.             throw new CategoryNotFoundException($navigationId);
  51.         }
  52.         $pageId $category->getCmsPageId();
  53.         $slotConfig $category->getTranslation('slotConfig');
  54.         $salesChannel $context->getSalesChannel();
  55.         if ($category->getId() === $salesChannel->getNavigationCategoryId() && $salesChannel->getHomeCmsPageId()) {
  56.             $pageId $salesChannel->getHomeCmsPageId();
  57.             $slotConfig $salesChannel->getTranslation('homeSlotConfig');
  58.         }
  59.         if (!$pageId) {
  60.             return new CategoryRouteResponse($category);
  61.         }
  62.         $resolverContext = new EntityResolverContext($context$request$this->categoryDefinition$category);
  63.         $pages $this->cmsPageLoader->load(
  64.             $request,
  65.             $this->createCriteria($pageId$request),
  66.             $context,
  67.             $slotConfig,
  68.             $resolverContext
  69.         );
  70.         if (!$pages->has($pageId)) {
  71.             throw new PageNotFoundException($pageId);
  72.         }
  73.         $category->setCmsPage($pages->get($pageId));
  74.         $category->setCmsPageId($pageId);
  75.         return new CategoryRouteResponse($category);
  76.     }
  77.     private function loadCategory(string $categoryIdSalesChannelContext $context): CategoryEntity
  78.     {
  79.         $criteria = new Criteria([$categoryId]);
  80.         $criteria->setTitle('category::data');
  81.         $criteria->addAssociation('media');
  82.         $category $this->categoryRepository
  83.             ->search($criteria$context)
  84.             ->get($categoryId);
  85.         if (!$category) {
  86.             throw new CategoryNotFoundException($categoryId);
  87.         }
  88.         return $category;
  89.     }
  90.     private function createCriteria(string $pageIdRequest $request): Criteria
  91.     {
  92.         $criteria = new Criteria([$pageId]);
  93.         $criteria->setTitle('category::cms-page');
  94.         $slots $request->get('slots');
  95.         if (\is_string($slots)) {
  96.             $slots explode('|'$slots);
  97.         }
  98.         if (!empty($slots) && \is_array($slots)) {
  99.             $criteria
  100.                 ->getAssociation('sections.blocks')
  101.                 ->addFilter(new EqualsAnyFilter('slots.id'$slots));
  102.         }
  103.         return $criteria;
  104.     }
  105. }