Skip to content

Commit

Permalink
feat: Add canonical to sitemap category page
Browse files Browse the repository at this point in the history
  • Loading branch information
kpitn committed Mar 26, 2024
1 parent 692ae87 commit 8fcbd8b
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions Controller/Sitemap/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}

/**
Expand All @@ -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;
}
}

0 comments on commit 8fcbd8b

Please sign in to comment.