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

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