<?php
namespace B2bSellersCore\Components\Category\SalesChannel;
use Shopware\Core\Content\Category\SalesChannel\AbstractCategoryRoute;
use Shopware\Core\Content\Category\SalesChannel\CategoryRouteResponse;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route(defaults={"_routeScope"={"store-api"}})
*/
class CategoryRouteDecorator extends AbstractCategoryRoute
{
public const LISTING_PROPERTY_GROUPS_FIELD = 'b2b_table_listing_property_group_ids';
private AbstractCategoryRoute $decorated;
private EntityRepositoryInterface $propertyGroupRepository;
public function __construct(
AbstractCategoryRoute $decorated,
EntityRepositoryInterface $propertyGroupRepository
)
{
$this->decorated = $decorated;
$this->propertyGroupRepository = $propertyGroupRepository;
}
public function getDecorated(): AbstractCategoryRoute
{
return $this->decorated;
}
/**
* @Route("/store-api/category/{navigationId}", name="store-api.category.detail", methods={"GET","POST"})
*/
public function load(string $navigationId, Request $request, SalesChannelContext $context): CategoryRouteResponse
{
$result = $this->decorated->load($navigationId, $request, $context);
$category = $result->getCategory();
$customFields = $category->getCustomFields();
if (!$category->getCmsPage()) {
return $result;
}
$category->getCmsPage()->addExtension('category', clone $category);
if (!isset($customFields[self::LISTING_PROPERTY_GROUPS_FIELD]) || empty($customFields[self::LISTING_PROPERTY_GROUPS_FIELD])) {
return $result;
}
$ids = $customFields[self::LISTING_PROPERTY_GROUPS_FIELD];
$propertyGroups = $this->propertyGroupRepository->search(new Criteria($ids), $context->getContext());
$category->getCmsPage()->addExtension('listingTablePropertyGroups', $propertyGroups->getEntities());
return $result;
}
}