custom/plugins/WbfkExtensions/src/CmsElementResolver/PartnerGridCmsElementResolver.php line 52

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 PartnerGridCmsElementResolver extends AbstractCmsElementResolver
  11. {
  12.     public function getType(): string
  13.     {
  14.         return 'partner-grid';
  15.     }
  16.     public function collect(CmsSlotEntity $slotResolverContext $resolverContext): ?CriteriaCollection
  17.     {
  18.         $partnerImageIds = [];
  19.         $partnerElements $slot->getFieldConfig()->get('partners')?->getValue();
  20.         if (!isset($partnerElements)) {
  21.             return null;
  22.         }
  23.         foreach ($partnerElements as $element) {
  24.             if (isset($element['imageId'])) {
  25.                 if (is_array($element['imageId'])) {
  26.                     $partnerImageIds[] = $element['imageId']["id"];
  27.                 } else {
  28.                     $partnerImageIds[] = $element['imageId'];
  29.                 }
  30.             }
  31.         }
  32.         if (empty($partnerImageIds)) {
  33.             return null;
  34.         }
  35.         $criteria = new Criteria($partnerImageIds);
  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.         $partners = [];
  43.         $partnerElements $slot->getFieldConfig()->get('partners')?->getValue();
  44.         if (!isset($partnerElements)) {
  45.             return;
  46.         }
  47.         $partnerElementsCollection $result->get('media')->getEntities()->getElements();
  48.         foreach ($partnerElements as $element) {
  49.             if (isset($element['imageId']) && isset($partnerElementsCollection[$element['imageId']])) {
  50.                 $partners[] = [
  51.                     'media' => $partnerElementsCollection[$element['imageId']],
  52.                     'element' => $element
  53.                 ];
  54.             }
  55.         }
  56.         $slot->assign([
  57.             'partners' => $partners
  58.         ]);
  59.     }
  60. }