custom/plugins/WbfkExtensions/src/CmsElementResolver/ServiceBoxElementResolver.php line 41

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\AbstractCmsElementResolver;
  7. use Shopware\Core\Content\Cms\DataResolver\Element\ElementDataCollection;
  8. use Shopware\Core\Content\Cms\DataResolver\ResolverContext\ResolverContext;
  9. use Shopware\Core\Content\Media\MediaDefinition;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. class ServiceBoxElementResolver extends AbstractCmsElementResolver
  12. {
  13.     protected const ELEMENT_TYPE 'service-box';
  14.     public function getType(): string
  15.     {
  16.         return static::ELEMENT_TYPE;
  17.     }
  18.     public function collect(CmsSlotEntity $slotResolverContext $resolverContext): ?CriteriaCollection
  19.     {
  20.         $config $slot->getFieldConfig();
  21.         // "mediaDesktop" is legacy for old elements. Now there is only "media" configured
  22.         $mediaConfig $config->get('media') ?? $config->get('mediaDesktop');
  23.         if (empty($mediaConfig?->getValue())) {
  24.             return null;
  25.         }
  26.         $criteriaCollection = new CriteriaCollection();
  27.         $criteria = new Criteria([$mediaConfig->getValue()]);
  28.         $criteriaCollection->add('service_box_' $mediaConfig->getValue(), MediaDefinition::class, $criteria);
  29.         return $criteriaCollection;
  30.     }
  31.     public function enrich(CmsSlotEntity $slotResolverContext $resolverContextElementDataCollection $result): void
  32.     {
  33.         $config $slot->getFieldConfig();
  34.         // "mediaDesktop" is legacy for old elements. Now there is only "media" configured
  35.         $mediaConfig $config->get('media') ?? $config->get('mediaDesktop');
  36.         if ($mediaConfig->isStatic()) {
  37.             $mediaID $mediaConfig->getValue();
  38.             $searchResult $result->get('service_box_' $mediaID);
  39.             if ($searchResult) {
  40.                 $slot->assign(['media' => $searchResult->get($mediaID)]);
  41.             }
  42.         }
  43.     }
  44. }