src/Storefront/Theme/ResolvedConfigLoader.php line 30

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Theme;
  3. use Shopware\Core\Content\Media\MediaCollection;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\Log\Package;
  7. use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
  8. use Shopware\Core\Framework\Uuid\Uuid;
  9. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  10. #[Package('storefront')]
  11. class ResolvedConfigLoader extends AbstractResolvedConfigLoader
  12. {
  13.     /**
  14.      * @internal
  15.      */
  16.     public function __construct(private readonly EntityRepository $repository, private readonly ThemeService $service)
  17.     {
  18.     }
  19.     public function getDecorated(): AbstractResolvedConfigLoader
  20.     {
  21.         throw new DecorationPatternException(self::class);
  22.     }
  23.     public function load(string $themeIdSalesChannelContext $context): array
  24.     {
  25.         $config $this->service->getThemeConfiguration($themeIdfalse$context->getContext());
  26.         $resolvedConfig = [];
  27.         $mediaItems = [];
  28.         if (!\array_key_exists('fields'$config)) {
  29.             return [];
  30.         }
  31.         foreach ($config['fields'] as $key => $data) {
  32.             if ($data['type'] === 'media' && $data['value'] && Uuid::isValid($data['value'])) {
  33.                 $mediaItems[$data['value']][] = $key;
  34.             }
  35.             $resolvedConfig[$key] = $data['value'];
  36.         }
  37.         $result = new MediaCollection();
  38.         /** @var array<string> $mediaIds */
  39.         $mediaIds array_keys($mediaItems);
  40.         if (!empty($mediaIds)) {
  41.             $criteria = new Criteria($mediaIds);
  42.             $criteria->setTitle('theme-service::resolve-media');
  43.             $result $this->repository->search($criteria$context->getContext());
  44.         }
  45.         foreach ($result as $media) {
  46.             if (!\array_key_exists($media->getId(), $mediaItems)) {
  47.                 continue;
  48.             }
  49.             foreach ($mediaItems[$media->getId()] as $key) {
  50.                 $resolvedConfig[$key] = $media->getUrl();
  51.             }
  52.         }
  53.         return $resolvedConfig;
  54.     }
  55. }