Skip to content

Commit 0bb1e59

Browse files
devdev
authored andcommitted
Max. depth for sidebar categories, ability to display featured posts on blog home page,
1 parent 80cc335 commit 0bb1e59

File tree

8 files changed

+154
-20
lines changed

8 files changed

+154
-20
lines changed

Block/Index.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,29 @@ protected function _prepareLayout()
3535
return parent::_prepareLayout();
3636
}
3737

38+
/**
39+
* Prepare posts collection
40+
*
41+
* @return void
42+
*/
43+
protected function _preparePostCollection()
44+
{
45+
parent::_preparePostCollection();
46+
47+
$displayMode = $this->_scopeConfig->getValue(
48+
\Magefan\Blog\Helper\Config::XML_PATH_HOMEPAGE_DISPLAY_MODE,
49+
ScopeInterface::SCOPE_STORE
50+
);
51+
/* If featured posts enabled */
52+
if ($displayMode == 1) {
53+
$postIds = $this->_scopeConfig->getValue(
54+
\Magefan\Blog\Helper\Config::XML_PATH_HOMEPAGE_FEATURED_POST_IDS,
55+
ScopeInterface::SCOPE_STORE
56+
);
57+
$this->_postCollection->addPostsFilter($postIds);
58+
}
59+
}
60+
3861
/**
3962
* Retrieve blog title
4063
* @return string

Block/Sidebar/Categories.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ public function getGroupedChilds()
5656
->setOrder('position')
5757
->getTreeOrderedArray();
5858

59+
foreach ($array as $key => $item) {
60+
$maxDepth = $this->maxDepth();
61+
if ($maxDepth > 0 && $item->getLevel() >= $maxDepth) {
62+
unset($array[$key]);
63+
}
64+
}
65+
5966
$this->setData($k, $array);
6067
}
6168

@@ -86,4 +93,18 @@ public function getIdentities()
8693
{
8794
return [\Magento\Cms\Model\Block::CACHE_TAG . '_blog_categories_widget' ];
8895
}
96+
97+
98+
/**
99+
* Retrieve categories maximum depth
100+
* @return int
101+
*/
102+
public function maxDepth()
103+
{
104+
$maxDepth = $this->_scopeConfig->getValue(
105+
'mfblog/sidebar/'.$this->_widgetKey.'/max_depth', ScopeInterface::SCOPE_STORE
106+
);
107+
108+
return (int)$maxDepth;
109+
}
89110
}

Block/Sidebar/Featured.php

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,8 @@ class Featured extends \Magefan\Blog\Block\Post\PostList\AbstractList
2828
protected function _preparePostCollection()
2929
{
3030
parent::_preparePostCollection();
31-
32-
$postIds = $this->getPostIdsConfigValue();
33-
$postIds = explode(',', $postIds);
34-
foreach ($postIds as $key => $id) {
35-
$id = trim($id);
36-
if (!$id) {
37-
unset($postIds[$key]);
38-
}
39-
}
40-
41-
if (!count($postIds)) {
42-
$postIds = [0];
43-
}
44-
45-
$this->_postCollection->addFieldToFilter(
46-
'post_id',
47-
['in' => $postIds]
31+
$this->_postCollection->addPostsFilter(
32+
$this->getPostIdsConfigValue()
4833
);
4934
}
5035

Helper/Config.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ class Config extends \Magento\Framework\App\Helper\AbstractHelper
2929
*/
3030
const XML_PATH_TOP_MENU_SHOW_ITEM = 'mfblog/top_menu/show_item';
3131

32+
/**
33+
* Blog homepage display mode
34+
*/
35+
const XML_PATH_HOMEPAGE_DISPLAY_MODE = 'mfblog/index_page/display_mode';
36+
37+
/**
38+
* Blog homepage featured post ids
39+
*/
40+
const XML_PATH_HOMEPAGE_FEATURED_POST_IDS = 'mfblog/index_page/post_ids';
41+
3242
/**
3343
* Top menu item text config path
3444
*/

Model/Config/Source/DisplayMode.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
* Copyright © 2015-17 Magefan ([email protected]). All rights reserved.
4+
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
5+
*
6+
* Glory to Ukraine! Glory to the heroes!
7+
*/
8+
9+
namespace Magefan\Blog\Model\Config\Source;
10+
11+
/**
12+
* Comment statuses
13+
*/
14+
class DisplayMode implements \Magento\Framework\Option\ArrayInterface
15+
{
16+
/**
17+
* @const string
18+
*/
19+
const PENDING = 0;
20+
21+
/**
22+
* @const int
23+
*/
24+
const APPROVED = 1;
25+
26+
/**
27+
* Options int
28+
*
29+
* @return array
30+
*/
31+
public function toOptionArray()
32+
{
33+
return [
34+
['value' => self::PENDING, 'label' => __('Recent Blog Posts')],
35+
['value' => self::APPROVED, 'label' => __('Featured Blog Posts')],
36+
];
37+
}
38+
39+
/**
40+
* Get options in "key-value" format
41+
*
42+
* @return array
43+
*/
44+
public function toArray()
45+
{
46+
$array = [];
47+
foreach ($this->toOptionArray() as $item) {
48+
$array[$item['value']] = $item['label'];
49+
}
50+
return $array;
51+
}
52+
}

Model/ResourceModel/Post/Collection.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,33 @@ public function addStoreFilter($store, $withAdmin = true)
131131
return $this;
132132
}
133133

