custom/plugins/SasBlogModule/src/Page/Blog/BlogPageLoader.php line 147

  1. <?php declare(strict_types=1);
  2. namespace Sas\BlogModule\Page\Blog;
  3. use Sas\BlogModule\Content\Blog\BlogEntriesEntity;
  4. use Shopware\Core\Content\Cms\CmsPageEntity;
  5. use Shopware\Core\Content\Cms\Exception\PageNotFoundException;
  6. use Shopware\Core\Content\Cms\SalesChannel\SalesChannelCmsPageLoaderInterface;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\Routing\Exception\MissingRequestParameterException;
  11. use Shopware\Core\Framework\Routing\RoutingException;
  12. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  13. use Shopware\Core\System\SystemConfig\Exception\ConfigurationNotFoundException;
  14. use Shopware\Core\System\SystemConfig\SystemConfigService;
  15. use Shopware\Storefront\Page\GenericPageLoaderInterface;
  16. use Shopware\Storefront\Page\MetaInformation;
  17. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  18. use Symfony\Component\HttpFoundation\Request;
  19. class BlogPageLoader
  20. {
  21.     private SystemConfigService $systemConfigService;
  22.     private GenericPageLoaderInterface $genericLoader;
  23.     private EventDispatcherInterface $eventDispatcher;
  24.     private SalesChannelCmsPageLoaderInterface $cmsPageLoader;
  25.     private EntityRepository $blogRepository;
  26.     public function __construct(
  27.         SystemConfigService $systemConfigService,
  28.         GenericPageLoaderInterface $genericLoader,
  29.         EventDispatcherInterface $eventDispatcher,
  30.         SalesChannelCmsPageLoaderInterface $cmsPageLoader,
  31.         EntityRepository $blogRepository
  32.     ) {
  33.         $this->systemConfigService $systemConfigService;
  34.         $this->genericLoader $genericLoader;
  35.         $this->eventDispatcher $eventDispatcher;
  36.         $this->cmsPageLoader $cmsPageLoader;
  37.         $this->blogRepository $blogRepository;
  38.     }
  39.     /**
  40.      * Loads the blog page data
  41.      * It gets article id from request
  42.      * It get Storefront Page's instance for given request
  43.      * It assigns metadata to page instance
  44.      * It dispatches an event to allow other extensions to modify the page instance
  45.      *
  46.      * @throws PageNotFoundException
  47.      * @throws InconsistentCriteriaIdsException
  48.      * @throws MissingRequestParameterException
  49.      * @throws ConfigurationNotFoundException
  50.      */
  51.     public function load(Request $requestSalesChannelContext $context): BlogPage
  52.     {
  53.         $articleId $request->attributes->get('articleId');
  54.         if (!$articleId) {
  55.             throw RoutingException::missingRequestParameter('articleId''/articleId');
  56.         }
  57.         $blogEntry $this->loadBlogEntry($articleId$context);
  58.         $detailCmsPage $this->loadBlogDetailCmsPage($request$context);
  59.         $page $this->genericLoader->load($request$context);
  60.         $page BlogPage::createFrom($page);
  61.         $page->setBlogEntry($blogEntry);
  62.         $page->setCmsPage($detailCmsPage);
  63.         if (
  64.             $page->getHeader()
  65.             && $page->getHeader()->getNavigation()
  66.             && $page->getHeader()->getNavigation()->getActive()
  67.         ) {
  68.             $navigationId $page->getHeader()->getNavigation()->getActive()->getId();
  69.             $page->setNavigationId($navigationId);
  70.         }
  71.         $metaInformation $page->getMetaInformation();
  72.         if ($metaInformation instanceof MetaInformation) {
  73.             $metaTitle $blogEntry->getTranslation('metaTitle') ?? $blogEntry->getTitle();
  74.             $metaDescription $blogEntry->getTranslation('metaDescription') ?? $blogEntry->getTeaser();
  75.             $metaAuthor $blogEntry->getBlogAuthor() ? $blogEntry->getBlogAuthor()->getFullName() : '';
  76.             $metaKeywords $blogEntry->getTranslation('metaKeywords');
  77.             $metaInformation->setMetaKeywords($metaKeywords ?? '');
  78.             $metaInformation->setMetaTitle($metaTitle ?? '');
  79.             $metaInformation->setMetaDescription($metaDescription ?? '');
  80.             $metaInformation->setAuthor($metaAuthor ?? '');
  81.             $page->setMetaInformation($metaInformation);
  82.         }
  83.         $this->eventDispatcher->dispatch(new BlogPageLoadedEvent($page$context$request));
  84.         return $page;
  85.     }
  86.     /**
  87.      * Loads the Blog Entry for the given article id
  88.      * It creates a criteria with the given article id
  89.      *   then associates the author's salutation and blog categories
  90.      * It dispatches an event to allow other extensions to modify the criteria
  91.      * It gets and returns the Blog Entry's instance for the given criteria
  92.      *
  93.      * @throws PageNotFoundException
  94.      */
  95.     private function loadBlogEntry(string $articleIdSalesChannelContext $context): BlogEntriesEntity
  96.     {
  97.         $criteria = (new Criteria([$articleId]))
  98.             ->addAssociation('blogAuthor.salutation')
  99.             ->addAssociation('media')
  100.             ->addAssociation('blogCategories');
  101.         $this->eventDispatcher->dispatch(new BlogPageCriteriaEvent($articleId$criteria$context));
  102.         $blogEntry $this->blogRepository
  103.             ->search($criteria$context->getContext())
  104.             ->first();
  105.         if (!$blogEntry instanceof BlogEntriesEntity) {
  106.             throw new PageNotFoundException($articleId);
  107.         }
  108.         return $blogEntry;
  109.     }
  110.     /**
  111.      * Loads the CMS Page for the blog detail page
  112.      * It gets the CMS Page's id from the plugin configuration
  113.      * It gets and returns the CMS Page's instance for the given id
  114.      *
  115.      * @throws PageNotFoundException
  116.      * @throws ConfigurationNotFoundException
  117.      */
  118.     private function loadBlogDetailCmsPage(Request $requestSalesChannelContext $context): CmsPageEntity
  119.     {
  120.         $detailCmsPageId $this->systemConfigService->getString('SasBlogModule.config.cmsBlogDetailPage');
  121.         if (!$detailCmsPageId) {
  122.             throw new ConfigurationNotFoundException('SasBlogModule');
  123.         }
  124.         $detailCmsPage $this->cmsPageLoader->load($request, new Criteria([$detailCmsPageId]), $context)->first();
  125.         if (!$detailCmsPage instanceof CmsPageEntity) {
  126.             throw new PageNotFoundException($detailCmsPageId);
  127.         }
  128.         return $detailCmsPage;
  129.     }
  130. }