From 3c58d2d0963fce06bf78011fcf0c9f2a7ae4e594 Mon Sep 17 00:00:00 2001
From: Michail Slabko <mslabko@ebay.com>
Date: Thu, 25 Jun 2015 17:08:47 +0300
Subject: [PATCH] MAGETWO-35896: PHP - Apply New Data Grids in Products Data
Grids - MAGETWO-38675: Implement new Product Grid -- add website, store
columns -- add column sorting
---
.../Catalog/Block/Adminhtml/Product.php | 19 ---------
.../Model/Resource/Product/Collection.php | 2 +-
.../Catalog/Ui/Component/ColumnFactory.php | 10 ++++-
.../Catalog/Ui/Component/Listing/Columns.php | 3 +-
.../Listing/Columns/ProductActions.php | 2 +-
.../Ui/Component/Listing/Columns/Websites.php | 21 ++++++++++
.../Product/AddStoreFieldToCollection.php | 42 ++++---------------
.../Product/ProductDataProvider.php | 20 +++++++++
app/code/Magento/Catalog/etc/adminhtml/di.xml | 4 +-
.../layout/catalog_product_index.xml | 13 +-----
.../ui_component/product_listing.xml | 33 +++++++--------
.../AddFilterToCollectionInterface.php | 22 ++++++++++
12 files changed, 104 insertions(+), 87 deletions(-)
create mode 100644 app/code/Magento/Ui/DataProvider/AddFilterToCollectionInterface.php
diff --git a/app/code/Magento/Catalog/Block/Adminhtml/Product.php b/app/code/Magento/Catalog/Block/Adminhtml/Product.php
index cad79a874c4..5d44134b06e 100644
--- a/app/code/Magento/Catalog/Block/Adminhtml/Product.php
+++ b/app/code/Magento/Catalog/Block/Adminhtml/Product.php
@@ -13,11 +13,6 @@ namespace Magento\Catalog\Block\Adminhtml;
class Product extends \Magento\Backend\Block\Widget\Container
{
- /**
- * @var string
- */
- protected $_template = 'catalog/product.phtml';
-
/**
* @var \Magento\Catalog\Model\Product\TypeFactory
*/
@@ -63,10 +58,6 @@ class Product extends \Magento\Backend\Block\Widget\Container
];
$this->buttonList->add('add_new', $addButtonProps);
- $this->setChild(
- 'grid',
- $this->getLayout()->createBlock('Magento\Catalog\Block\Adminhtml\Product\Grid', 'product.grid')
- );
return parent::_prepareLayout();
}
@@ -111,16 +102,6 @@ class Product extends \Magento\Backend\Block\Widget\Container
);
}
- /**
- * Render grid
- *
- * @return string
- */
- public function getGridHtml()
- {
- return $this->getChildHtml('grid');
- }
-
/**
* Check whether it is single store mode
*
diff --git a/app/code/Magento/Catalog/Model/Resource/Product/Collection.php b/app/code/Magento/Catalog/Model/Resource/Product/Collection.php
index f3bc63e9053..91b866faf47 100644
--- a/app/code/Magento/Catalog/Model/Resource/Product/Collection.php
+++ b/app/code/Magento/Catalog/Model/Resource/Product/Collection.php
@@ -725,7 +725,7 @@ class Collection extends \Magento\Catalog\Model\Resource\Collection\AbstractColl
if ($this->isLoaded()) {
return $this;
}
- parent::load($printQuery = false, $logQuery = false);
+ parent::load($printQuery, $logQuery);
if ($this->needToAddWebsiteNamesToResult) {
$this->doAddWebsiteNamesToResult();
diff --git a/app/code/Magento/Catalog/Ui/Component/ColumnFactory.php b/app/code/Magento/Catalog/Ui/Component/ColumnFactory.php
index 18e1461d868..128e757ab53 100644
--- a/app/code/Magento/Catalog/Ui/Component/ColumnFactory.php
+++ b/app/code/Magento/Catalog/Ui/Component/ColumnFactory.php
@@ -7,6 +7,11 @@ namespace Magento\Catalog\Ui\Component;
class ColumnFactory
{
+ /**
+ * @var int
+ */
+ private $columnSortOrder = 0;
+
/**
* @var \Magento\Framework\View\Element\UiComponentFactory
*/
@@ -44,10 +49,12 @@ class ColumnFactory
/**
* @param \Magento\Catalog\Api\Data\ProductAttributeInterface $attribute
* @param \Magento\Framework\View\Element\UiComponent\ContextInterface $context
+ * @param int $columnsAmount
* @return \Magento\Ui\Component\Listing\Columns\ColumnInterface
*/
- public function create($attribute, $context)
+ public function create($attribute, $context, $columnsAmount)
{
+ $this->columnSortOrder += 1;
$columnName = $attribute->getAttributeCode();
$config = [
'label' => __($attribute->getDefaultFrontendLabel()),
@@ -56,6 +63,7 @@ class ColumnFactory
'align' => 'left',
'add_field' => true,
'visible' => $attribute->getIsVisibleInGrid(),
+ 'sortOrder' => $this->columnSortOrder + $columnsAmount,
];
if ($attribute->usesSource()) {
diff --git a/app/code/Magento/Catalog/Ui/Component/Listing/Columns.php b/app/code/Magento/Catalog/Ui/Component/Listing/Columns.php
index a660d54191e..68d9866733a 100644
--- a/app/code/Magento/Catalog/Ui/Component/Listing/Columns.php
+++ b/app/code/Magento/Catalog/Ui/Component/Listing/Columns.php
@@ -31,9 +31,10 @@ class Columns extends \Magento\Ui\Component\Listing\Columns
*/
public function prepare()
{
+ $columnsAmount = count($this->components);
foreach ($this->attributeRepository->getList() as $attribute) {
if (!isset($this->components[$attribute->getAttributeCode()])) {
- $column = $this->columnFactory->create($attribute, $this->getContext());
+ $column = $this->columnFactory->create($attribute, $this->getContext(), $columnsAmount);
$column->prepare();
$this->addComponent($attribute->getAttributeCode(), $column);
}
diff --git a/app/code/Magento/Catalog/Ui/Component/Listing/Columns/ProductActions.php b/app/code/Magento/Catalog/Ui/Component/Listing/Columns/ProductActions.php
index 4a9b0b3d503..586ed53cd90 100644
--- a/app/code/Magento/Catalog/Ui/Component/Listing/Columns/ProductActions.php
+++ b/app/code/Magento/Catalog/Ui/Component/Listing/Columns/ProductActions.php
@@ -49,7 +49,7 @@ class ProductActions extends Column
if (isset($dataSource['data']['items'])) {
foreach ($dataSource['data']['items'] as &$item) {
$item[$this->getData('name')]['edit'] = [
- 'href' => $this->urlBuilder->getUrl('catalog/*/edit', ['id' => $item['entity_id']]),
+ 'href' => $this->urlBuilder->getUrl('catalog/product/edit', ['id' => $item['entity_id']]),
'label' => __('Edit'),
'hidden' => false,
];
diff --git a/app/code/Magento/Catalog/Ui/Component/Listing/Columns/Websites.php b/app/code/Magento/Catalog/Ui/Component/Listing/Columns/Websites.php
index e5f53b3676d..5282a3e03ce 100644
--- a/app/code/Magento/Catalog/Ui/Component/Listing/Columns/Websites.php
+++ b/app/code/Magento/Catalog/Ui/Component/Listing/Columns/Websites.php
@@ -41,6 +41,27 @@ class Websites extends \Magento\Ui\Component\Listing\Columns\Column
$this->storeManager = $storeManager;
}
+ /**
+ * {@inheritdoc}
+ */
+ public function prepareDataSource(array & $dataSource)
+ {
+ $websiteNames = [];
+ foreach ($this->getData('options') as $website) {
+ $websiteNames[$website->getWebsiteId()] = $website->getName();
+ }
+ if (isset($dataSource['data']['items'])) {
+ $fieldName = $this->getData('name');
+ foreach ($dataSource['data']['items'] as & $item) {
+ $websites = [];
+ foreach ($item[$fieldName] as $websiteId) {
+ $websites[] = $websiteNames[$websiteId];
+ }
+ $item[$fieldName] = implode(', ', $websites);
+ }
+ }
+ }
+
/**
* Prepare component configuration
*/
diff --git a/app/code/Magento/Catalog/Ui/DataProvider/Product/AddStoreFieldToCollection.php b/app/code/Magento/Catalog/Ui/DataProvider/Product/AddStoreFieldToCollection.php
index c0fcad416b0..38c56980272 100644
--- a/app/code/Magento/Catalog/Ui/DataProvider/Product/AddStoreFieldToCollection.php
+++ b/app/code/Magento/Catalog/Ui/DataProvider/Product/AddStoreFieldToCollection.php
@@ -5,16 +5,11 @@
*/
namespace Magento\Catalog\Ui\DataProvider\Product;
-use Magento\Framework\App\RequestInterface;
use Magento\Framework\Data\Collection;
-use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;
-use Magento\Ui\DataProvider\AddFieldToCollectionInterface;
+use Magento\Ui\DataProvider\AddFilterToCollectionInterface;
-/**
- * Class AddStoreFieldToCollection
- */
-class AddStoreFieldToCollection implements AddFieldToCollectionInterface
+class AddStoreFieldToCollection implements AddFilterToCollectionInterface
{
/**
* Store manager
@@ -23,45 +18,24 @@ class AddStoreFieldToCollection implements AddFieldToCollectionInterface
*/
protected $storeManager;
- /**
- * Request
- *
- * @var RequestInterface
- */
- protected $request;
-
/**
* Construct
*
* @param StoreManagerInterface $storeManager
- * @param RequestInterface $request
*/
- public function __construct(
- StoreManagerInterface $storeManager,
- RequestInterface $request
- ) {
+ public function __construct(StoreManagerInterface $storeManager)
+ {
$this->storeManager = $storeManager;
- $this->request = $request;
}
/**
* {@inheritdoc}
*/
- public function addField(Collection $collection, $field, $alias = null)
+ public function addFilter(Collection $collection, $field, $condition = null)
{
- $store = $this->getStore();
- if ($store->getId()) {
- $collection->addStoreFilter($store);
+ $storeId = isset($condition['eq']) ? $condition['eq'] : null;
+ if ($storeId) {
+ $collection->addStoreFilter($this->storeManager->getStore($storeId));
}
}
-
- /**
- * Get store
- *
- * @return Store
- */
- protected function getStore()
- {
- return $this->storeManager->getStore($this->request->getParam('store', Store::DEFAULT_STORE_ID));
- }
}
diff --git a/app/code/Magento/Catalog/Ui/DataProvider/Product/ProductDataProvider.php b/app/code/Magento/Catalog/Ui/DataProvider/Product/ProductDataProvider.php
index 81394864edd..f4f8e690aeb 100644
--- a/app/code/Magento/Catalog/Ui/DataProvider/Product/ProductDataProvider.php
+++ b/app/code/Magento/Catalog/Ui/DataProvider/Product/ProductDataProvider.php
@@ -24,6 +24,11 @@ class ProductDataProvider extends \Magento\Ui\DataProvider\AbstractEavDataProvid
*/
protected $addFieldStrategies;
+ /**
+ * @var \Magento\Ui\DataProvider\AddFilterToCollectionInterface[]
+ */
+ protected $addFilterStrategies;
+
/**
* Construct
*
@@ -32,6 +37,7 @@ class ProductDataProvider extends \Magento\Ui\DataProvider\AbstractEavDataProvid
* @param string $requestFieldName
* @param CollectionFactory $collectionFactory
* @param \Magento\Ui\DataProvider\AddFieldToCollectionInterface[] $addFieldStrategies
+ * @param \Magento\Ui\DataProvider\AddFilterToCollectionInterface[] $addFilterStrategies
* @param array $meta
* @param array $data
*/
@@ -41,12 +47,14 @@ class ProductDataProvider extends \Magento\Ui\DataProvider\AbstractEavDataProvid
$requestFieldName,
CollectionFactory $collectionFactory,
array $addFieldStrategies,
+ array $addFilterStrategies,
array $meta = [],
array $data = []
) {
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
$this->collection = $collectionFactory->create();
$this->addFieldStrategies = $addFieldStrategies;
+ $this->addFilterStrategies = $addFilterStrategies;
}
/**
@@ -91,4 +99,16 @@ class ProductDataProvider extends \Magento\Ui\DataProvider\AbstractEavDataProvid
$this->addFieldStrategies['default']->addField($this->getCollection(), $field, $alias = null);
}
}
+
+ /**
+ * {@inheritdoc}
+ */
+ public function addFilter($field, $condition = null)
+ {
+ if (isset($this->addFilterStrategies[$field])) {
+ $this->addFilterStrategies[$field]->addFilter($this->getCollection(), $field, $condition);
+ } else {
+ parent::addFilter($field, $condition);
+ }
+ }
}
diff --git a/app/code/Magento/Catalog/etc/adminhtml/di.xml b/app/code/Magento/Catalog/etc/adminhtml/di.xml
index c29f0652811..a1b392c9586 100644
--- a/app/code/Magento/Catalog/etc/adminhtml/di.xml
+++ b/app/code/Magento/Catalog/etc/adminhtml/di.xml
@@ -71,7 +71,9 @@
<argument name="addFieldStrategies" xsi:type="array">
<item name="default" xsi:type="object">Magento\Ui\DataProvider\AddFieldToEavCollection</item>
<item name="websites" xsi:type="object">Magento\Catalog\Ui\DataProvider\Product\AddWebsitesFieldToCollection</item>
- <item name="store" xsi:type="object">Magento\Catalog\Ui\DataProvider\Product\AddStoreFieldToCollection</item>
+ </argument>
+ <argument name="addFilterStrategies" xsi:type="array">
+ <item name="store_id" xsi:type="object">Magento\Catalog\Ui\DataProvider\Product\AddStoreFieldToCollection</item>
</argument>
</arguments>
</type>
diff --git a/app/code/Magento/Catalog/view/adminhtml/layout/catalog_product_index.xml b/app/code/Magento/Catalog/view/adminhtml/layout/catalog_product_index.xml
index 14d34d03d4b..c61f287a6aa 100644
--- a/app/code/Magento/Catalog/view/adminhtml/layout/catalog_product_index.xml
+++ b/app/code/Magento/Catalog/view/adminhtml/layout/catalog_product_index.xml
@@ -6,6 +6,7 @@
*/
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
+ <update handle="styles"/>
<body>
<referenceBlock name="menu">
<action method="setActive">
@@ -17,21 +18,9 @@
<argument name="class" xsi:type="string">complex</argument>
</action>
</referenceBlock>
- <referenceContainer name="page.main.actions">
- <block class="Magento\Backend\Block\Store\Switcher" name="store_switcher">
- <action method="setUseConfirm">
- <argument name="params" xsi:type="string">1</argument>
- </action>
- </block>
- </referenceContainer>
<referenceContainer name="content">
<uiComponent name="product_listing"/>
<block class="Magento\Catalog\Block\Adminhtml\Product" name="products_list"/>
</referenceContainer>
- <referenceContainer name="product.grid">
- <block class="Magento\Framework\View\Element\Text\ListText" name="grid.bottom.links">
- <block class="Magento\Catalog\Block\Adminhtml\Rss\Grid\Link" name="grid.rss.link"/>
- </block>
- </referenceContainer>
</body>
</page>
diff --git a/app/code/Magento/Catalog/view/adminhtml/ui_component/product_listing.xml b/app/code/Magento/Catalog/view/adminhtml/ui_component/product_listing.xml
index 075ff83d009..08590256e9d 100644
--- a/app/code/Magento/Catalog/view/adminhtml/ui_component/product_listing.xml
+++ b/app/code/Magento/Catalog/view/adminhtml/ui_component/product_listing.xml
@@ -113,6 +113,9 @@
<item name="dataScope" xsi:type="string">store_id</item>
<item name="caption" xsi:type="string" translate="true">Select...</item>
<item name="label" xsi:type="string" translate="true">Store View</item>
+ <item name="imports" xsi:type="array">
+ <item name="visible" xsi:type="string"></item>
+ </item>
</item>
</argument>
</filterSelect>
@@ -199,7 +202,7 @@
</item>
<item name="config" xsi:type="array">
<item name="indexField" xsi:type="string">entity_id</item>
- <item name="appendTo" xsi:type="string"></item>
+ <item name="sortOrder" xsi:type="number">0</item>
</item>
</argument>
</column>
@@ -213,6 +216,7 @@
<item name="sorting" xsi:type="string">asc</item>
<item name="align" xsi:type="string">left</item>
<item name="label" xsi:type="string" translate="true">ID</item>
+ <item name="sortOrder" xsi:type="number">1</item>
</item>
</argument>
</column>
@@ -230,6 +234,7 @@
<item name="sorting" xsi:type="string">asc</item>
<item name="align" xsi:type="string">left</item>
<item name="label" xsi:type="string" translate="true">Thumbnail</item>
+ <item name="sortOrder" xsi:type="number">2</item>
</item>
</argument>
</column>
@@ -244,6 +249,7 @@
<item name="sorting" xsi:type="string">asc</item>
<item name="align" xsi:type="string">left</item>
<item name="label" xsi:type="string" translate="true">Name</item>
+ <item name="sortOrder" xsi:type="number">3</item>
</item>
</argument>
</column>
@@ -257,6 +263,7 @@
<item name="dataType" xsi:type="string">select</item>
<item name="align" xsi:type="string">left</item>
<item name="label" xsi:type="string" translate="true">Type</item>
+ <item name="sortOrder" xsi:type="number">4</item>
</item>
</argument>
</column>
@@ -270,6 +277,7 @@
<item name="dataType" xsi:type="string">select</item>
<item name="align" xsi:type="string">left</item>
<item name="label" xsi:type="string" translate="true">Attribute Set</item>
+ <item name="sortOrder" xsi:type="number">5</item>
</item>
</argument>
</column>
@@ -283,6 +291,7 @@
<item name="sorting" xsi:type="string">asc</item>
<item name="align" xsi:type="string">left</item>
<item name="label" xsi:type="string" translate="true">SKU</item>
+ <item name="sortOrder" xsi:type="number">6</item>
</item>
</argument>
</column>
@@ -297,6 +306,7 @@
<item name="sorting" xsi:type="string">asc</item>
<item name="align" xsi:type="string">left</item>
<item name="label" xsi:type="string" translate="true">Price</item>
+ <item name="sortOrder" xsi:type="number">7</item>
</item>
</argument>
</column>
@@ -311,6 +321,7 @@
<item name="dataType" xsi:type="string">select</item>
<item name="align" xsi:type="string">left</item>
<item name="label" xsi:type="string" translate="true">Visibility</item>
+ <item name="sortOrder" xsi:type="number">8</item>
</item>
</argument>
</column>
@@ -325,6 +336,7 @@
<item name="dataType" xsi:type="string">select</item>
<item name="align" xsi:type="string">left</item>
<item name="label" xsi:type="string" translate="true">Status</item>
+ <item name="sortOrder" xsi:type="number">9</item>
</item>
</argument>
</column>
@@ -332,34 +344,21 @@
<argument name="data" xsi:type="array">
<item name="options" xsi:type="object">Magento\Store\Model\Resource\Website\Collection</item>
<item name="js_config" xsi:type="array">
- <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/select</item>
+ <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/sortable</item>
</item>
<item name="config" xsi:type="array">
<item name="add_field" xsi:type="boolean">true</item>
<item name="dataType" xsi:type="string">select</item>
<item name="align" xsi:type="string">left</item>
<item name="label" xsi:type="string" translate="true">Websites</item>
- </item>
- </argument>
- </column>
- <column name="store_id" class="Magento\Store\Ui\Component\Listing\Column\Store">
- <argument name="data" xsi:type="array">
- <item name="js_config" xsi:type="array">
- <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/sortable</item>
- </item>
- <item name="config" xsi:type="array">
- <item name="add_field" xsi:type="boolean">true</item>
- <item name="bodyTmpl" xsi:type="string">ui/grid/cells/html</item>
- <item name="sortable" xsi:type="boolean">false</item>
- <item name="dataType" xsi:type="string">text</item>
- <item name="align" xsi:type="string">left</item>
- <item name="label" xsi:type="string" translate="true">Store View</item>
+ <item name="sortOrder" xsi:type="number">10</item>
</item>
</argument>
</column>
<column name="actions" class="Magento\Catalog\Ui\Component\Listing\Columns\ProductActions">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
+ <item name="sortOrder" xsi:type="number">200</item>
<item name="dataType" xsi:type="string">actions</item>
<item name="align" xsi:type="string">left</item>
<item name="label" xsi:type="string" translate="true">Action</item>
diff --git a/app/code/Magento/Ui/DataProvider/AddFilterToCollectionInterface.php b/app/code/Magento/Ui/DataProvider/AddFilterToCollectionInterface.php
new file mode 100644
index 00000000000..bc52878544d
--- /dev/null
+++ b/app/code/Magento/Ui/DataProvider/AddFilterToCollectionInterface.php
@@ -0,0 +1,22 @@
+<?php
+/**
+ * Copyright © 2015 Magento. All rights reserved.
+ * See COPYING.txt for license details.
+ */
+namespace Magento\Ui\DataProvider;
+
+use Magento\Framework\Data\Collection;
+
+/**
+ * AddFilterToCollection interface
+ */
+interface AddFilterToCollectionInterface
+{
+ /**
+ * @param Collection $collection
+ * @param $field
+ * @param null $condition
+ * @return mixed
+ */
+ public function addFilter(Collection $collection, $field, $condition = null);
+}
--
GitLab