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

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace WbfkExtensions\CmsElementResolver;
  4. use Shopware\Core\Content\Cms\Aggregate\CmsSlot\CmsSlotEntity;
  5. use Shopware\Core\Content\Cms\DataResolver\CriteriaCollection;
  6. use Shopware\Core\Content\Cms\DataResolver\Element\ElementDataCollection;
  7. use Shopware\Core\Content\Cms\DataResolver\FieldConfig;
  8. use Shopware\Core\Content\Cms\DataResolver\ResolverContext\EntityResolverContext;
  9. use Shopware\Core\Content\Cms\DataResolver\ResolverContext\ResolverContext;
  10. use Shopware\Core\Content\Media\Cms\AbstractDefaultMediaResolver;
  11. use Shopware\Core\Content\Media\MediaDefinition;
  12. use Shopware\Core\Content\Media\MediaEntity;
  13. use Shopware\Core\Content\ProductStream\Service\ProductStreamBuilderInterface;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  15. use Shopware\Core\System\SystemConfig\SystemConfigService;
  16. class ImageProductSliderElementResolver extends ProductSliderCmsElementResolver
  17. {
  18.     use SliderElementResolverTrait;
  19.     protected const ELEMENT_TYPE 'image-product-slider';
  20.     private AbstractDefaultMediaResolver $mediaResolver;
  21.     public function __construct(ProductStreamBuilderInterface $productStreamBuilderSystemConfigService $systemConfigServiceAbstractDefaultMediaResolver $mediaResolver)
  22.     {
  23.         parent::__construct($productStreamBuilder$systemConfigService);
  24.         $this->mediaResolver $mediaResolver;
  25.     }
  26.     public function getType(): string
  27.     {
  28.         return static::ELEMENT_TYPE;
  29.     }
  30.     public function collect(CmsSlotEntity $slotResolverContext $resolverContext): ?CriteriaCollection
  31.     {
  32.         $collection parent::collect($slot$resolverContext);
  33.         $mediaConfig $slot->getFieldConfig()->get('media');
  34.         if (!(
  35.             $mediaConfig === null
  36.             || $mediaConfig->isMapped()
  37.             || $mediaConfig->isDefault()
  38.             || $mediaConfig->getValue() === null
  39.         )) {
  40.             $criteria = new Criteria([$mediaConfig->getStringValue()]);
  41.             $collection->add('media_' $slot->getUniqueIdentifier(), MediaDefinition::class, $criteria);
  42.         }
  43.         return $collection->all() ? $collection null;
  44.     }
  45.     public function enrich(CmsSlotEntity $slotResolverContext $resolverContextElementDataCollection $result): void
  46.     {
  47.         parent::enrich($slot$resolverContext$result);
  48.         $additionalDate = [];
  49.         $fieldConfig $slot->getFieldConfig();
  50.         $searchResult $result->get('media_' $slot->getUniqueIdentifier());
  51.         if ($searchResult !== null) {
  52.             $mediaConfig $fieldConfig->get('media');
  53.             if ($mediaConfig && $mediaConfig->getValue()) {
  54.                 $this->addMediaEntity($slot$additionalDate$result$mediaConfig$resolverContext);
  55.             }
  56.         }
  57.         /*
  58.          * Additional Slider Configurations
  59.          */
  60.         $isWbfkTwoRow $fieldConfig->get('boxLayout')->getValue() === 'wbfkTwoRows';
  61.         $isWithImage = !empty($additionalDate['media']);
  62.         $responsive = [
  63.             'xs' => $isWbfkTwoRow 1,
  64.             'sm' => $isWbfkTwoRow 2,
  65.             'md' => $isWithImage ? ($isWbfkTwoRow 1) : ($isWbfkTwoRow 2),
  66.             'lg' => $isWithImage ? ($isWbfkTwoRow 1) : ($isWbfkTwoRow 3),
  67.             'xl' => $isWithImage ? ($isWbfkTwoRow 2) : ($isWbfkTwoRow 4),
  68.         ];
  69.         $this->getSliderConfig($additionalDate$fieldConfig$responsive);;
  70.         $slot->assign($additionalDate);
  71.     }
  72.     private function addMediaEntity(
  73.         CmsSlotEntity $slot,
  74.         array &$additionalDate,
  75.         ElementDataCollection $result,
  76.         FieldConfig $config,
  77.         ResolverContext $resolverContext
  78.     ): void {
  79.         if ($config->isDefault()) {
  80.             $media $this->mediaResolver->getDefaultCmsMediaEntity($config->getStringValue());
  81.             if ($media) {
  82.                 $additionalDate['media'] = $media;
  83.             }
  84.         }
  85.         if ($config->isMapped() && $resolverContext instanceof EntityResolverContext) {
  86.             /** @var MediaEntity|null $media */
  87.             $media $this->resolveEntityValue($resolverContext->getEntity(), $config->getStringValue());
  88.             if ($media !== null) {
  89.                 $additionalDate['mediaId'] = $media->getUniqueIdentifier();
  90.                 $additionalDate['media'] = $media;
  91.             }
  92.         }
  93.         if ($config->isStatic()) {
  94.             $additionalDate['mediaId'] = $config->getStringValue();
  95.             $searchResult $result->get('media_' $slot->getUniqueIdentifier());
  96.             if (!$searchResult) {
  97.                 return;
  98.             }
  99.             /** @var MediaEntity|null $media */
  100.             $media $searchResult->get($config->getValue());
  101.             if (!$media) {
  102.                 return;
  103.             }
  104.             $additionalDate['media'] = $media;
  105.         }
  106.     }
  107. }