From 8e225ff64d6d886219a71f4f840f7d2265da9c39 Mon Sep 17 00:00:00 2001 From: DenysTsymbal Date: Tue, 9 Aug 2022 12:30:31 +0300 Subject: [PATCH 1/5] Add sorting in recent widget --- Block/Widget/Recent.php | 20 ++++++++++++++++++++ etc/widget.xml | 3 +++ 2 files changed, 23 insertions(+) diff --git a/Block/Widget/Recent.php b/Block/Widget/Recent.php index f2fc6f88..3b734b39 100755 --- a/Block/Widget/Recent.php +++ b/Block/Widget/Recent.php @@ -8,6 +8,8 @@ namespace Magefan\Blog\Block\Widget; +use Magefan\Blog\Model\Config\Source\PostsSortBy; + /** * Blog recent posts widget */ @@ -131,6 +133,24 @@ protected function _preparePostCollection() if ($enableNoRepeat && self::$processedIds) { $this->_postCollection->addFieldToFilter('post_id', ['nin' => self::$processedIds]); } + + if ('' !== (string)$this->getData('sort_by')) { + $sortBy = (int)$this->getData('sort_by'); + if (in_array($sortBy, [PostsSortBy::PUBLISH_DATE, PostsSortBy::POSITION, PostsSortBy::TITLE])) { + $orderField = 'publish_time'; + + switch ($sortBy) { + case PostsSortBy::POSITION: + $orderField = 'position'; + break; + case PostsSortBy::TITLE: + $orderField = 'title'; + break; + } + + $this->_postCollection->setOrder($orderField, 'DESC'); + } + } } /** diff --git a/etc/widget.xml b/etc/widget.xml index 826bc879..faa5bf66 100755 --- a/etc/widget.xml +++ b/etc/widget.xml @@ -76,6 +76,9 @@
                 ]]>
             
+            
+                
+            
         
     
     

From 1b5ba06325e42bc329332913a0088ca4d1c50f5a Mon Sep 17 00:00:00 2001
From: DenysTsymbal 
Date: Tue, 9 Aug 2022 15:20:28 +0300
Subject: [PATCH 2/5] Add sorting in recent

---
 Block/Widget/Recent.php | 40 ++++++++++++++++++++++------------------
 1 file changed, 22 insertions(+), 18 deletions(-)

diff --git a/Block/Widget/Recent.php b/Block/Widget/Recent.php
index 3b734b39..ab68b120 100755
--- a/Block/Widget/Recent.php
+++ b/Block/Widget/Recent.php
@@ -8,6 +8,7 @@
 
 namespace Magefan\Blog\Block\Widget;
 
+use Magefan\Blog\Block\Post\PostList\AbstractList;
 use Magefan\Blog\Model\Config\Source\PostsSortBy;
 
 /**
@@ -133,24 +134,6 @@ protected function _preparePostCollection()
         if ($enableNoRepeat && self::$processedIds) {
             $this->_postCollection->addFieldToFilter('post_id', ['nin' => self::$processedIds]);
         }
-
-        if ('' !== (string)$this->getData('sort_by')) {
-            $sortBy = (int)$this->getData('sort_by');
-            if (in_array($sortBy, [PostsSortBy::PUBLISH_DATE, PostsSortBy::POSITION, PostsSortBy::TITLE])) {
-                $orderField = 'publish_time';
-
-                switch ($sortBy) {
-                    case PostsSortBy::POSITION:
-                        $orderField = 'position';
-                        break;
-                    case PostsSortBy::TITLE:
-                        $orderField = 'title';
-                        break;
-                }
-
-                $this->_postCollection->setOrder($orderField, 'DESC');
-            }
-        }
     }
 
     /**
@@ -190,4 +173,25 @@ public function getShorContent($post, $len = null, $endСharacters = null)
     {
         return $post->getShortFilteredContent($len, $endСharacters);
     }
+
+    /**
+     * @return string
+     */
+    public function getCollectionOrderDirection(): string
+    {
+        if ('' !== (string)$this->getData('sort_by')) {
+            $sortBy = (int)$this->getData('sort_by');
+            if (in_array($sortBy, [PostsSortBy::PUBLISH_DATE, PostsSortBy::POSITION, PostsSortBy::TITLE])) {
+
+                switch ($sortBy) {
+                    case PostsSortBy::POSITION:
+                        return AbstractList::POSTS_SORT_FIELD_BY_POSITION;
+                    case PostsSortBy::TITLE:
+                        return AbstractList::POSTS_SORT_FIELD_BY_TITLE;
+                }
+            }
+        }
+
+        return parent::getCollectionOrderField();
+    }
 }

