Skip to content

Commit

Permalink
Merge pull request #441 from magefan/6820-sort-by-end-date-in-recent-…
Browse files Browse the repository at this point in the history
…widget

Add sorting in recent widget
  • Loading branch information
magefan authored Aug 17, 2022
2 parents fa2940c + 4eaa490 commit fba7647
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
40 changes: 39 additions & 1 deletion Block/Widget/Recent.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@

namespace Magefan\Blog\Block\Widget;

use Magefan\Blog\Block\Post\PostList\AbstractList;
use Magefan\Blog\Model\Config\Source\PostsSortBy;
use Magento\Framework\Api\SortOrder;

/**
* Blog recent posts widget
*/
class Recent extends \Magefan\Blog\Block\Post\PostList\AbstractList implements \Magento\Widget\Block\BlockInterface
class Recent extends AbstractList implements \Magento\Widget\Block\BlockInterface
{
/**
* @var array
Expand Down Expand Up @@ -170,4 +174,38 @@ public function getShorContent($post, $len = null, $endСharacters = null)
{
return $post->getShortFilteredContent($len, $endСharacters);
}

/**
* @return string
*/
public function getCollectionOrderField(): string
{
$postsSortBy = (int)$this->getData('posts_sort_by');
if ($postsSortBy) {
switch ($postsSortBy) {
case PostsSortBy::POSITION:
return AbstractList::POSTS_SORT_FIELD_BY_POSITION;
case PostsSortBy::TITLE:
return AbstractList::POSTS_SORT_FIELD_BY_TITLE;
}
}

return parent::getCollectionOrderField();
}

/**
* Retrieve collection order direction
*
* @return string
*/
public function getCollectionOrderDirection()
{
$postsSortBy = (int)$this->getData('posts_sort_by');

if (PostsSortBy::TITLE == $postsSortBy) {
return SortOrder::SORT_ASC;
}

return parent::getCollectionOrderDirection();
}
}
3 changes: 3 additions & 0 deletions etc/widget.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@
<pre>
]]></description>
</parameter>
<parameter name="posts_sort_by" xsi:type="select" source_model="Magefan\Blog\Model\Config\Source\PostsSortBy" visible="true" sort_order="90" >
<label translate="true">Posts Sort By</label>
</parameter>
</parameters>
</widget>
<widget id="magefan_blog_featured" class="Magefan\Blog\Block\Widget\Featured">
Expand Down

0 comments on commit fba7647

Please sign in to comment.