From 8fcbd8bf8ad863d70a2673316d50ecfc4ff335b3 Mon Sep 17 00:00:00 2001 From: kpitn Date: Tue, 26 Mar 2024 17:12:54 +0100 Subject: [PATCH] feat: Add canonical to sitemap category page --- Controller/Sitemap/Category.php | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/Controller/Sitemap/Category.php b/Controller/Sitemap/Category.php index 308676a..bf21900 100644 --- a/Controller/Sitemap/Category.php +++ b/Controller/Sitemap/Category.php @@ -8,6 +8,8 @@ use Magento\Framework\App\Action\Context; use Magento\Framework\App\ResponseInterface; use Magento\Framework\Controller\ResultInterface; +use Magento\Framework\UrlInterface; +use Magento\Framework\View\Page\Config as PageConfig; use Magento\Framework\View\Result\Page; use Magento\Framework\View\Result\PageFactory; @@ -28,20 +30,32 @@ class Category extends Action * @var PageFactory $pageFactory */ protected $pageFactory; + /** + * Page config + * + * @var PageConfig $pageConfig + */ + protected $pageConfig; /** * Category constructor. * - * @param Context $context - * @param PageFactory $pageFactory + * @param Context $context + * @param PageFactory $pageFactory + * @param PageConfig $pageConfig + * @param UrlInterface $urlBuilder */ public function __construct( Context $context, - PageFactory $pageFactory + PageFactory $pageFactory, + PageConfig $pageConfig, + UrlInterface $urlBuilder ) { parent::__construct($context); $this->pageFactory = $pageFactory; + $this->pageConfig = $pageConfig; + $this->urlBuilder = $urlBuilder; } /** @@ -53,6 +67,14 @@ public function execute() $pageFactory = $this->pageFactory->create(); $pageFactory->getConfig()->getTitle()->prepend(__('Sitemap')); + if (!$this->pageConfig->getAssetCollection()->getGroupByContentType('canonical')) { + $this->pageConfig->addRemotePageAsset( + $this->urlBuilder->getCurrentUrl(), + 'canonical', + ['attributes' => ['rel' => 'canonical']] + ); + } + return $pageFactory; } }