Skip to content

Commit d747886

Browse files
authored
Merge pull request #639 from ivanhrytsaim/12550-Blog-Archive-Page-Meta-data
12550-Blog-Archive-Page-Meta-data
2 parents bcec444 + 17638b0 commit d747886

File tree

5 files changed

+159
-16
lines changed

5 files changed

+159
-16
lines changed

Block/Archive/Description.php

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
declare(strict_types=1);
10+
11+
namespace Magefan\Blog\Block\Archive;
12+
13+
use Magento\Framework\View\Element\Template;
14+
15+
/**
16+
* Blog index description
17+
*/
18+
class Description extends Template
19+
{
20+
/**
21+
* @var \Magento\Framework\Registry
22+
*/
23+
private $_coreRegistry;
24+
25+
/**
26+
* @param Template\Context $context
27+
* @param \Magento\Framework\Registry $coreRegistry
28+
* @param array $data
29+
*/
30+
public function __construct(
31+
Template\Context $context,
32+
\Magento\Framework\Registry $coreRegistry,
33+
array $data = []
34+
)
35+
{
36+
parent::__construct($context, $data);
37+
$this->_coreRegistry = $coreRegistry;
38+
}
39+
40+
/**
41+
* @return string
42+
*/
43+
public function getDescription(): string
44+
{
45+
$description = $this->_scopeConfig->getValue(
46+
'mfblog/archive/description',
47+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
48+
);
49+
50+
if (!$description) {
51+
return '';
52+
}
53+
54+
$vars = ['year', 'month'];
55+
$values = [];
56+
57+
foreach ($vars as $var) {
58+
$schemaVar = '{{' . $var . '}}';
59+
if (strpos($description, $schemaVar) !== false) {
60+
switch ($var) {
61+
case 'year':
62+
$values[$var] = date('Y', strtotime((int)$this->_coreRegistry->registry('current_blog_archive_year') . '-01-01'));
63+
break;
64+
case 'month':
65+
$data = (int)$this->_coreRegistry->registry('current_blog_archive_year') . '-' . (int)$this->_coreRegistry->registry('current_blog_archive_month') . '-01';
66+
$values[$var] = date('F', strtotime($data));
67+
break;
68+
}
69+
$description = str_replace($schemaVar, $values[$var] ?? '', $description);
70+
}
71+
72+
}
73+
return (string)$description;
74+
}
75+
}

Block/Archive/PostList.php

+48-16
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ protected function _preparePostCollection()
3131

