<?php declare(strict_types=1);
namespace MoorlMagazine\Storefront\Controller;
use MoorlMagazine\Storefront\Page\MagazineArticle\MagazineArticlePageLoader;
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Storefront\Controller\StorefrontController;
use Shopware\Storefront\Framework\Cache\Annotation\HttpCache;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route(defaults={"_routeScope"={"storefront"}})
*/
class MagazineArticleController extends StorefrontController
{
private MagazineArticlePageLoader $magazineArticlePageLoader;
public function __construct(
MagazineArticlePageLoader $magazineArticlePageLoader
) {
$this->magazineArticlePageLoader = $magazineArticlePageLoader;
}
/**
* @HttpCache()
* @Route("/moorl-magazine/article/{magazineArticleId}", name="moorl.magazine.article", methods={"GET"})
*/
public function index(SalesChannelContext $context, Request $request): Response
{
$page = $this->magazineArticlePageLoader->load($request, $context);
$magazineArticle = $page->getMagazineArticle();
/* If a CMS page for content is defined */
if ($magazineArticle->getContentCmsPageId() && $magazineArticle->hasExtension('contentCmsPage')) {
$contentCmsPage = $magazineArticle->getExtension('contentCmsPage');
$content = $this->renderView('@Storefront/storefront/page/content/detail.html.twig', [
'cmsPage' => $contentCmsPage
]);
$magazineArticle->addTranslated('content', $content);
$magazineArticle->setContent($content);
$magazineArticle->removeExtension('contentCmsPage');
$page = $this->magazineArticlePageLoader->reload($request, $page, $context);
}
if (!$page->getCmsPage()) {
return $this->renderStorefront('@Storefront/plugin/moorl-magazine/page/article.html.twig', [
'page' => $page
]);
}
return $this->renderStorefront('@Storefront/plugin/moorl-magazine/page/content/magazine-article-detail.html.twig', [
'page' => $page
]);
}
}