Skip to content

Commit 1756734

Browse files
committed
Fix canonical for pagination, rel prev,next
1 parent 0b4d00f commit 1756734

File tree

11 files changed

+189
-23
lines changed

11 files changed

+189
-23
lines changed

Block/Archive/PostList.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace Magefan\Blog\Block\Archive;
1010

11+
use Magefan\Blog\Block\Post\PostList\Toolbar;
1112
use Magento\Store\Model\ScopeInterface;
1213

1314
/**
@@ -58,11 +59,19 @@ protected function _prepareLayout()
5859
$this->pageConfig->getTitle()->set($title);
5960

6061
if ($this->config->getDisplayCanonicalTag(\Magefan\Blog\Model\Config::CANONICAL_PAGE_TYPE_ARCHIVE)) {
62+
63+
$canonicalUrl = $this->_url->getUrl(
64+
$this->getYear() . '-' . str_pad($this->getMonth(), 2, '0', STR_PAD_LEFT),
65+
\Magefan\Blog\Model\Url::CONTROLLER_ARCHIVE
66+
);
67+
$page = (int)$this->_request->getParam(Toolbar::PAGE_PARM_NAME);
68+
if ($page > 1) {
69+
$canonicalUrl .= ((false === strpos($canonicalUrl, '?')) ? '?' : '&')
70+
. Toolbar::PAGE_PARM_NAME . '=' . $page;
71+
}
72+
6173
$this->pageConfig->addRemotePageAsset(
62-
$this->_url->getUrl(
63-
$this->getYear() . '-' . str_pad($this->getMonth(), 2, '0', STR_PAD_LEFT),
64-
\Magefan\Blog\Model\Url::CONTROLLER_ARCHIVE
65-
),
74+
$canonicalUrl,
6675
'canonical',
6776
['attributes' => ['rel' => 'canonical']]
6877
);

Block/Author/PostList.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace Magefan\Blog\Block\Author;
1010

11+
use Magefan\Blog\Block\Post\PostList\Toolbar;
1112
use Magento\Store\Model\ScopeInterface;
1213

1314
/**
@@ -53,8 +54,16 @@ protected function _prepareLayout()
5354
$this->pageConfig->setDescription($author->getMetaDescription());
5455

5556
if ($this->config->getDisplayCanonicalTag(\Magefan\Blog\Model\Config::CANONICAL_PAGE_TYPE_AUTHOR)) {
57+
58+
$canonicalUrl = $author->getAuthorUrl();
59+
$page = (int)$this->_request->getParam(Toolbar::PAGE_PARM_NAME);
60+
if ($page > 1) {
61+
$canonicalUrl .= ((false === strpos($canonicalUrl, '?')) ? '?' : '&')
62+
. Toolbar::PAGE_PARM_NAME . '=' . $page;
63+
}
64+
5665
$this->pageConfig->addRemotePageAsset(
57-
$author->getAuthorUrl(),
66+
$canonicalUrl,
5867
'canonical',
5968
['attributes' => ['rel' => 'canonical']]
6069
);

Block/Category/PostList.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Framework\Api\SortOrder;
1212
use Magefan\Blog\Model\Config\Source\CategoryDisplayMode;
1313
use Magefan\Blog\Model\Config\Source\PostsSortBy;
14+
use Magefan\Blog\Block\Post\PostList\Toolbar;
1415

1516
/**
1617
* Blog category posts list
@@ -115,8 +116,16 @@ protected function _prepareLayout()
115116
$this->pageConfig->setDescription($category->getMetaDescription());
116117

117118
if ($this->config->getDisplayCanonicalTag(\Magefan\Blog\Model\Config::CANONICAL_PAGE_TYPE_CATEGORY)) {
119+
120+
$canonicalUrl = $category->getCanonicalUrl();
121+
$page = (int)$this->_request->getParam(Toolbar::PAGE_PARM_NAME);
122+
if ($page > 1) {
123+
$canonicalUrl .= ((false === strpos($canonicalUrl, '?')) ? '?' : '&')
124+
. Toolbar::PAGE_PARM_NAME . '=' . $page;
125+
}
126+
118127
$this->pageConfig->addRemotePageAsset(
119-
$category->getCanonicalUrl(),
128+
$canonicalUrl,
120129
'canonical',
121130
['attributes' => ['rel' => 'canonical']]
122131
);

Block/Category/View.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace Magefan\Blog\Block\Category;
1010

11+
use Magefan\Blog\Block\Post\PostList\Toolbar;
1112
use Magento\Store\Model\ScopeInterface;
1213

1314
/**
@@ -56,8 +57,16 @@ protected function _prepareLayout()
5657
$this->pageConfig->setDescription($category->getMetaDescription());
5758

5859
if ($this->config->getDisplayCanonicalTag(\Magefan\Blog\Model\Config::CANONICAL_PAGE_TYPE_CATEGORY)) {
60+
61+
$canonicalUrl = $category->getCanonicalUrl();
62+
$page = (int)$this->_request->getParam(Toolbar::PAGE_PARM_NAME);
63+
if ($page > 1) {
64+
$canonicalUrl .= ((false === strpos($canonicalUrl, '?')) ? '?' : '&')
65+
. Toolbar::PAGE_PARM_NAME . '=' . $page;
66+
}
67+
5968
$this->pageConfig->addRemotePageAsset(
60-
$category->getCanonicalUrl(),
69+
$canonicalUrl,
6170
'canonical',
6271
['attributes' => ['rel' => 'canonical']]
6372
);

Block/Index.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Framework\Api\SortOrder;
1212
use Magento\Store\Model\ScopeInterface;
1313
use Magefan\Blog\Model\Config\Source\PostsSortBy;
14+
use Magefan\Blog\Block\Post\PostList\Toolbar;
1415

1516
/**
1617
* Blog index block
@@ -32,8 +33,16 @@ protected function _prepareLayout()
3233
$this->pageConfig->setDescription($this->_getConfigValue('meta_description'));
3334

3435
if ($this->config->getDisplayCanonicalTag(\Magefan\Blog\Model\Config::CANONICAL_PAGE_TYPE_INDEX)) {
36+
37+
$canonicalUrl = $this->_url->getBaseUrl();
38+
$page = (int)$this->_request->getParam(Toolbar::PAGE_PARM_NAME);
39+
if ($page > 1) {
40+
$canonicalUrl .= ((false === strpos($canonicalUrl, '?')) ? '?' : '&')
41+
. Toolbar::PAGE_PARM_NAME . '=' . $page;
42+
}
43+
3544
$this->pageConfig->addRemotePageAsset(
36-
$this->_url->getBaseUrl(),
45+
$canonicalUrl,
3746
'canonical',
3847
['attributes' => ['rel' => 'canonical']]
3948
);

Block/Post/PostList.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ class PostList extends \Magefan\Blog\Block\Post\PostList\AbstractList
2121
*/
2222
protected $_defaultToolbarBlock = \Magefan\Blog\Block\Post\PostList\Toolbar::class;
2323

