vendor/shopware/core/Content/Media/Cms/ImageCmsElementResolver.php line 95

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Media\Cms;
  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\FieldConfig;
  8. use Shopware\Core\Content\Cms\DataResolver\ResolverContext\EntityResolverContext;
  9. use Shopware\Core\Content\Cms\DataResolver\ResolverContext\ResolverContext;
  10. use Shopware\Core\Content\Cms\SalesChannel\Struct\ImageStruct;
  11. use Shopware\Core\Content\Media\MediaDefinition;
  12. use Shopware\Core\Content\Media\MediaEntity;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  14. use Shopware\Core\Framework\Log\Package;
  15. #[Package('content')]
  16. class ImageCmsElementResolver extends AbstractCmsElementResolver
  17. {
  18.     final public const CMS_DEFAULT_ASSETS_PATH '/bundles/storefront/assets/default/cms/';
  19.     /**
  20.      * @internal
  21.      */
  22.     public function __construct(private readonly AbstractDefaultMediaResolver $mediaResolver)
  23.     {
  24.     }
  25.     public function getType(): string
  26.     {
  27.         return 'image';
  28.     }
  29.     public function collect(CmsSlotEntity $slotResolverContext $resolverContext): ?CriteriaCollection
  30.     {
  31.         $mediaConfig $slot->getFieldConfig()->get('media');
  32.         if (
  33.             $mediaConfig === null
  34.             || $mediaConfig->isMapped()
  35.             || $mediaConfig->isDefault()
  36.             || $mediaConfig->getValue() === null
  37.         ) {
  38.             return null;
  39.         }
  40.         $criteria = new Criteria([$mediaConfig->getStringValue()]);
  41.         $criteriaCollection = new CriteriaCollection();
  42.         $criteriaCollection->add('media_' $slot->getUniqueIdentifier(), MediaDefinition::class, $criteria);
  43.         return $criteriaCollection;
  44.     }
  45.     public function enrich(CmsSlotEntity $slotResolverContext $resolverContextElementDataCollection $result): void
  46.     {
  47.         $config $slot->getFieldConfig();
  48.         $image = new ImageStruct();
  49.         $slot->setData($image);
  50.         $urlConfig $config->get('url');
  51.         if ($urlConfig !== null) {
  52.             if ($urlConfig->isStatic()) {
  53.                 $image->setUrl($urlConfig->getStringValue());
  54.             }
  55.             if ($urlConfig->isMapped() && $resolverContext instanceof EntityResolverContext) {
  56.                 $url $this->resolveEntityValue($resolverContext->getEntity(), $urlConfig->getStringValue());
  57.                 if ($url) {
  58.                     $image->setUrl($url);
  59.                 }
  60.             }
  61.             $newTabConfig $config->get('newTab');
  62.             if ($newTabConfig !== null) {
  63.                 $image->setNewTab($newTabConfig->getBoolValue());
  64.             }
  65.         }
  66.         $mediaConfig $config->get('media');
  67.         if ($mediaConfig && $mediaConfig->getValue()) {
  68.             $this->addMediaEntity($slot$image$result$mediaConfig$resolverContext);
  69.         }
  70.     }
  71.     private function addMediaEntity(
  72.         CmsSlotEntity $slot,
  73.         ImageStruct $image,
  74.         ElementDataCollection $result,
  75.         FieldConfig $config,
  76.         ResolverContext $resolverContext
  77.     ): void {
  78.         if ($config->isDefault()) {
  79.             $media $this->mediaResolver->getDefaultCmsMediaEntity($config->getStringValue());
  80.             if ($media) {
  81.                 $image->setMedia($media);
  82.             }
  83.         }
  84.         if ($config->isMapped() && $resolverContext instanceof EntityResolverContext) {
  85.             $media $this->resolveEntityValue($resolverContext->getEntity(), $config->getStringValue());
  86.             if ($media instanceof MediaEntity) {
  87.                 $image->setMediaId($media->getUniqueIdentifier());
  88.                 $image->setMedia($media);
  89.             }
  90.         }
  91.         if ($config->isStatic()) {
  92.             $image->setMediaId($config->getStringValue());
  93.             $searchResult $result->get('media_' $slot->getUniqueIdentifier());
  94.             if (!$searchResult) {
  95.                 return;
  96.             }
  97.             $media $searchResult->get($config->getStringValue());
  98.             if (!$media instanceof MediaEntity) {
  99.                 return;
  100.             }
  101.             $image->setMedia($media);
  102.         }
  103.     }
  104. }