custom/plugins/WbfkExtensions/src/CmsElementResolver/DynamicSliderCmsElementResolver.php line 72

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace WbfkExtensions\CmsElementResolver;
  3. use Shopware\Core\Content\Cms\Aggregate\CmsSlot\CmsSlotEntity;
  4. use Shopware\Core\Content\Cms\DataResolver\CriteriaCollection;
  5. use Shopware\Core\Content\Cms\DataResolver\Element\AbstractCmsElementResolver;
  6. use Shopware\Core\Content\Cms\DataResolver\Element\ElementDataCollection;
  7. use Shopware\Core\Content\Cms\DataResolver\ResolverContext\ResolverContext;
  8. use Shopware\Core\Content\Media\MediaDefinition;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. class DynamicSliderCmsElementResolver extends AbstractCmsElementResolver
  11. {
  12.     public function getType(): string
  13.     {
  14.         return 'dynamic-slider';
  15.     }
  16.     public function collect(CmsSlotEntity $slotResolverContext $resolverContext): ?CriteriaCollection
  17.     {
  18.         $sliderImageIds = [];
  19.         $config $slot->getFieldConfig();
  20.         $sliderElements $config->getElements()["sliderElements"]->getValue();
  21.         foreach ($sliderElements as $element) {
  22.             foreach (["mediaDesktopId""mediaMobileId"] as $type) {
  23.                 if (isset($element[$type])) {
  24.                     if (is_array($element[$type])) {
  25.                         array_push($sliderImageIds$element[$type]["id"]);
  26.                     } else {
  27.                         array_push($sliderImageIds$element[$type]);
  28.                     }
  29.                 }
  30.             }
  31.         }
  32.         if (empty($sliderImageIds)) {
  33.             return null;
  34.         }
  35.         $criteria = new Criteria($sliderImageIds);
  36.         $criteriaCollection = new CriteriaCollection();
  37.         $criteriaCollection->add('media'MediaDefinition::class, $criteria);
  38.         return $criteriaCollection;
  39.     }
  40.     public function enrich(CmsSlotEntity $slotResolverContext $resolverContextElementDataCollection $result): void
  41.     {
  42.         $slides = [];
  43.         $config $slot->getFieldConfig()->getElements()["sliderElements"]->getValue();
  44.         $sliderElementsCollection $result->get('media')?->getEntities()?->getElements();
  45.         foreach ($config as $element) {
  46.             $slide = [];
  47.             foreach (["mediaDesktopId""mediaMobileId"] as $mediaType) {
  48.                 if (! empty($element[$mediaType])) {
  49.                     $mediaId $element[$mediaType];
  50.                     if (isset($sliderElementsCollection[$mediaId])) {
  51.                         $slide[$mediaType] = $sliderElementsCollection[$mediaId];
  52.                     }
  53.                 }
  54.             }
  55.             $slides[] = $slide;
  56.         }
  57.         $slot->assign([
  58.             'slides' => $slides
  59.         ]);
  60.     }
  61. }