24+
/**
25+
* @var
26+
*/
27+
protected $toolbarBlock;
28+
2429
/**
2530
* Preparing global layout
2631
*
@@ -33,7 +38,7 @@ protected function _prepareLayout()
3338
);
3439

3540
if ($page > 1) {
36-
$this->pageConfig->setRobots('NOINDEX,FOLLOW');
41+
//$this->pageConfig->setRobots('NOINDEX,FOLLOW');
3742

3843
$title = $this->pageConfig->getTitle()->getShortHeading() . ' - ' . (__('Page') . ' ' . $page);
3944
$this->pageConfig->getTitle()->set($title);
@@ -85,16 +90,21 @@ protected function getPostTemplateType()
8590
*/
8691
public function getToolbarBlock()
8792
{
88-
$blockName = $this->getToolbarBlockName();
89-
90-
if ($blockName) {
91-
$block = $this->getLayout()->getBlock($blockName);
92-
if ($block) {
93-
return $block;
93+
if (null === $this->toolbarBlock) {
94+
$blockName = $this->getToolbarBlockName();
95+
96+
if ($blockName) {
97+
$block = $this->getLayout()->getBlock($blockName);
98+
if ($block) {
99+
$this->toolbarBlock = $block;
100+
}
101+
}
102+
if (!$this->toolbarBlock) {
103+
$this->toolbarBlock = $this->getLayout()->createBlock($this->_defaultToolbarBlock, uniqid(microtime()));
94104
}
95105
}
96-
$block = $this->getLayout()->createBlock($this->_defaultToolbarBlock, uniqid(microtime()));
97-
return $block;
106+
107+
return $this->toolbarBlock;
98108
}
99109

100110
/**

Block/Post/PostList/Toolbar.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,9 @@ public function getCurrentPage()
8989
}
9090

9191
/**
92-
* Render pagination HTML
93-
*
94-
* @return string
92+
* @return bool|\Magento\Framework\DataObject|\Magento\Framework\View\Element\AbstractBlock|\Magento\Theme\Block\Html\Pager
9593
*/
96-
public function getPagerHtml()
94+
public function getPagerBlock()
9795
{
9896
$pagerBlock = $this->getChildBlock('post_list_toolbar_pager');
9997
if ($pagerBlock instanceof \Magento\Framework\DataObject) {
@@ -106,7 +104,7 @@ public function getPagerHtml()
106104
)->setShowAmounts(
107105
false
108106
)->setPageVarName(
109-
'page'
107+
self::PAGE_PARM_NAME
110108
)->setFrameLength(
111109
$this->_scopeConfig->getValue(
112110
'design/pagination/pagination_frame',
@@ -122,6 +120,23 @@ public function getPagerHtml()
122120
)->setCollection(
123121
$this->getCollection()
124122
);
123+
} else {
124+
$pagerBlock = false;
125+
}
126+
127+
128+
return $pagerBlock;
129+
}
130+
131+
/**
132+
* Render pagination HTML
133+
*
134+
* @return string
135+
*/
136+
public function getPagerHtml()
137+
{
138+
$pagerBlock = $this->getPagerBlock();
139+
if ($pagerBlock instanceof \Magento\Framework\DataObject) {
125140
return $pagerBlock->toHtml();
126141
}
127142

Block/Search/PostList.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ protected function _prepareLayout()
6868
$this->pageConfig->setRobots($robots);
6969
}
7070

71+
if ($page > 1) {
72+
$this->pageConfig->setRobots('NOINDEX,FOLLOW');
73+
}
74+
7175
$pageMainTitle = $this->getLayout()->getBlock('page.main.title');
7276
if ($pageMainTitle) {
7377
$pageMainTitle->setPageTitle(

Block/Tag/PostList.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace Magefan\Blog\Block\Tag;
1010

11+
use Magefan\Blog\Block\Post\PostList\Toolbar;
1112
use Magento\Store\Model\ScopeInterface;
1213

1314
/**
@@ -64,8 +65,16 @@ protected function _prepareLayout()
6465
}
6566

6667
if ($this->config->getDisplayCanonicalTag(\Magefan\Blog\Model\Config::CANONICAL_PAGE_TYPE_TAG)) {
68+
69+
$canonicalUrl = $tag->getTagUrl();
70+
$page = (int)$this->_request->getParam(Toolbar::PAGE_PARM_NAME);
71+
if ($page > 1) {
72+
$canonicalUrl .= ((false === strpos($canonicalUrl, '?')) ? '?' : '&')
73+
. Toolbar::PAGE_PARM_NAME . '=' . $page;
74+
}
75+
6776
$this->pageConfig->addRemotePageAsset(
68-
$tag->getTagUrl(),
77+
$canonicalUrl,
6978
'canonical',
7079
['attributes' => ['rel' => 'canonical']]
7180
);
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
/**
3+
* Copyright © Magefan ([email protected]). All rights reserved.
4+
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
5+
*
6+
* Glory to Ukraine! Glory to the heroes!
7+
*/
8+
9+
namespace Magefan\Blog\Observer;
10+
11+
use Magento\Framework\Event\ObserverInterface;
12+
13+
class LayoutGenerateBlocksAfter implements ObserverInterface
14+
{
15+
/**
16+
* @var \Magento\Framework\View\Page\Config
17+
*/
18+
private $pageConfig;
19+
20+
/**
21+
* @param \Magento\Framework\View\Page\Config $pageConfig
22+
*/
23+
public function __construct(
24+
\Magento\Framework\View\Page\Config $pageConfig
25+
) {
26+
$this->pageConfig = $pageConfig;
27+
}
28+
29+
/**
30+
* Add rel prev and rel next
31+
* @param \Magento\Framework\Event\Observer $observer
32+
* @return $this|void
33+
*/
34+
public function execute(\Magento\Framework\Event\Observer $observer)
35+
{
36+
$availableActions = [
37+
'blog_archive_view',
38+
'blog_author_view',
39+
'blog_category_view',
40+
'blog_index_index',
41+
'blog_tag_view'
42+
];
43+
if (!in_array($observer->getEvent()->getFullActionName(), $availableActions)) {
44+
return;
45+
}
46+
47+
$productListBlock = $observer->getEvent()->getLayout()->getBlock('blog.posts.list');
48+
if (!$productListBlock) {
49+
return;
50+
}
51+
52+
$toolbar = $productListBlock->getToolbarBlock();
53+
$toolbar->setCollection($productListBlock->getPostCollection());
54+
55+
$pagerBlock = $toolbar->getPagerBlock();
56+
if (!($pagerBlock instanceof \Magento\Framework\DataObject)) {
57+
return;
58+
}
59+
60+
if (1 < $pagerBlock->getCurrentPage()) {
61+
$this->pageConfig->addRemotePageAsset(
62+
$pagerBlock->getPageUrl(
63+
$pagerBlock->getCollection()->getCurPage(-1)
64+
),
65+
'link_rel',
66+
['attributes' => ['rel' => 'prev']]
67+
);
68+
}
69+
if ($pagerBlock->getCurrentPage() < $pagerBlock->getLastPageNum()) {
70+
$this->pageConfig->addRemotePageAsset(
71+
$pagerBlock->getPageUrl(
72+
$pagerBlock->getCollection()->getCurPage(+1)
73+
),
74+
'link_rel',
75+
['attributes' => ['rel' => 'next']]
76+
);
77+
}
78+
79+
}
80+
}

0 commit comments

Comments
 (0)