src/Core/Content/Cms/SalesChannel/SalesChannelCmsPageLoader.php line 86

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Cms\SalesChannel;
  3. use Shopware\Core\Content\Cms\Aggregate\CmsBlock\CmsBlockEntity;
  4. use Shopware\Core\Content\Cms\Aggregate\CmsSection\CmsSectionEntity;
  5. use Shopware\Core\Content\Cms\Aggregate\CmsSlot\CmsSlotEntity;
  6. use Shopware\Core\Content\Cms\CmsPageEntity;
  7. use Shopware\Core\Content\Cms\DataResolver\CmsSlotsDataResolver;
  8. use Shopware\Core\Content\Cms\DataResolver\ResolverContext\ResolverContext;
  9. use Shopware\Core\Content\Cms\Events\CmsPageLoadedEvent;
  10. use Shopware\Core\Content\Cms\Events\CmsPageLoaderCriteriaEvent;
  11. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
  14. use Shopware\Core\Framework\Log\Package;
  15. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  16. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  17. use Symfony\Component\HttpFoundation\Request;
  18. #[Package('content')]
  19. class SalesChannelCmsPageLoader implements SalesChannelCmsPageLoaderInterface
  20. {
  21.     /**
  22.      * @internal
  23.      */
  24.     public function __construct(private readonly EntityRepository $cmsPageRepository, private readonly CmsSlotsDataResolver $slotDataResolver, private readonly EventDispatcherInterface $eventDispatcher)
  25.     {
  26.     }
  27.     public function load(
  28.         Request $request,
  29.         Criteria $criteria,
  30.         SalesChannelContext $context,
  31.         ?array $config null,
  32.         ?ResolverContext $resolverContext null
  33.     ): EntitySearchResult {
  34.         $this->eventDispatcher->dispatch(new CmsPageLoaderCriteriaEvent($request$criteria$context));
  35.         $config ??= [];
  36.         // ensure sections, blocks and slots are loaded, slots and blocks can be restricted by caller
  37.         $criteria->addAssociation('sections.backgroundMedia');
  38.         $criteria->addAssociation('sections.blocks.backgroundMedia');
  39.         $criteria->addAssociation('sections.blocks.slots');
  40.         // step 1, load cms pages with blocks and slots
  41.         $pages $this->cmsPageRepository->search($criteria$context->getContext());
  42.         foreach ($pages as $page) {
  43.             if ($page->getSections() === null) {
  44.                 continue;
  45.             }
  46.             $page->getSections()->sort(fn (CmsSectionEntity $aCmsSectionEntity $b) => $a->getPosition() <=> $b->getPosition());
  47.             if (!$resolverContext) {
  48.                 $resolverContext = new ResolverContext($context$request);
  49.             }
  50.             // step 2, sort blocks into sectionPositions
  51.             foreach ($page->getSections() as $section) {
  52.                 $section->getBlocks()->sort(fn (CmsBlockEntity $aCmsBlockEntity $b) => $a->getPosition() <=> $b->getPosition());
  53.                 foreach ($section->getBlocks() as $block) {
  54.                     $block->getSlots()->sort(fn (CmsSlotEntity $aCmsSlotEntity $b) => $a->getSlot() <=> $b->getSlot());
  55.                 }
  56.             }
  57.             // step 3, find config overwrite
  58.             $overwrite $config[$page->getId()] ?? $config;
  59.             // step 4, overwrite slot config
  60.             $this->overwriteSlotConfig($page$overwrite);
  61.             // step 5, resolve slot data
  62.             $this->loadSlotData($page$resolverContext);
  63.         }
  64.         $this->eventDispatcher->dispatch(new CmsPageLoadedEvent($request$pages->getEntities(), $context));
  65.         return $pages;
  66.     }
  67.     private function loadSlotData(CmsPageEntity $pageResolverContext $resolverContext): void
  68.     {
  69.         $slots $this->slotDataResolver->resolve($page->getSections()->getBlocks()->getSlots(), $resolverContext);
  70.         $page->getSections()->getBlocks()->setSlots($slots);
  71.     }
  72.     private function overwriteSlotConfig(CmsPageEntity $page, array $config): void
  73.     {
  74.         foreach ($page->getSections()->getBlocks()->getSlots() as $slot) {
  75.             if ($slot->getConfig() === null && $slot->getTranslation('config') !== null) {
  76.                 $slot->setConfig($slot->getTranslation('config'));
  77.             }
  78.             if (empty($config)) {
  79.                 continue;
  80.             }
  81.             if (!isset($config[$slot->getId()])) {
  82.                 continue;
  83.             }
  84.             $defaultConfig $slot->getConfig() ?? [];
  85.             $merged array_replace_recursive(
  86.                 $defaultConfig,
  87.                 $config[$slot->getId()]
  88.             );
  89.             $slot->setConfig($merged);
  90.             $slot->addTranslated('config'$merged);
  91.         }
  92.     }
  93. }