Skip to content

Commit

Permalink
Backport.Sorting by Websites not working in product grid in backoffice
Browse files Browse the repository at this point in the history
  • Loading branch information
XxXgeoXxX committed Jan 23, 2019
1 parent cbd8d8a commit 686f310
Showing 1 changed file with 58 additions and 2 deletions.
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,
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']);
}
}
}

0 comments on commit 686f310

Please sign in to comment.