Skip to content

Commit 19bdf55

Browse files
committed
Allow to show posts count in Categories sidebar widget
1 parent 5e51f29 commit 19bdf55

File tree

5 files changed

+61
-5
lines changed

5 files changed

+61
-5
lines changed

Block/Sidebar/Categories.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright © 2015 Ihor Vansach ([email protected]). All rights reserved.
3+
* Copyright © 2017 Ihor Vansach ([email protected]). All rights reserved.
44
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
55
*
66
* Glory to Ukraine! Glory to the heroes!
@@ -62,6 +62,21 @@ public function getGroupedChilds()
6262
return $this->getData($k);
6363
}
6464

65+
/**
66+
* Retrieve true if need to show posts count
67+
* @return int
68+
*/
69+
public function showPostsCount()
70+
{
71+
$key = 'show_posts_count';
72+
if (!$this->hasData($key)) {
73+
$this->setData($key, (bool)$this->_scopeConfig->getValue(
74+
'mfblog/sidebar/'.$this->_widgetKey.'/show_posts_count', ScopeInterface::SCOPE_STORE
75+
));
76+
}
77+
return $this->getData($key);
78+
}
79+
6580

6681
/**
6782
* Retrieve block identities

Model/Category.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright © 2016 Ihor Vansach ([email protected]). All rights reserved.
3+
* Copyright © 2017 Ihor Vansach ([email protected]). All rights reserved.
44
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
55
*
66
* Glory to Ukraine! Glory to the heroes!
@@ -55,6 +55,11 @@ class Category extends \Magento\Framework\Model\AbstractModel
5555
*/
5656
protected $_url;
5757

58+
/**
59+
* @var \Magefan\Blog\Model\ResourceModel\Post\CollectionFactory
60+
*/
61+
protected $postCollectionFactory;
62+
5863
/**
5964
* Initialize dependencies.
6065
*
@@ -69,11 +74,13 @@ public function __construct(
6974
\Magento\Framework\Model\Context $context,
7075
\Magento\Framework\Registry $registry,
7176
Url $url,
77+
\Magefan\Blog\Model\ResourceModel\Post\CollectionFactory $postCollectionFactory,
7278
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
7379
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
7480
array $data = []
7581
) {
7682
$this->_url = $url;
83+
$this->postCollectionFactory = $postCollectionFactory;
7784
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
7885
}
7986

@@ -305,4 +312,30 @@ public function isVisibleOnStore($storeId)
305312
{
306313
return $this->getIsActive() && array_intersect([0, $storeId], $this->getStoreIds());
307314
}
315+
316+
/**
317+
* Retrieve number of posts in this category
318+
*
319+
* @return int
320+
*/
321+
public function getPostsCount()
322+
{
323+
$key = 'posts_count';
324+
if (!$this->hasData($key)) {
325+
326+
$categories = $this->getChildrenIds();
327+
$categories[] = $this->getId();
328+
329+
$posts = $this->postCollectionFactory->create()
330+
->addActiveFilter()
331+
->addStoreFilter($this->getStoreId())
332+
->addCategoryFilter($categories);
333+
334+
335+
336+
$this->setData($key, (int)$posts->getSize());
337+
}
338+
339+
return $this->getData($key);
340+
}
308341
}

etc/adminhtml/system.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0"?>
22
<!--
33
/**
4-
* Copyright © 2016 Ihor Vansach ([email protected]). All rights reserved.
4+
* Copyright © 2017 Ihor Vansach ([email protected]). All rights reserved.
55
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
66
*
77
* Glory to Ukraine! Glory to the heroes!
@@ -177,6 +177,10 @@
177177
<label>Enabled</label>
178178
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
179179
</field>
180+
<field id="show_posts_count" translate="label comment" type="select" sortOrder="15" showInDefault="1" showInWebsite="1" showInStore="1">
181+
<label>Display Number Of Posts</label>
182+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
183+
</field>
180184
<field id="sort_order" translate="label comment" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
181185
<label>Sort Order</label>
182186
</field>

etc/config.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0"?>
22
<!--
33
/**
4-
* Copyright © 2016 Ihor Vansach ([email protected]). All rights reserved.
4+
* Copyright © 2017 Ihor Vansach ([email protected]). All rights reserved.
55
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
66
*
77
* Glory to Ukraine! Glory to the heroes!
@@ -48,6 +48,7 @@
4848
</search>
4949
<categories>
5050
<enabled>1</enabled>
51+
<show_posts_count>1</show_posts_count>
5152
<sort_order>20</sort_order>
5253
</categories>
5354
<recent_posts>

view/frontend/templates/sidebar/categories.phtml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright © 2015 Ihor Vansach ([email protected]). All rights reserved.
3+
* Copyright © 2017 Ihor Vansach ([email protected]). All rights reserved.
44
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
55
*
66
* Glory to Ukraine! Glory to the heroes!
@@ -49,6 +49,9 @@
4949
<a href="<?php echo $item->getCategoryUrl() ?>">
5050
<?php echo $block->escapeHtml($item->getTitle()) ?>
5151
</a>
52+
<?php if ($block->showPostsCount()) { ?>
53+
(<?php echo $block->escapeHtml($item->getPostsCount()) ?>)
54+
<?php } ?>
5255
<?php
5356
$level = $newLevel;
5457
$first = false;

0 commit comments

Comments
 (0)