vendor/shopware/core/Content/Cms/SalesChannel/SalesChannelCmsPageLoader.php line 50

  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(
  25.         private readonly EntityRepository $cmsPageRepository,
  26.         private readonly CmsSlotsDataResolver $slotDataResolver,
  27.         private readonly EventDispatcherInterface $eventDispatcher
  28.     ) {
  29.     }
  30.     public function load(
  31.         Request $request,
  32.         Criteria $criteria,
  33.         SalesChannelContext $context,
  34.         ?array $config null,
  35.         ?ResolverContext $resolverContext null
  36.     ): EntitySearchResult {
  37.         $this->eventDispatcher->dispatch(new CmsPageLoaderCriteriaEvent($request$criteria$context));
  38.         $config ??= [];
  39.         // ensure sections, blocks and slots are loaded, slots and blocks can be restricted by caller
  40.         $criteria->addAssociation('sections.backgroundMedia');
  41.         $criteria->addAssociation('sections.blocks.backgroundMedia');
  42.         $criteria->addAssociation('sections.blocks.slots');
  43.         // step 1, load cms pages with blocks and slots
  44.         $pages $this->cmsPageRepository->search($criteria$context->getContext());
  45.         foreach ($pages as $page) {
  46.             if ($page->getSections() === null) {
  47.                 continue;
  48.             }
  49.             $page->getSections()->sort(fn (CmsSectionEntity $aCmsSectionEntity $b) => $a->getPosition() <=> $b->getPosition());
  50.             if (!$resolverContext) {
  51.                 $resolverContext = new ResolverContext($context$request);
  52.             }
  53.             // step 2, sort blocks into sectionPositions
  54.             foreach ($page->getSections() as $section) {
  55.                 $section->getBlocks()->sort(fn (CmsBlockEntity $aCmsBlockEntity $b) => $a->getPosition() <=> $b->getPosition());
  56.                 foreach ($section->getBlocks() as $block) {
  57.                     $block->getSlots()->sort(fn (CmsSlotEntity $aCmsSlotEntity $b) => $a->getSlot() <=> $b->getSlot());
  58.                 }
  59.             }
  60.             // step 3, find config overwrite
  61.             $overwrite $config[$page->getId()] ?? $config;
  62.             // step 4, overwrite slot config
  63.             $this->overwriteSlotConfig($page$overwrite);
  64.             // step 5, resolve slot data
  65.             $this->loadSlotData($page$resolverContext);
  66.         }
  67.         $this->eventDispatcher->dispatch(new CmsPageLoadedEvent($request$pages->getEntities(), $context));
  68.         return $pages;
  69.     }
  70.     private function loadSlotData(CmsPageEntity $pageResolverContext $resolverContext): void
  71.     {
  72.         $slots $this->slotDataResolver->resolve($page->getSections()->getBlocks()->getSlots(), $resolverContext);
  73.         $page->getSections()->getBlocks()->setSlots($slots);
  74.     }
  75.     private function overwriteSlotConfig(CmsPageEntity $page, array $config): void
  76.     {
  77.         foreach ($page->getSections()->getBlocks()->getSlots() as $slot) {
  78.             if ($slot->getConfig() === null && $slot->getTranslation('config') !== null) {
  79.                 $slot->setConfig($slot->getTranslation('config'));
  80.             }
  81.             if (empty($config)) {
  82.                 continue;
  83.             }
  84.             if (!isset($config[$slot->getId()])) {
  85.                 continue;
  86.             }
  87.             $defaultConfig $slot->getConfig() ?? [];
  88.             $merged array_replace_recursive(
  89.                 $defaultConfig,
  90.                 $config[$slot->getId()]
  91.             );
  92.             $slot->setConfig($merged);
  93.             $slot->addTranslated('config'$merged);
  94.         }
  95.     }
  96. }