Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport]Sorting by Websites not working in product grid in backoffice #20514

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 58 additions & 2 deletions app/code/Magento/Catalog/Ui/Component/Listing/Columns/Websites.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
Expand All @@ -8,6 +8,7 @@
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Store\Model\StoreManagerInterface;
use \Magento\Framework\DB\Helper;

/**
* @api
Expand All @@ -20,29 +21,42 @@ class Websites extends \Magento\Ui\Component\Listing\Columns\Column
*/
const NAME = 'websites';

/**
* Data for concatenated website names value
*/
const WEBSITE_NAMES = 'website_names';

/**
* Store manager
*
* @var StoreManagerInterface
*/
protected $storeManager;

/**
* @var \Magento\Framework\DB\Helper
*/
protected $_resourceHelper;

/**
* @param ContextInterface $context
* @param UiComponentFactory $uiComponentFactory
* @param StoreManagerInterface $storeManager
* @param Helper $resourceHelper
* @param array $components
* @param array $data
*/
public function __construct(
ContextInterface $context,
UiComponentFactory $uiComponentFactory,
StoreManagerInterface $storeManager,
Helper $resourceHelper,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, do not change arguments order. This change goes against backward compatibility. Add the resourceHelper to the very end of the arguments set

array $components = [],
array $data = []
) {
parent::__construct($context, $uiComponentFactory, $components, $data);
$this->storeManager = $storeManager;
$this->_resourceHelper = $resourceHelper;
}

/**
Expand Down Expand Up @@ -71,7 +85,7 @@ public function prepareDataSource(array $dataSource)

return $dataSource;
}

/**
* Prepare component configuration
* @return void
Expand All @@ -83,4 +97,46 @@ public function prepare()
$this->_data['config']['componentDisabled'] = true;
}
}

/**
* Apply sorting
*
* @return void
*/
protected function applySorting()
{
$sorting = $this->getContext()->getRequestParam('sorting');
$isSortable = $this->getData('config/sortable');
if ($isSortable !== false
&& !empty($sorting['field'])
&& !empty($sorting['direction'])
&& $sorting['field'] === $this->getName()
) {
$collection = $this->getContext()->getDataProvider()->getCollection();
$collection
->joinField(
'websites_ids',
'catalog_product_website',
'website_id',
'product_id=entity_id',
null,
'left'
)
->joinTable(
'store_website',
'website_id = websites_ids',
['name'],
null,
'left'
)
->groupByAttribute('entity_id');
$this->_resourceHelper->addGroupConcatColumn(
$collection->getSelect(),
self::WEBSITE_NAMES,
'name'
);

$collection->getSelect()->order(self::WEBSITE_NAMES . ' ' . $sorting['direction']);
}
}
}