From 25ec320a688af61bc4cfdb2bb03242e818a5fe87 Mon Sep 17 00:00:00 2001
From: DenysTsymbal 
Date: Tue, 9 Aug 2022 15:28:44 +0300
Subject: [PATCH 3/5] Add sorting to recent widget

---
 Block/Widget/Recent.php | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/Block/Widget/Recent.php b/Block/Widget/Recent.php
index ab68b120..69add190 100755
--- a/Block/Widget/Recent.php
+++ b/Block/Widget/Recent.php
@@ -177,7 +177,7 @@ public function getShorContent($post, $len = null, $endСharacters = null)
     /**
      * @return string
      */
-    public function getCollectionOrderDirection(): string
+    public function getCollectionOrderField(): string
     {
         if ('' !== (string)$this->getData('sort_by')) {
             $sortBy = (int)$this->getData('sort_by');

From 5b1923ca3cd4eff415aa3978c4ded2d178e24684 Mon Sep 17 00:00:00 2001
From: DenysTsymbal 
Date: Tue, 9 Aug 2022 15:31:49 +0300
Subject: [PATCH 4/5] Add shortcut

---
 Block/Widget/Recent.php | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/Block/Widget/Recent.php b/Block/Widget/Recent.php
index 69add190..2c23adf8 100755
--- a/Block/Widget/Recent.php
+++ b/Block/Widget/Recent.php
@@ -14,7 +14,7 @@
 /**
  * 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

From 4eaa49000bf4b9d2b74f1593b2aaae8a61a714b2 Mon Sep 17 00:00:00 2001
From: DenysTsymbal 
Date: Wed, 10 Aug 2022 09:44:37 +0300
Subject: [PATCH 5/5] Some changes

---
 Block/Widget/Recent.php | 34 ++++++++++++++++++++++++----------
 etc/widget.xml          |  4 ++--
 2 files changed, 26 insertions(+), 12 deletions(-)

diff --git a/Block/Widget/Recent.php b/Block/Widget/Recent.php
index 2c23adf8..db95be59 100755
--- a/Block/Widget/Recent.php
+++ b/Block/Widget/Recent.php
@@ -10,6 +10,7 @@
 
 use Magefan\Blog\Block\Post\PostList\AbstractList;
 use Magefan\Blog\Model\Config\Source\PostsSortBy;
+use Magento\Framework\Api\SortOrder;
 
 /**
  * Blog recent posts widget
@@ -179,19 +180,32 @@ public function getShorContent($post, $len = null, $endСharacters = null)
      */
     public function getCollectionOrderField(): string
     {
-        if ('' !== (string)$this->getData('sort_by')) {
-            $sortBy = (int)$this->getData('sort_by');
-            if (in_array($sortBy, [PostsSortBy::PUBLISH_DATE, PostsSortBy::POSITION, PostsSortBy::TITLE])) {
-
-                switch ($sortBy) {
-                    case PostsSortBy::POSITION:
-                        return AbstractList::POSTS_SORT_FIELD_BY_POSITION;
-                    case PostsSortBy::TITLE:
-                        return AbstractList::POSTS_SORT_FIELD_BY_TITLE;
-                }
+        $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();
+    }
 }
diff --git a/etc/widget.xml b/etc/widget.xml
index faa5bf66..f081448d 100755
--- a/etc/widget.xml
+++ b/etc/widget.xml
@@ -76,8 +76,8 @@
                     
                 ]]>
             
-            
-                
+            
+