Skip to content

Commit 0dfe0f0

Browse files
committed
Sorting by Websites not working in product grid in backoffice #20511
1 parent f710f9b commit 0dfe0f0

File tree

1 file changed

+57
-1
lines changed
  • app/code/Magento/Catalog/Ui/Component/Listing/Columns

1 file changed

+57
-1
lines changed

app/code/Magento/Catalog/Ui/Component/Listing/Columns/Websites.php

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php declare(strict_types=1);
22
/**
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
@@ -8,6 +8,7 @@
88
use Magento\Framework\View\Element\UiComponentFactory;
99
use Magento\Framework\View\Element\UiComponent\ContextInterface;
1010
use Magento\Store\Model\StoreManagerInterface;
11+
use \Magento\Framework\DB\Helper;
1112

1213
/**
1314
* @api
@@ -20,29 +21,42 @@ class Websites extends \Magento\Ui\Component\Listing\Columns\Column
2021
*/
2122
const NAME = 'websites';
2223

24+
/**
25+
* Data for concatenated website names value
26+
*/
27+
const WEBSITE_NAMES = 'website_names';
28+
2329
/**
2430
* Store manager
2531
*
2632
* @var StoreManagerInterface
2733
*/
2834
protected $storeManager;
2935

36+
/**
37+
* @var \Magento\Framework\DB\Helper
38+
*/
39+
protected $_resourceHelper;
40+
3041
/**
3142
* @param ContextInterface $context
3243
* @param UiComponentFactory $uiComponentFactory
3344
* @param StoreManagerInterface $storeManager
45+
* @param Helper $resourceHelper
3446
* @param array $components
3547
* @param array $data
3648
*/
3749
public function __construct(
3850
ContextInterface $context,
3951
UiComponentFactory $uiComponentFactory,
4052
StoreManagerInterface $storeManager,
53+
Helper $resourceHelper,
4154
array $components = [],
4255
array $data = []
4356
) {
4457
parent::__construct($context, $uiComponentFactory, $components, $data);
4558
$this->storeManager = $storeManager;
59+
$this->_resourceHelper = $resourceHelper;
4660
}
4761

4862
/**
@@ -83,4 +97,46 @@ public function prepare()
8397
$this->_data['config']['componentDisabled'] = true;
8498
}
8599
}
100+
101+
/**
102+
* Apply sorting
103+
*
104+
* @return void
105+
*/
106+
protected function applySorting()
107+
{
108+
$sorting = $this->getContext()->getRequestParam('sorting');
109+
$isSortable = $this->getData('config/sortable');
110+
if ($isSortable !== false
111+
&& !empty($sorting['field'])
112+
&& !empty($sorting['direction'])
113+
&& $sorting['field'] === $this->getName()
114+
) {
115+
$collection = $this->getContext()->getDataProvider()->getCollection();
116+
$collection
117+
->joinField(
118+
'websites_ids',
119+
'catalog_product_website',
120+
'website_id',
121+
'product_id=entity_id',
122+
null,
123+
'left'
124+
)
125+
->joinTable(
126+
'store_website',
127+
'website_id = websites_ids',
128+
['name'],
129+
null,
130+
'left'
131+
)
132+
->groupByAttribute('entity_id');
133+
$this->_resourceHelper->addGroupConcatColumn(
134+
$collection->getSelect(),
135+
self::WEBSITE_NAMES,
136+
'name'
137+
);
138+
139+
$collection->getSelect()->order(self::WEBSITE_NAMES . ' ' . $sorting['direction']);
140+
}
141+
}
86142
}

0 commit comments

Comments
 (0)