vendor/store.shopware.com/h1webblog/src/Storefront/Page/Blog/BlogPageLoader.php line 132

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace H1web\Blog\Storefront\Page\Blog;
  3. use H1web\Blog\Blog\BlogDefinition;
  4. use H1web\Blog\Blog\BlogEntity;
  5. use H1web\Blog\Blog\SalesChannel\BlogAvailableFilter;
  6. use H1web\Blog\Service\BlogCategoryBreadCrumbBuilder;
  7. use Shopware\Core\Content\Cms\Aggregate\CmsBlock\CmsBlockEntity;
  8. use Shopware\Core\Content\Cms\Aggregate\CmsSection\CmsSectionEntity;
  9. use Shopware\Core\Content\Cms\CmsPageEntity;
  10. use Shopware\Core\Content\Cms\DataResolver\CmsSlotsDataResolver;
  11. use Shopware\Core\Content\Cms\DataResolver\ResolverContext\EntityResolverContext;
  12. use Shopware\Core\Content\Cms\Exception\PageNotFoundException;
  13. use Shopware\Core\Content\Cms\SalesChannel\SalesChannelCmsPageRepository;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  15. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  16. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  17. use Shopware\Storefront\Page\GenericPageLoader;
  18. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  19. use Symfony\Component\HttpFoundation\Request;
  20. class BlogPageLoader
  21. {
  22.     /**
  23.      * @var GenericPageLoader
  24.      */
  25.     private $genericLoader;
  26.     /**
  27.      * @var EventDispatcherInterface
  28.      */
  29.     private $eventDispatcher;
  30.     /**
  31.      * @var CmsSlotsDataResolver
  32.      */
  33.     private $slotDataResolver;
  34.     /**
  35.      * @var SalesChannelCmsPageRepository
  36.      */
  37.     private $cmsPageRepository;
  38.     /**
  39.      * @var EntityRepository
  40.      */
  41.     private $blogRepository;
  42.     /**
  43.      * @var BlogCategoryBreadCrumbBuilder
  44.      */
  45.     private $breadCrumbBuilder;
  46.     public function __construct(
  47.         GenericPageLoader $genericLoader,
  48.         EventDispatcherInterface $eventDispatcher,
  49.         CmsSlotsDataResolver $slotDataResolver,
  50.         SalesChannelCmsPageRepository $cmsPageRepository,
  51.         EntityRepository $blogRepository,
  52.         BlogCategoryBreadCrumbBuilder $breadCrumbBuilder
  53.     ) {
  54.         $this->eventDispatcher $eventDispatcher;
  55.         $this->genericLoader $genericLoader;
  56.         $this->slotDataResolver $slotDataResolver;
  57.         $this->cmsPageRepository $cmsPageRepository;
  58.         $this->blogRepository $blogRepository;
  59.         $this->breadCrumbBuilder $breadCrumbBuilder;
  60.     }
  61.     /**
  62.      * @throws PageNotFoundException
  63.      */
  64.     public function load(string $blogIdRequest $requestSalesChannelContext $context): BlogPage
  65.     {
  66.         try {
  67.             $page $this->genericLoader->load($request$context);
  68.             $page BlogPage::createFrom($page);
  69.             $criteria = new Criteria([$blogId]);
  70.             $criteria->addFilter(new BlogAvailableFilter($context->getSalesChannelId(), $request->cookies->get("timezone")));
  71.             $criteria->addAssociation('categories')
  72.                 ->addAssociation('tags')
  73.                 ->addAssociation('customFields')
  74.                 ->addAssociation('properties')
  75.                 ->addAssociation('properties.group');
  76.             $entities $this->blogRepository->search(
  77.                 $criteria,
  78.                 $context->getContext()
  79.             );
  80.             if ($entities->count() == 0) {
  81.                 throw new PageNotFoundException($blogId);
  82.             }
  83.             $blog $entities->first();
  84.             $blog->setSeoCategory($this->breadCrumbBuilder->getBlogSeoCategory($blog$context));
  85.             $page->setBlog($blog);
  86.             $this->loadMetaData($page);
  87.             if ($cmsPage $this->loadCmsPage($request$context$blog)) {
  88.                 $page->setCmsPage($cmsPage);
  89.             }
  90.             $this->eventDispatcher->dispatch(new BlogPageLoadedEvent($page$context$request));
  91.             return $page;
  92.         } catch (\Exception $e) {
  93.             throw new PageNotFoundException($blogId);
  94.         }
  95.     }
  96.     private function loadMetaData(BlogPage $page): void
  97.     {
  98.         $metaInformation $page->getMetaInformation();
  99.         $metaDescription $page->getBlog()->getMetaDescription();
  100.         $metaInformation->setMetaDescription((string) $metaDescription);
  101.         $metaInformation->setMetaKeywords((string) $page->getBlog()->getKeywords());
  102.         if ((string) $page->getBlog()->getMetaTitle() !== '') {
  103.             $metaInformation->setMetaTitle((string) $page->getBlog()->getMetaTitle());
  104.         } else {
  105.             $metaInformation->setMetaTitle((string) $page->getBlog()->getTitle());
  106.         }
  107.     }
  108.     private function loadCmsPage(Request $requestSalesChannelContext $contextBlogEntity $blog): ?CmsPageEntity
  109.     {
  110.         if (!$cmsPageId $blog->getCmsPageId()) {
  111.             return null;
  112.         }
  113.         $pages $this->cmsPageRepository->read([$cmsPageId], $context);
  114.         if ($pages->count() === 0) {
  115.             return null;
  116.         }
  117.         /** @var CmsPageEntity $page */
  118.         $page $pages->first();
  119.         if (!$page->getSections()) {
  120.             return null;
  121.         }
  122.         $page->getSections()->sort(function (CmsSectionEntity $aCmsSectionEntity $b) {
  123.             return $a->getPosition() <=> $b->getPosition();
  124.         });
  125.         $resolverContext = new EntityResolverContext($context$request, new BlogDefinition(), $blog);
  126.         // sort blocks into sectionPositions
  127.         foreach ($page->getSections() as $section) {
  128.             $section->getBlocks()->sort(function (CmsBlockEntity $aCmsBlockEntity $b) {
  129.                 return $a->getPosition() <=> $b->getPosition();
  130.             });
  131.         }
  132.         // find config overwrite
  133.         $config $blog->getSlotConfig();
  134.         $overwrite $config[$page->getId()] ?? $config;
  135.         // overwrite slot config
  136.         $this->overwriteSlotConfig($page$overwrite);
  137.         // resolve slot data
  138.         $slots $this->slotDataResolver->resolve($page->getSections()->getBlocks()->getSlots(), $resolverContext);
  139.         $page->getSections()->getBlocks()->setSlots($slots);
  140.         return $page;
  141.     }
  142.     private function overwriteSlotConfig(CmsPageEntity $page, ?array $config): void
  143.     {
  144.         foreach ($page->getSections()->getBlocks()->getSlots() as $slot) {
  145.             if ($slot->getConfig() === null && $slot->getTranslation('config') !== null) {
  146.                 $slot->setConfig($slot->getTranslation('config'));
  147.             }
  148.             if (empty($config)) {
  149.                 continue;
  150.             }
  151.             if (!isset($config[$slot->getId()])) {
  152.                 continue;
  153.             }
  154.             $defaultConfig $slot->getConfig() ?? [];
  155.             $merged array_replace_recursive(
  156.                 $defaultConfig,
  157.                 $config[$slot->getId()]
  158.             );
  159.             $slot->setConfig($merged);
  160.             $slot->addTranslated('config'$merged);
  161.         }
  162.     }
  163. }