Skip to content

Commit dffa2ff

Browse files
committed
MC-15250: Explicit product sorting in PageBuilder Products Content type
1 parent d89bd6d commit dffa2ff

File tree

25 files changed

+1067
-2
lines changed

25 files changed

+1067
-2
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\PageBuilder\Model\Catalog;
8+
9+
/**
10+
* Sorting of Catalog Widget Products
11+
*
12+
* @package Magento\PageBuilder\Model
13+
*/
14+
class Sorting
15+
{
16+
/**
17+
* @var array
18+
*/
19+
protected $sortClasses = [
20+
'default' => Sorting\DefaultSorting::class
21+
];
22+
23+
/**
24+
* @var Sorting\Factory
25+
*/
26+
protected $factory;
27+
28+
/**
29+
* @var array
30+
*/
31+
protected $sortInstances = [];
32+
33+
/**
34+
* Sorting constructor.
35+
* @param Sorting\Factory $factory
36+
* @param array $sortClasses
37+
* @throws \Magento\Framework\Exception\LocalizedException
38+
*/
39+
public function __construct(
40+
Sorting\Factory $factory,
41+
array $sortClasses
42+
) {
43+
$this->sortClasses = array_merge($this->sortClasses, $sortClasses);
44+
$this->factory = $factory;
45+
foreach ($this->sortClasses as $key => $className) {
46+
$this->sortInstances[$key] = $this->factory->create($className);
47+
}
48+
}
49+
50+
/**
51+
* Sorting options array
52+
*
53+
* @return array
54+
*/
55+
public function getSortingOptions(): array
56+
{
57+
$options = [];
58+
foreach ($this->sortInstances as $key => $instance) {
59+
$options[$key] = $instance->getLabel();
60+
}
61+
return $options;
62+
}
63+
64+
/**
65+
* Get the instance of the first option which is None
66+
*
67+
* @param string $sortOption
68+
* @return Sorting\SortInterface|null
69+
*/
70+
public function getSortingInstance($sortOption): Sorting\SortInterface
71+
{
72+
if (isset($this->sortInstances[$sortOption])) {
73+
return $this->sortInstances[$sortOption];
74+
}
75+
return $this->sortInstances['default'];
76+
}
77+
78+
/**
79+
* Applying sorting option
80+
*
81+
* @param string $option
82+
* @param \Magento\Catalog\Model\ResourceModel\Product\Collection $collection
83+
* @return \Magento\Catalog\Model\ResourceModel\Product\Collection
84+
*/
85+
public function applySorting(
86+
string $option,
87+
\Magento\Catalog\Model\ResourceModel\Product\Collection $collection
88+
): \Magento\Catalog\Model\ResourceModel\Product\Collection {
89+
$sortBuilder = $this->getSortingInstance($option);
90+
$_collection = $sortBuilder->sort($collection->setCurPage(0));
91+
92+
if ($_collection->isLoaded()) {
93+
$_collection->clear();
94+
}
95+
96+
return $_collection;
97+
}
98+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\PageBuilder\Model\Catalog\Sorting;
8+
9+
use \Magento\Framework\DB\Select;
10+
11+
/**
12+
* Class AttributeAbstract
13+
*
14+
* @package Magento\PageBuilder\Model\Catalog\Sorting
15+
*/
16+
abstract class AttributeAbstract extends SortAbstract implements SortInterface
17+
{
18+
/**
19+
* Sort direction
20+
*
21+
* @return string
22+
*/
23+
abstract protected function getSortDirection();
24+
25+
/**
26+
* Attribute field
27+
*
28+
* @return string
29+
*/
30+
abstract protected function getSortField();
31+
32+
/**
33+
* @inheritdoc
34+
*/
35+
public function sort(
36+
\Magento\Catalog\Model\ResourceModel\Product\Collection $collection
37+
): \Magento\Catalog\Model\ResourceModel\Product\Collection {
38+
$collection->getSelect()->reset(Select::ORDER);
39+
$collection->addOrder($this->getSortField(), $this->getSortDirection());
40+
return $collection;
41+
}
42+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\PageBuilder\Model\Catalog\Sorting\Date;
8+
9+
use \Magento\PageBuilder\Model\Catalog\Sorting\AttributeAbstract;
10+
11+
/**
12+
* Class NewestTop
13+
*
14+
* @package Magento\PageBuilder\Model\Catalog\Sorting\Date
15+
*/
16+
class NewestTop extends AttributeAbstract
17+
{
18+
/**
19+
* @inheritdoc
20+
*/
21+
protected function getSortField()
22+
{
23+
return 'entity_id';
24+
}
25+
26+
/**
27+
* @inheritdoc
28+
*/
29+
protected function getSortDirection()
30+
{
31+
return $this->descOrder();
32+
}
33+
34+
/**
35+
* @inheritdoc
36+
*/
37+
public function getLabel(): string
38+
{
39+
return __('Newest products first');
40+
}
41+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\PageBuilder\Model\Catalog\Sorting\Date;
8+
9+
use \Magento\PageBuilder\Model\Catalog\Sorting\AttributeAbstract;
10+
11+
/**
12+
* Class OldestTop
13+
*
14+
* @package Magento\PageBuilder\Model\Catalog\Sorting\Date
15+
*/
16+
class OldestTop extends AttributeAbstract
17+
{
18+
/**
19+
* @inheritdoc
20+
*/
21+
protected function getSortField()
22+
{
23+
return 'entity_id';
24+
}
25+
26+
/**
27+
* @inheritdoc
28+
*/
29+
protected function getSortDirection()
30+
{
31+
return $this->ascOrder();
32+
}
33+
34+
/**
35+
* @inheritdoc
36+
*/
37+
public function getLabel(): string
38+
{
39+
return __('Oldest products first');
40+
}
41+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\PageBuilder\Model\Catalog\Sorting;
8+
9+
/**
10+
* Class DefaultSorting
11+
*
12+
* @package Magento\PageBuilder\Model\Catalog\Sorting
13+
*/
14+
class DefaultSorting extends SortAbstract implements SortInterface
15+
{
16+
/**
17+
* @inheritdoc
18+
*/
19+
public function sort(
20+
\Magento\Catalog\Model\ResourceModel\Product\Collection $collection
21+
): \Magento\Catalog\Model\ResourceModel\Product\Collection {
22+
return $collection;
23+
}
24+
25+
/**
26+
* @inheritdoc
27+
*/
28+
public function getLabel(): string
29+
{
30+
return __('None');
31+
}
32+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\PageBuilder\Model\Catalog\Sorting;
8+
9+
/**
10+
* Class Factory
11+
*
12+
* @package Magento\PageBuilder\Model\Catalog\Sorting
13+
*/
14+
class Factory
15+
{
16+
/**
17+
* @var \Magento\Framework\ObjectManagerInterface
18+
*/
19+
protected $_objectManager;
20+
21+
/**
22+
* @param \Magento\Framework\ObjectManagerInterface $objectManager
23+
*/
24+
public function __construct(\Magento\Framework\ObjectManagerInterface $objectManager)
25+
{
26+
$this->_objectManager = $objectManager;
27+
}
28+
29+
/**
30+
* Create instance of sorting class
31+
*
32+
* @param string $className
33+
* @param array $data
34+
* @return SortInterface
35+
* @throws \Magento\Framework\Exception\LocalizedException
36+
*/
37+
public function create($className, array $data = []): SortInterface
38+
{
39+
$instance = $this->_objectManager->create($className, $data);
40+
41+
if (!$instance instanceof \Magento\PageBuilder\Model\Catalog\Sorting\SortInterface) {
42+
throw new \Magento\Framework\Exception\LocalizedException(
43+
__('%1 doesn\'t implement SortInterface', $className)
44+
);
45+
}
46+
return $instance;
47+
}
48+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\PageBuilder\Model\Catalog\Sorting\Name;
8+
9+
use \Magento\PageBuilder\Model\Catalog\Sorting\AttributeAbstract;
10+
11+
/**
12+
* Class Name\Ascending
13+
*
14+
* @package Magento\PageBuilder\Model\Catalog\Sorting\Name
15+
*/
16+
class Ascending extends AttributeAbstract
17+
{
18+
/**
19+
* @inheritdoc
20+
*/
21+
protected function getSortField()
22+
{
23+
return 'name';
24+
}
25+
26+
/**
27+
* @inheritdoc
28+
*/
29+
protected function getSortDirection()
30+
{
31+
return $this->ascOrder();
32+
}
33+
34+
/**
35+
* @inheritdoc
36+
*/
37+
public function getLabel(): string
38+
{
39+
return __('Name: A - Z');
40+
}
41+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\PageBuilder\Model\Catalog\Sorting\Name;
8+
9+
use \Magento\PageBuilder\Model\Catalog\Sorting\AttributeAbstract;
10+
11+
/**
12+
* Class Name\Descending
13+
*
14+
* @package Magento\PageBuilder\Model\Catalog\Sorting\Name
15+
*/
16+
class Descending extends AttributeAbstract
17+
{
18+
/**
19+
* @inheritdoc
20+
*/
21+
protected function getSortField()
22+
{
23+
return 'name';
24+
}
25+
26+
/**
27+
* @inheritdoc
28+
*/
29+
protected function getSortDirection()
30+
{
31+
return $this->descOrder();
32+
}
33+
34+
/**
35+
* @inheritdoc
36+
*/
37+
public function getLabel(): string
38+
{
39+
return __('Name: Z - A');
40+
}
41+
}

0 commit comments

Comments
 (0)