Skip to content

Commit

Permalink
Sorting by Websites not working in product grid in backoffice #20511
Browse files Browse the repository at this point in the history
  • Loading branch information
XxXgeoXxX committed Jan 23, 2019
1 parent f710f9b commit 0dfe0f0
Showing 1 changed file with 57 additions and 1 deletion.
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,
array $components = [],
array $data = []
) {
parent::__construct($context, $uiComponentFactory, $components, $data);
$this->storeManager = $storeManager;
$this->_resourceHelper = $resourceHelper;
}

/**
Expand Down Expand Up @@ -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']);
}
}
}

0 comments on commit 0dfe0f0

Please sign in to comment.