src/Core/Content/Category/SalesChannel/CategoryRoute.php line 99

  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(private readonly SalesChannelRepository $categoryRepository, private readonly SalesChannelCmsPageLoaderInterface $cmsPageLoader, private readonly CategoryDefinition $categoryDefinition)
  26.     {
  27.     }
  28.     public function getDecorated(): AbstractCategoryRoute
  29.     {
  30.         throw new DecorationPatternException(self::class);
  31.     }
  32.     #[Route(path'/store-api/category/{navigationId}'name'store-api.category.detail'methods: ['GET''POST'])]
  33.     public function load(string $navigationIdRequest $requestSalesChannelContext $context): CategoryRouteResponse
  34.     {
  35.         if ($navigationId === self::HOME) {
  36.             $navigationId $context->getSalesChannel()->getNavigationCategoryId();
  37.             $request->attributes->set('navigationId'$navigationId);
  38.             $routeParams $request->attributes->get('_route_params', []);
  39.             $routeParams['navigationId'] = $navigationId;
  40.             $request->attributes->set('_route_params'$routeParams);
  41.         }
  42.         $category $this->loadCategory($navigationId$context);
  43.         if (($category->getType() === CategoryDefinition::TYPE_FOLDER
  44.                 || $category->getType() === CategoryDefinition::TYPE_LINK)
  45.             && $context->getSalesChannel()->getNavigationCategoryId() !== $navigationId
  46.         ) {
  47.             throw new CategoryNotFoundException($navigationId);
  48.         }
  49.         $pageId $category->getCmsPageId();
  50.         $slotConfig $category->getTranslation('slotConfig');
  51.         $salesChannel $context->getSalesChannel();
  52.         if ($category->getId() === $salesChannel->getNavigationCategoryId() && $salesChannel->getHomeCmsPageId()) {
  53.             $pageId $salesChannel->getHomeCmsPageId();
  54.             $slotConfig $salesChannel->getTranslation('homeSlotConfig');
  55.         }
  56.         if (!$pageId) {
  57.             return new CategoryRouteResponse($category);
  58.         }
  59.         $resolverContext = new EntityResolverContext($context$request$this->categoryDefinition$category);
  60.         $pages $this->cmsPageLoader->load(
  61.             $request,
  62.             $this->createCriteria($pageId$request),
  63.             $context,
  64.             $slotConfig,
  65.             $resolverContext
  66.         );
  67.         if (!$pages->has($pageId)) {
  68.             throw new PageNotFoundException($pageId);
  69.         }
  70.         $category->setCmsPage($pages->get($pageId));
  71.         $category->setCmsPageId($pageId);
  72.         return new CategoryRouteResponse($category);
  73.     }
  74.     private function loadCategory(string $categoryIdSalesChannelContext $context): CategoryEntity
  75.     {
  76.         $criteria = new Criteria([$categoryId]);
  77.         $criteria->setTitle('category::data');
  78.         $criteria->addAssociation('media');
  79.         $category $this->categoryRepository
  80.             ->search($criteria$context)
  81.             ->get($categoryId);
  82.         if (!$category) {
  83.             throw new CategoryNotFoundException($categoryId);
  84.         }
  85.         return $category;
  86.     }
  87.     private function createCriteria(string $pageIdRequest $request): Criteria
  88.     {
  89.         $criteria = new Criteria([$pageId]);
  90.         $criteria->setTitle('category::cms-page');
  91.         $slots $request->get('slots');
  92.         if (\is_string($slots)) {
  93.             $slots explode('|'$slots);
  94.         }
  95.         if (!empty($slots) && \is_array($slots)) {
  96.             $criteria
  97.                 ->getAssociation('sections.blocks')
  98.                 ->addFilter(new EqualsAnyFilter('slots.id'$slots));
  99.         }
  100.         return $criteria;
  101.     }
  102. }