Skip to content

Commit fba7647

Browse files
authored
Merge pull request #441 from magefan/6820-sort-by-end-date-in-recent-widget
Add sorting in recent widget
2 parents fa2940c + 4eaa490 commit fba7647

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

Block/Widget/Recent.php

+39-1
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@
88

99
namespace Magefan\Blog\Block\Widget;
1010

11+
use Magefan\Blog\Block\Post\PostList\AbstractList;
12+
use Magefan\Blog\Model\Config\Source\PostsSortBy;
13+
use Magento\Framework\Api\SortOrder;
14+
1115
/**
1216
* Blog recent posts widget
1317
*/
14-
class Recent extends \Magefan\Blog\Block\Post\PostList\AbstractList implements \Magento\Widget\Block\BlockInterface
18+
class Recent extends AbstractList implements \Magento\Widget\Block\BlockInterface
1519
{
1620
/**
1721
* @var array
@@ -170,4 +174,38 @@ public function getShorContent($post, $len = null, $endСharacters = null)
170174
{
171175
return $post->getShortFilteredContent($len, $endСharacters);
172176
}
177+
178+
/**
179+
* @return string
180+
*/
181+
public function getCollectionOrderField(): string
182+
{
183+
$postsSortBy = (int)$this->getData('posts_sort_by');
184+
if ($postsSortBy) {
185+
switch ($postsSortBy) {
186+
case PostsSortBy::POSITION:
187+
return AbstractList::POSTS_SORT_FIELD_BY_POSITION;
188+
case PostsSortBy::TITLE:
189+
return AbstractList::POSTS_SORT_FIELD_BY_TITLE;
190+
}
191+
}
192+
193+
return parent::getCollectionOrderField();
194+
}
195+
196+
/**
197+
* Retrieve collection order direction
198+
*
199+
* @return string
200+
*/
201+
public function getCollectionOrderDirection()
202+
{
203+
$postsSortBy = (int)$this->getData('posts_sort_by');
204+
205+
if (PostsSortBy::TITLE == $postsSortBy) {
206+
return SortOrder::SORT_ASC;
207+
}
208+
209+
return parent::getCollectionOrderDirection();
210+
}
173211
}

etc/widget.xml

+3
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@
7676
<pre>
7777
]]></description>
7878
</parameter>
79+
<parameter name="posts_sort_by" xsi:type="select" source_model="Magefan\Blog\Model\Config\Source\PostsSortBy" visible="true" sort_order="90" >
80+
<label translate="true">Posts Sort By</label>
81+
</parameter>
7982
</parameters>
8083
</widget>
8184
<widget id="magefan_blog_featured" class="Magefan\Blog\Block\Widget\Featured">

0 commit comments

Comments
 (0)