custom/plugins/NetzpBlog6/src/Storefront/Page/BlogPageLoader.php line 65

  1. <?php declare(strict_types=1);
  2. namespace NetzpBlog6\Storefront\Page;
  3. use NetzpBlog6\Controller\StoreApi\BlogPost\AbstractBlogPostRoute;
  4. use NetzpBlog6\Core\Content\Blog\BlogEntity;
  5. use Psr\EventDispatcher\EventDispatcherInterface;
  6. use Shopware\Core\Content\Cms\SalesChannel\SalesChannelCmsPageLoaderInterface;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  9. use Shopware\Core\System\SystemConfig\SystemConfigService;
  10. use Shopware\Storefront\Page\GenericPageLoaderInterface;
  11. use Shopware\Storefront\Page\MetaInformation;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  14. class BlogPageLoader
  15. {
  16.     public function __construct(private readonly GenericPageLoaderInterface $genericPageLoader,
  17.                                 private readonly SalesChannelCmsPageLoaderInterface $cmsPageLoader,
  18.                                 private readonly EventDispatcherInterface $eventDispatcher,
  19.                                 private readonly AbstractBlogPostRoute $blogRoute,
  20.                                 private readonly SystemConfigService $config
  21.     )
  22.     {
  23.     }
  24.     public function load(Request $requestSalesChannelContext $contextstring $postId): BlogPage
  25.     {
  26.         $criteria = new Criteria([$postId]);
  27.         $post $this->blogRoute->load($postId$criteria$context)->getBlogPost();
  28.         if(! $post) {
  29.             throw new NotFoundHttpException('Blog post not found');
  30.         }
  31.         $config $this->config->get('NetzpBlog6.config'$context->getSalesChannelId());
  32.         $category $post->getCategory();
  33.         if($category && $category->getCmspageid()) {
  34.             $cmsPageId $category->getCmspageid();
  35.         }
  36.         else {
  37.             $cmsPageId array_key_exists('cmspage'$config) && $config['cmspage'] !== '' $config['cmspage'] : null;
  38.         }
  39.         if($cmsPageId !== null || $cmsPageId != '') {
  40.             $page $this->loadCmsPage($request$context$cmsPageId$post);
  41.         }
  42.         else {
  43.             $page $this->loadNormalPage($request$context$post);
  44.         }
  45.         $page->setPost($post);
  46.         $this->eventDispatcher->dispatch(
  47.             new BlogPageLoadedEvent($page$context$request)
  48.         );
  49.         return $page;
  50.     }
  51.     private function loadCmsPage(Request $request,
  52.                                  SalesChannelContext $salesChannelContext,
  53.                                  $cmsPageId,
  54.                                  BlogEntity $post)
  55.     {
  56.         $cmsPage $this->cmsPageLoader->load($request, new Criteria([$cmsPageId]), $salesChannelContext)->first();
  57.         $page BlogPage::createFrom($this->genericPageLoader->load($request$salesChannelContext));
  58.         $page->setCmsPage($cmsPage);
  59.         $page->setMetaInformation($this->getMetaInformation($post));
  60.         return $page;
  61.     }
  62.     private function loadNormalPage(Request $request,
  63.                                     SalesChannelContext $salesChannelContext,
  64.                                     BlogEntity $post)
  65.     {
  66.         $page $this->genericPageLoader->load($request$salesChannelContext);
  67.         $page BlogPage::createFrom($page);
  68.         $page->setMetaInformation($this->getMetaInformation($post));
  69.         return $page;
  70.     }
  71.     private function getMetaInformation(BlogEntity $post)
  72.     {
  73.         $meta = new MetaInformation();
  74.         $meta->setMetaTitle($post->getTranslation('metatitle') ?? $post->getTranslation('title'));
  75.         $meta->setMetaDescription($post->getTranslation('metadescription') ?? '');
  76.         if ($post->getAuthor()) {
  77.             $meta->setAuthor($post->getAuthor()->getTranslation('name') ?? '');
  78.         }
  79.         if ($post->getNoindex()) {
  80.             $meta->setRobots('noindex, nofollow');
  81.         }
  82.         return $meta;
  83.     }
  84. }