Skip to content

Commit f5ed217

Browse files
authored
Merge pull request #120 from magefan/t186
Added Additional filters for resent blog posts widget
2 parents 66efcae + f346592 commit f5ed217

File tree

4 files changed

+71
-5
lines changed

4 files changed

+71
-5
lines changed

Block/Widget/Recent.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright © 2015-2017 Ihor Vansach (ihor@magefan.com). All rights reserved.
3+
* Copyright © Magefan (support@magefan.com). 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!
@@ -94,6 +94,24 @@ protected function _preparePostCollection()
9494
if ($category = $this->getCategory()) {
9595
$this->_postCollection->addCategoryFilter($category);
9696
}
97+
98+
if ($tagId = $this->getData('tag_id')) {
99+
$this->_postCollection->addTagFilter($tagId);
100+
}
101+
102+
if ($authorId = $this->getData('author_id')) {
103+
$this->_postCollection->addAuthorFilter($authorId);
104+
}
105+
106+
if ($from = $this->getData('from')) {
107+
$this->_postCollection
108+
->addFieldToFilter('publish_time', array('gteq' => $from . " 00:00:00"));
109+
}
110+
111+
if ($to = $this->getData('to')) {
112+
$this->_postCollection
113+
->addFieldToFilter('publish_time', array('lteq' => $to . " 00:00:00"));
114+
}
97115
}
98116

99117
/**

Model/Config/Source/Tag.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
namespace Magefan\Blog\Model\Config\Source;
1010

1111
/**
12-
* Used in recent post widget
12+
* Used in recent post widget & post edit page
1313
*
1414
*/
1515
class Tag implements \Magento\Framework\Option\ArrayInterface

Model/Config/Source/WidgetTag.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Ihor Vansach ([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+
* Used in recent post widget
13+
*
14+
*/
15+
class WidgetTag extends Tag
16+
{
17+
/**
18+
* Options getter
19+
*
20+
* @return array
21+
*/
22+
public function toOptionArray()
23+
{
24+
if ($this->options === null) {
25+
parent::toOptionArray();
26+
array_unshift($this->options, ['label' => __('Please select'), 'value' => 0]);
27+
}
28+
29+
return $this->options;
30+
}
31+
32+
}

etc/widget.xml

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
22
<!--
33
/**
4-
* Copyright © 2015-2017 Ihor Vansach (ihor@magefan.com). All rights reserved.
4+
* Copyright © Magefan (support@magefan.com). 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!
@@ -24,7 +24,23 @@
2424
<label translate="true">Blog Category</label>
2525
<description translate="true">Leave blank to display posts from all categories.</description>
2626
</parameter>
27-
<parameter name="custom_template" xsi:type="text" visible="true" sort_order="40" >
27+
<parameter name="tag_id" xsi:type="select" source_model="Magefan\Blog\Model\Config\Source\WidgetTag" visible="true" sort_order="40" >
28+
<label translate="true">Blog Tag</label>
29+
<description translate="true">Leave blank to display posts from all tags.</description>
30+
</parameter>
31+
<parameter name="author_id" xsi:type="select" source_model="Magefan\Blog\Model\Config\Source\Author" visible="true" sort_order="50" >
32+
<label translate="true">Blog Author</label>
33+
<description translate="true">Leave blank to display posts from all author.</description>
34+
</parameter>
35+
<parameter name="from" xsi:type="text" visible="true" sort_order="60" >
36+
<label translate="true">Blog Publish Date From</label>
37+
<description translate="true">Please use date format YYYY-MM-DD.</description>
38+
</parameter>
39+
<parameter name="to" xsi:type="text" visible="true" sort_order="70" >
40+
<label translate="true">Blog Publish Date To</label>
41+
<description translate="true">Please use date format YYYY-MM-DD.</description>
42+
</parameter>
43+
<parameter name="custom_template" xsi:type="text" visible="true" sort_order="80" >
2844
<label translate="true">Custom Template</label>
2945
<description translate="true"><![CDATA[
3046
Leave blank to use default template <em>Magefan_Blog::widget/recent.phtml</em>.<br/>
@@ -54,4 +70,4 @@
5470
</parameter>
5571
</parameters>
5672
</widget>
57-
</widgets>
73+
</widgets>

0 commit comments

Comments
 (0)