custom/plugins/WbfkExtensions/src/CmsElementResolver/CategoriesCmsElementResolver.php line 62

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace WbfkExtensions\CmsElementResolver;
  3. use Shopware\Core\Content\Category\CategoryDefinition;
  4. use Shopware\Core\Content\Category\Service\AbstractCategoryUrlGenerator;
  5. use Shopware\Core\Content\Cms\Aggregate\CmsSlot\CmsSlotEntity;
  6. use Shopware\Core\Content\Cms\DataResolver\CriteriaCollection;
  7. use Shopware\Core\Content\Cms\DataResolver\Element\AbstractCmsElementResolver;
  8. use Shopware\Core\Content\Cms\DataResolver\Element\ElementDataCollection;
  9. use Shopware\Core\Content\Cms\DataResolver\ResolverContext\ResolverContext;
  10. use Shopware\Core\Content\Media\MediaDefinition;
  11. use Shopware\Core\Content\Media\MediaEntity;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. class CategoriesCmsElementResolver extends AbstractCmsElementResolver
  14. {
  15.     /**
  16.      * @var AbstractCategoryUrlGenerator
  17.      */
  18.     private $categoryUrlGenerator;
  19.     public function __construct(
  20.         AbstractCategoryUrlGenerator $categoryUrlGenerator
  21.     ) {
  22.         $this->categoryUrlGenerator $categoryUrlGenerator;
  23.     }
  24.     public function getType(): string
  25.     {
  26.         return 'icon-banner';
  27.     }
  28.     public function collect(CmsSlotEntity $slotResolverContext $resolverContext): ?CriteriaCollection
  29.     {
  30.         $categoryId $slot->getConfig()['linkUrl']['value'];
  31.         $criteria = new Criteria([$categoryId]);
  32.         $criteria->addAssociations([
  33.             'children.children.translated.externalLink',
  34.             'children.children',
  35.             'children.translated.externalLink',
  36.             'children.active'
  37.         ]);
  38.         $config $slot->getFieldConfig();
  39.         $icon $config->get('icon');
  40.         $criteriaCollection = new CriteriaCollection();
  41.         $criteriaCollection->add('category'CategoryDefinition::class, $criteria);
  42.         if ($icon && $icon->getValue() !== null) {
  43.             $criteria = new Criteria([$icon->getValue()]);
  44.             $criteriaCollection->add('desktop_' $icon->getValue(), MediaDefinition::class, $criteria);
  45.         }
  46.         return $criteriaCollection;
  47.     }
  48.     public function enrich(CmsSlotEntity $slotResolverContext $resolverContextElementDataCollection $result): void
  49.     {
  50.         if (!(count($result->get('category')->getCriteria()->getIds()) > 0)) {
  51.             return;
  52.         }
  53.         $categoryId $result->get('category')->getCriteria()->getIds()[0];
  54.         $saleschannel $resolverContext->getSalesChannelContext()->getSalesChannel();
  55.         $category $result->get('category')->getEntities()->getElements()[$categoryId];
  56.         $data["linkUrl"] = [
  57.             "name" => $category->getName(),
  58.             "url" => $this->categoryUrlGenerator->generate($category$saleschannel)
  59.         ];
  60.         $config $slot->getFieldConfig();
  61.         $desktop $this->getMedia('desktop'$config->get('icon'), $result);
  62.         $slot->assign(
  63.             [
  64.                 'images' => [
  65.                     'bannerIcon' => $desktop
  66.                 ]
  67.             ]
  68.         );
  69.         $slot->setConfig($data);
  70.     }
  71.     private function getMedia($size$mediaConfig$result)
  72.     {
  73.         if (is_null($mediaConfig)) {
  74.             return;
  75.         }
  76.         if ($mediaConfig->isStatic()) {
  77.             $mediaID $mediaConfig->getValue();
  78.             $searchResult $result->get($size '_' $mediaID);
  79.             if ($searchResult) {
  80.                 /** @var MediaEntity|null $media */
  81.                 return $searchResult->get($mediaID);
  82.             }
  83.         }
  84.         return false;
  85.     }
  86. }