custom/plugins/SasBlogModule/src/Controller/BlogController.php line 52

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Sas\BlogModule\Controller;
  3. use Sas\BlogModule\Content\Blog\BlogEntriesCollection;
  4. use Sas\BlogModule\Content\Blog\BlogEntriesEntity;
  5. use Sas\BlogModule\Content\BlogAuthor\BlogAuthorEntity;
  6. use Shopware\Core\Content\Cms\Exception\PageNotFoundException;
  7. use Shopware\Core\Content\Cms\SalesChannel\SalesChannelCmsPageLoaderInterface;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  11. use Shopware\Core\System\SystemConfig\SystemConfigService;
  12. use Shopware\Storefront\Controller\StorefrontController;
  13. use Shopware\Storefront\Page\GenericPageLoaderInterface;
  14. use Shopware\Storefront\Page\MetaInformation;
  15. use Shopware\Storefront\Page\Navigation\NavigationPage;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. /**
  20.  * Blog detail page controller
  21.  *
  22.  * @Route(defaults={"_routeScope"={"storefront"}})
  23.  */
  24. class BlogController extends StorefrontController
  25. {
  26.     private SystemConfigService $systemConfigService;
  27.     private GenericPageLoaderInterface $genericPageLoader;
  28.     private SalesChannelCmsPageLoaderInterface $cmsPageLoader;
  29.     private EntityRepositoryInterface $blogRepository;
  30.     public function __construct(
  31.         SystemConfigService $systemConfigService,
  32.         GenericPageLoaderInterface $genericPageLoader,
  33.         SalesChannelCmsPageLoaderInterface $cmsPageLoader,
  34.         EntityRepositoryInterface $blogRepository
  35.     ) {
  36.         $this->systemConfigService $systemConfigService;
  37.         $this->genericPageLoader $genericPageLoader;
  38.         $this->cmsPageLoader $cmsPageLoader;
  39.         $this->blogRepository $blogRepository;
  40.     }
  41.     /**
  42.      * @Route("/sas_blog/{articleId}", name="sas.frontend.blog.detail", methods={"GET"})
  43.      */
  44.     public function detailAction(string $articleIdRequest $requestSalesChannelContext $context): Response
  45.     {
  46.         $page $this->genericPageLoader->load($request$context);
  47.         $page NavigationPage::createFrom($page);
  48.         $criteria = new Criteria([$articleId]);
  49.         $criteria->addAssociations(['blogAuthor.salutation''blogCategories']);
  50.         /** @var BlogEntriesCollection $results */
  51.         $results $this->blogRepository->search($criteria$context->getContext())->getEntities();
  52.         $cmsBlogDetailPageId $this->systemConfigService->get('SasBlogModule.config.cmsBlogDetailPage');
  53.         if (!\is_string($cmsBlogDetailPageId)) {
  54.             throw new PageNotFoundException($articleId);
  55.         }
  56.         if (!$results->first()) {
  57.             throw new PageNotFoundException($articleId);
  58.         }
  59.         $entry $results->first();
  60.         if (!$entry instanceof BlogEntriesEntity) {
  61.             throw new PageNotFoundException($articleId);
  62.         }
  63.         $pages $this->cmsPageLoader->load(
  64.             $request,
  65.             new Criteria([$cmsBlogDetailPageId]),
  66.             $context
  67.         );
  68.         $page->setCmsPage($pages->first());
  69.         $blogAuthor $entry->getBlogAuthor();
  70.         if (!$blogAuthor instanceof BlogAuthorEntity) {
  71.             throw new PageNotFoundException($articleId);
  72.         }
  73.         $metaInformation $page->getMetaInformation();
  74.         if ($metaInformation instanceof MetaInformation) {
  75.             $metaTitle $entry->getMetaTitle() ?? $entry->getTitle();
  76.             $metaDescription $entry->getMetaDescription() ?? $entry->getTeaser();
  77.             $metaAuthor $blogAuthor->getTranslated()['name'];
  78.             $metaInformation->setMetaTitle($metaTitle ?? '');
  79.             $metaInformation->setMetaDescription($metaDescription ?? '');
  80.             $metaInformation->setAuthor($metaAuthor ?? '');
  81.             $page->setMetaInformation($metaInformation);
  82.         }
  83.         $page->setNavigationId($page->getHeader()->getNavigation()->getActive()->getId());
  84.         return $this->renderStorefront('@Storefront/storefront/page/content/index.html.twig', [
  85.             'page' => $page,
  86.             'entry' => $entry,
  87.         ]);
  88.     }
  89. }