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

  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\Cms\Aggregate\CmsSlot\CmsSlotEntity;
  9. use Shopware\Core\Content\Cms\DataResolver\CriteriaCollection;
  10. use Shopware\Core\Content\Cms\DataResolver\Element\AbstractCmsElementResolver;
  11. use Shopware\Core\Content\Cms\DataResolver\Element\ElementDataCollection;
  12. use Shopware\Core\Content\Cms\DataResolver\FieldConfigCollection;
  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 $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.         if (!$categoryIds) {
  36.             return null;
  37.         }
  38.         $criteria = new Criteria($categoryIds);
  39.         $criteria->addAssociation('media');
  40.         $criteriaCollection = new CriteriaCollection();
  41.         $criteriaCollection->add('categories_' $slot->getUniqueIdentifier(), CategoryDefinition::class, $criteria);
  42.         return $criteriaCollection;
  43.     }
  44.     public function enrich(CmsSlotEntity $slotResolverContext $resolverContextElementDataCollection $result): void
  45.     {
  46.         $categoryListing = new CategoryListingStruct();
  47.         $slot->setData($categoryListing);
  48.         /** @var FieldConfigCollection $config */
  49.         $config $slot->getFieldConfig();
  50.         $categories $config->get('categories');
  51.         if ($categories->isMapped()) {
  52.             $categoryCollection = new CategoryCollection();
  53.             $categoryListing->setCategories($categoryCollection);;
  54.             $categoryIds $categories->getValue();
  55.             if (!$categoryIds) {
  56.                 $this->addChildCategories($resolverContext$categoryCollection);
  57.             } else {
  58.                 foreach ($categoryIds as $categoryId) {
  59.                     $this->addCategory($slot$categoryCollection$result$categoryId);
  60.                 }
  61.             }
  62.         }
  63.         if($rowClass $config->get('rowElementClassName')) {
  64.             $categoryListing->setRowElementClassName($rowClass->getValue());
  65.         }
  66.         if($colClass $config->get('colElementClassName')) {
  67.             $categoryListing->setColElementClassName($colClass->getValue());
  68.         }
  69.         if($headingPosition $config->get('headingPosition')) {
  70.             $categoryListing->setHeadingPosition($headingPosition->getValue());
  71.         }
  72.     }
  73.     private function addCategory(CmsSlotEntity $slotCategoryCollection $categoryCollectionElementDataCollection $resultstring $configId): void
  74.     {
  75.         $searchResult $result->get('categories_' $slot->getUniqueIdentifier());
  76.         if (!$searchResult) {
  77.             return;
  78.         }
  79.         $category $searchResult->get($configId);
  80.         if ($category instanceof CategoryEntity) {
  81.             $categoryCollection->add($category);
  82.         }
  83.     }
  84.     private function addChildCategories(ResolverContext $resolverContextCategoryCollection $categoryCollection): void
  85.     {
  86.         if($resolverContext instanceof EntityResolverContext) {
  87.             $category $resolverContext->getEntity();
  88.             if($category instanceof CategoryEntity) {
  89.                 $tree $this->navigationLoader->load($category->getId(), $resolverContext->getSalesChannelContext(), $category->getId(), 1);
  90.                 foreach($tree->getTree() as $child) {
  91.                     $categoryCollection->add($child->getCategory());
  92.                 }
  93.             }
  94.         }
  95.     }
  96. }