custom/plugins/JkwebShopwareCategoryListingPlugin/src/DataResolver/CategoryListingDataResolver.php line 45

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Jkweb\Shopware\Plugin\CategoryListing\DataResolver;
  3. use Jkweb\Shopware\Plugin\CategoryListing\Struct\CategoryListingStruct;
  4. use Shopware\Core\Content\Category\CategoryCollection;
  5. use Shopware\Core\Content\Category\CategoryDefinition;
  6. use Shopware\Core\Content\Category\CategoryEntity;
  7. use Shopware\Core\Content\Category\Service\NavigationLoader;
  8. use Shopware\Core\Content\Category\Tree\TreeItem;
  9. use Shopware\Core\Content\Cms\Aggregate\CmsSlot\CmsSlotEntity;
  10. use Shopware\Core\Content\Cms\DataResolver\CriteriaCollection;
  11. use Shopware\Core\Content\Cms\DataResolver\Element\AbstractCmsElementResolver;
  12. use Shopware\Core\Content\Cms\DataResolver\Element\ElementDataCollection;
  13. use Shopware\Core\Content\Cms\DataResolver\ResolverContext\EntityResolverContext;
  14. use Shopware\Core\Content\Cms\DataResolver\ResolverContext\ResolverContext;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  16. class CategoryListingDataResolver extends AbstractCmsElementResolver
  17. {
  18.     private $navigationLoader;
  19.     public function __construct(NavigationLoader $navigationLoader)
  20.     {
  21.         $this->navigationLoader $navigationLoader;
  22.     }
  23.     public function getType(): string
  24.     {
  25.         return 'category-listing';
  26.     }
  27.     public function collect(CmsSlotEntity $slotResolverContext $resolverContext): ?CriteriaCollection
  28.     {
  29.         $config $slot->getFieldConfig();
  30.         $categoriesConfig $config->get('categories');
  31.         if (!$categoriesConfig || $categoriesConfig->isStatic()) {
  32.             return null;
  33.         }
  34.         $categoryIds $categoriesConfig->getValue();
  35.         $criteria = new Criteria($categoryIds);
  36.         $criteria->addAssociation('media');
  37.         $criteriaCollection = new CriteriaCollection();
  38.         $criteriaCollection->add('categories_' $slot->getUniqueIdentifier(), CategoryDefinition::class, $criteria);
  39.         return $criteriaCollection;
  40.     }
  41.     public function enrich(CmsSlotEntity $slotResolverContext $resolverContextElementDataCollection $result): void
  42.     {
  43.         $categoryListing = new CategoryListingStruct();
  44.         $slot->setData($categoryListing);
  45.         $config $slot->getFieldConfig();
  46.         $categories $config->get('categories');
  47.         if ($categories->isMapped()) {
  48.             $categoryCollection = new CategoryCollection();
  49.             $categoryListing->setCategories($categoryCollection);;
  50.             if(empty($config->get('categories')->getValue())) {
  51.                 $this->addChildCategories($resolverContext$categoryCollection);
  52.             } else {
  53.                 foreach ($categories->getValue() as $categoryId) {
  54.                     $this->addCategory($slot$categoryCollection$result$categoryId);
  55.                 }
  56.             }
  57.         }
  58.         if($rowClass $config->get('rowElementClassName')) {
  59.             $categoryListing->setRowElementClassName($rowClass->getValue());
  60.         }
  61.         if($colClass $config->get('colElementClassName')) {
  62.             $categoryListing->setColElementClassName($colClass->getValue());
  63.         }
  64.         if($headingPosition $config->get('headingPosition')) {
  65.             $categoryListing->setHeadingPosition($headingPosition->getValue());
  66.         }
  67.     }
  68.     private function addCategory(CmsSlotEntity $slotCategoryCollection $categoryCollectionElementDataCollection $resultstring $configId): void
  69.     {
  70.         $searchResult $result->get('categories_' $slot->getUniqueIdentifier());
  71.         if (!$searchResult) {
  72.             return;
  73.         }
  74.         $category $searchResult->get($configId);
  75.         if ($category instanceof CategoryEntity) {
  76.             $categoryCollection->add($category);
  77.         }
  78.     }
  79.     private function addChildCategories(ResolverContext $resolverContextCategoryCollection $categoryCollection): void
  80.     {
  81.         if($resolverContext instanceof EntityResolverContext) {
  82.             $category $resolverContext->getEntity();
  83.             if($category instanceof CategoryEntity) {
  84.                 $tree $this->navigationLoader->load($category->getId(), $resolverContext->getSalesChannelContext(), $category->getId(), 1);
  85.                 foreach($tree->getTree() as $child) {
  86.                     $categoryCollection->add($child->getCategory());
  87.                 }
  88.             }
  89.         }
  90.     }
  91. }