134+
/**
135+
* Add posts filter to collection
136+
* @param array|int|string $category
137+
* @return $this
138+
*/
139+
public function addPostsFilter($postIds)
140+
{
141+
if (!is_array($postIds)) {
142+
$postIds = explode(',', $postIds);
143+
foreach ($postIds as $key => $id) {
144+
$id = trim($id);
145+
if (!$id) {
146+
unset($postIds[$key]);
147+
}
148+
}
149+
}
150+
151+
if (!count($postIds)) {
152+
$postIds = [0];
153+
}
154+
155+
$this->addFieldToFilter(
156+
'post_id',
157+
['in' => $postIds]
158+
);
159+
}
160+
134161
/**
135162
* Add category filter to collection
136163
* @param array|int|\Magefan\Blog\Model\Category $category

etc/adminhtml/system.xml

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,21 @@
3434
<field id="title" translate="label comment" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
3535
<label>Title</label>
3636
</field>
37-
<field id="meta_keywords" translate="label comment" type="textarea" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
37+
<field id="display_mode" translate="label comment" type="select" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
38+
<label>Display Mode</label>
39+
<source_model>Magefan\Blog\Model\Config\Source\DisplayMode</source_model>
40+
</field>
41+
<field id="post_ids" translate="label comment" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
42+
<label>Post IDs</label>
43+
<depends>
44+
<field id="display_mode">1</field>
45+
</depends>
46+
<comment>Please indicate post IDs (separated by comma) you want to display.</comment>
47+
</field>
48+
<field id="meta_keywords" translate="label comment" type="textarea" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1">
3849
<label>Meta Keywords</label>
3950
</field>
40-
<field id="meta_description" translate="label comment" type="textarea" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
51+
<field id="meta_description" translate="label comment" type="textarea" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="1">
4152
<label>Meta Description</label>
4253
</field>
4354
</group>
@@ -207,7 +218,10 @@
207218
<label>Display Number Of Posts</label>
208219
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
209220
</field>
210-
<field id="sort_order" translate="label comment" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
221+
<field id="max_depth" type="text" translate="label" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
222+
<label>Maximal Depth</label>
223+
</field>
224+
<field id="sort_order" translate="label comment" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
211225
<label>Sort Order</label>
212226
</field>
213227
</group>

etc/config.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
</general>
1616
<index_page>
1717
<title>Blog</title>
18+
<display_mode>0></display_mode>
1819
</index_page>
1920
<post_view>
2021
<related_posts>
@@ -53,6 +54,7 @@
5354
<enabled>1</enabled>
5455
<show_posts_count>1</show_posts_count>
5556
<sort_order>20</sort_order>
57+
<max_depth>2</max_depth>
5658
</categories>
5759
<recent_posts>
5860
<enabled>1</enabled>

0 commit comments

Comments
 (0)