3232
/**
3333
* Get archive month
34-
* @return string
34+
* @return int
3535
*/
3636
public function getMonth()
3737
{
@@ -40,7 +40,7 @@ public function getMonth()
4040

4141
/**
4242
* Get archive year
43-
* @return string
43+
* @return int
4444
*/
4545
public function getYear()
4646
{
@@ -58,6 +58,9 @@ protected function _prepareLayout()
5858
$this->_addBreadcrumbs($title, 'blog_search');
5959
$this->pageConfig->getTitle()->set($title);
6060

61+
$this->pageConfig->setKeywords($this->replaceVars($this->_getConfigValue('meta_keywords')));
62+
$this->pageConfig->setDescription($this->replaceVars($this->_getConfigValue('meta_description')));
63+
6164
if ($this->config->getDisplayCanonicalTag(\Magefan\Blog\Model\Config::CANONICAL_PAGE_TYPE_ARCHIVE)) {
6265
$month = '';
6366
if ($this->getMonth()) {
@@ -79,7 +82,7 @@ protected function _prepareLayout()
7982
['attributes' => ['rel' => 'canonical']]
8083
);
8184
}
82-
$this->pageConfig->setRobots('NOINDEX,FOLLOW');
85+
$this->pageConfig->setRobots($this->_getConfigValue('robots'));
8386

8487
$pageMainTitle = $this->getLayout()->getBlock('page.main.title');
8588
if ($pageMainTitle) {
@@ -97,19 +100,48 @@ protected function _prepareLayout()
97100
*/
98101
protected function _getTitle()
99102
{
100-
if ($this->getMonth()) {
101-
$time = strtotime($this->getYear() . '-' . $this->getMonth() . '-01');
102-
return sprintf(
103-
__('Monthly Archives: %s %s'),
104-
__(date('F', $time)),
105-
date('Y', $time)
106-
);
107-
} else {
108-
$time = strtotime($this->getYear() . '-01-01');
109-
return sprintf(
110-
__('Yearly Archives: %s'),
111-
date('Y', $time)
112-
);
103+
return (string)$this->replaceVars($this->_getConfigValue('meta_title') ?: $this->_getConfigValue('title'));
104+
}
105+
106+
107+
/**
108+
* @param $param
109+
* @return mixed
110+
*/
111+
protected function _getConfigValue($param)
112+
{
113+
return $this->_scopeConfig->getValue(
114+
'mfblog/archive/'.$param,
115+
ScopeInterface::SCOPE_STORE
116+
);
117+
}
118+
119+
/**
120+
* @param $content
121+
* @return array|mixed|string|string[]
122+
*/
123+
private function replaceVars($content)
124+
{
125+
if (!$content) {
126+
return '';
127+
}
128+
$vars = ['year', 'month'];
129+
$values = [];
130+
foreach ($vars as $var) {
131+
$schemaVar = '{{' . $var . '}}';
132+
if ($content && strpos($content, $schemaVar) !== false) {
133+
switch ($var) {
134+
case 'year':
135+
$values[$var] = date('Y', strtotime($this->getYear() . '-01-01'));
136+
break;
137+
case 'month':
138+
$values[$var] = date('F', strtotime($this->getYear() . '-' . $this->getMonth() . '-01'));
139+
break;
140+
}
141+
$content = str_replace($schemaVar, $values[$var] ?? '', $content);
142+
}
143+
113144
}
145+
return $content;
114146
}
115147
}

etc/adminhtml/system.xml

+27
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,33 @@
539539
</depends>
540540
</field>
541541
</group>
542+
<group id="archive" translate="label" type="text" sortOrder="66" showInDefault="1" showInWebsite="1" showInStore="1">
543+
<label>Archive</label>
544+
<field id="title" translate="label comment" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
545+
<label>Title</label>
546+
<comment><![CDATA[Available variables: {{month}}, {{year}}]]></comment>
547+
</field>
548+
<field id="description" translate="label comment" type="textarea" sortOrder="15" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
549+
<label>Description</label>
550+
<comment><![CDATA[Available variables: {{month}}, {{year}}]]></comment>
551+
</field>
552+
<field id="meta_title" translate="label comment" type="text" sortOrder="35" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
553+
<label>Meta Title</label>
554+
<comment><![CDATA[Available variables: {{month}}, {{year}}]]></comment>
555+
</field>
556+
<field id="meta_keywords" translate="label comment" type="textarea" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
557+
<label>Meta Keywords</label>
558+
<comment><![CDATA[Enter words separated by comma. Available variables: {{month}}, {{year}}]]></comment>
559+
</field>
560+
<field id="meta_description" translate="label comment" type="textarea" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
561+
<label>Meta Description</label>
562+
<comment><![CDATA[Available variables: {{month}}, {{year}}]]></comment>
563+
</field>
564+
<field id="robots" translate="label comment" type="select" sortOrder="60" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
565+
<label>Default Robots</label>
566+
<source_model>Magento\Config\Model\Config\Source\Design\Robots</source_model>
567+
</field>
568+
</group>
542569
<group id="tag" translate="label" type="text" sortOrder="67" showInDefault="1" showInWebsite="1" showInStore="1">
543570
<label>Tag</label>
544571
<field id="robots" translate="label comment" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">

etc/config.xml

+8
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,14 @@
112112
<lazyload_enabled>0</lazyload_enabled>
113113
<lazyload_padding>200</lazyload_padding>
114114
</post_list>
115+
<archive>
116+
<title>Monthly Archives: {{month}} {{year}}</title>
117+
<description/>
118+
<meta_title/>
119+
<meta_keywords/>
120+
<meta_description/>
121+
<robots>NOINDEX,FOLLOW</robots>
122+
</archive>
115123
<tag>
116124
<robots>INDEX,FOLLOW</robots>
117125
</tag>

view/frontend/layout/blog_archive_view.xml

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<update handle="blog_post_list"/>
1313
<body>
1414
<referenceContainer name="content">
15+
<block class="Magefan\Blog\Block\Archive\Description" name="blog.posts.description" template="Magefan_Blog::index/description.phtml" before="blog.posts.list" />
1516
<block class="Magefan\Blog\Block\Archive\PostList" name="blog.posts.list" template="post/list.phtml" />
1617
</referenceContainer>
1718
</body>

0 commit comments

Comments
 (0)