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

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