Skip to content

Commit a9af93d

Browse files
committed
ACPT-677: Catalog product grid improvement
1 parent 472a93e commit a9af93d

File tree

12 files changed

+161
-43
lines changed

12 files changed

+161
-43
lines changed

app/code/Magento/Backend/Block/Widget/Grid/Extended.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,4 +1317,14 @@ public function getMainButtonsHtml()
13171317
}
13181318
return $html;
13191319
}
1320+
1321+
/**
1322+
* Check if configuration setting to calculate approximate total number of products in grid is enabled.
1323+
*
1324+
* @return bool
1325+
*/
1326+
public function calculateApproximateProductsTotalNumber(): bool
1327+
{
1328+
return (bool)$this->_scopeConfig->getValue('admin/grid/calculate_approximate_total_number_of_products');
1329+
}
13201330
}

app/code/Magento/Backend/etc/adminhtml/system.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -482,11 +482,11 @@
482482
</group>
483483
<group id="grid" translate="label" type="text" sortOrder="45" showInDefault="1">
484484
<label>Admin Grids</label>
485-
<field id="show_approximate_total_number_of_products" translate="label comment"
485+
<field id="calculate_approximate_total_number_of_products" translate="label comment"
486486
type="select" sortOrder="1" showInDefault="1" canRestore="1">
487-
<label>Show approximate total number of products</label>
487+
<label>Calculate approximate total number of products</label>
488488
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
489-
<comment>Show approximate total number of products in products grid header.</comment>
489+
<comment>Calculate approximate total number of products in products grid.</comment>
490490
</field>
491491
</group>
492492
</section>

app/code/Magento/Backend/etc/config.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
</system>
3434
<admin>
3535
<grid>
36-
<show_approximate_total_number_of_products>1</show_approximate_total_number_of_products>
36+
<calculate_approximate_total_number_of_products>1</calculate_approximate_total_number_of_products>
3737
</grid>
3838
</admin>
3939
<general>

app/code/Magento/Backend/view/adminhtml/templates/widget/grid/extended.phtml

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
7+
use Magento\Catalog\Ui\DataProvider\Product\ProductCollection;
8+
69
?>
710
<?php
811
/**
@@ -68,11 +71,17 @@ $numColumns = count($block->getColumns());
6871
<?php endif; ?>
6972
<?php $countRecords = $block->getCollection()->getSize(); ?>
7073
<div class="admin__control-support-text">
74+
<?php if (!$block->calculateApproximateProductsTotalNumber()
75+
|| !$block->getCollection() instanceof ProductCollection
76+
|| ($block->calculateApproximateProductsTotalNumber()
77+
&& $block->getCollection() instanceof ProductCollection
78+
&& $countRecords < ProductCollection::RECORDS_LIMIT)): ?>
7179
<span id="<?= $block->escapeHtml($block->getHtmlId()) ?>-total-count"
7280
<?= /* @noEscape */ $block->getUiId('total-count') ?>>
7381
<?= /* @noEscape */ $countRecords ?>
7482
</span>
75-
<?= $block->escapeHtml(__('records found')) ?>
83+
<?= $block->escapeHtml(__('records found')) ?>
84+
<?php endif; ?>
7685
<span id="<?= $block->escapeHtml($block->getHtmlId()) ?>_massaction-count"
7786
class="mass-select-info _empty">
7887
<strong data-role="counter">0</strong>
@@ -124,23 +133,29 @@ $numColumns = count($block->getColumns());
124133
<span><?= $block->escapeHtml(__('Previous page')) ?></span>
125134
</button>
126135
<?php endif; ?>
127-
<input type="text"
128-
id="<?= $block->escapeHtml($block->getHtmlId()) ?>_page-current"
129-
name="<?= $block->escapeHtmlAttr($block->getVarNamePage()) ?>"
130-
value="<?= $block->escapeHtmlAttr($_curPage) ?>"
131-
class="admin__control-text"
132-
<?= /* @noEscape */ $block->getUiId('current-page') ?> />
133-
<?= /* @noEscape */ $secureRenderer->renderEventListenerAsTag(
134-
'onkeypress',
135-
/* @noEscape */ $block->getJsObjectName() . '.inputPage(event, \'' .
136-
/* @noEscape */ $_lastPage . '\')',
137-
'#' . $block->escapeHtml($block->getHtmlId()) . '_page-current'
138-
) ?>
139-
<label class="admin__control-support-text"
140-
for="<?= $block->escapeHtml($block->getHtmlId()) ?>_page-current">
141-
<?= /* @noEscape */ __('of %1', '<span>' .
142-
$block->getCollection()->getLastPageNumber() . '</span>') ?>
143-
</label>
136+
<?php if (!$block->calculateApproximateProductsTotalNumber()
137+
|| !$block->getCollection() instanceof ProductCollection
138+
|| ($block->calculateApproximateProductsTotalNumber()
139+
&& $block->getCollection() instanceof ProductCollection
140+
&& $countRecords < ProductCollection::RECORDS_LIMIT)): ?>
141+
<input type="text"
142+
id="<?= $block->escapeHtml($block->getHtmlId()) ?>_page-current"
143+
name="<?= $block->escapeHtmlAttr($block->getVarNamePage()) ?>"
144+
value="<?= $block->escapeHtmlAttr($_curPage) ?>"
145+
class="admin__control-text"
146+
<?= /* @noEscape */ $block->getUiId('current-page') ?> />
147+
<?= /* @noEscape */ $secureRenderer->renderEventListenerAsTag(
148+
'onkeypress',
149+
/* @noEscape */ $block->getJsObjectName() . '.inputPage(event, \'' .
150+
/* @noEscape */ $_lastPage . '\')',
151+
'#' . $block->escapeHtml($block->getHtmlId()) . '_page-current'
152+
) ?>
153+
<label class="admin__control-support-text"
154+
for="<?= $block->escapeHtml($block->getHtmlId()) ?>_page-current">
155+
<?= /* @noEscape */ __('of %1', '<span>' .
156+
$block->getCollection()->getLastPageNumber() . '</span>') ?>
157+
</label>
158+
<?php endif; ?>
144159
<?php if ($_curPage < $_lastPage): ?>
145160
<button type="button"
146161
title="<?= $block->escapeHtmlAttr(__('Next page')) ?>"
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Catalog\Plugin\Ui\DataProvider\Product;
10+
11+
use Magento\Catalog\Ui\DataProvider\Product\ProductCollection;
12+
use Magento\Catalog\Ui\DataProvider\Product\ProductDataProvider as CatalogProductDataProvider;
13+
use Magento\Framework\App\Config\ScopeConfigInterface;
14+
15+
/**
16+
* Modify catalog product UI data with show total records flag.
17+
*/
18+
class ProductDataProvider
19+
{
20+
/**
21+
* @var ScopeConfigInterface
22+
*/
23+
private ScopeConfigInterface $scopeConfig;
24+
25+
/**
26+
* @param ScopeConfigInterface $scopeConfig
27+
*/
28+
public function __construct(ScopeConfigInterface $scopeConfig)
29+
{
30+
$this->scopeConfig = $scopeConfig;
31+
}
32+
33+
/**
34+
* Modify catalog product UI data with show total records flag.
35+
*
36+
* @param CatalogProductDataProvider $subject
37+
* @param array $data
38+
* @return array
39+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
40+
*/
41+
public function afterGetData(CatalogProductDataProvider $subject, array $data): array
42+
{
43+
return $this->addShowTotalRecords($data);
44+
}
45+
46+
/**
47+
* Add flag to display/hide total records found and pagination elements in product grid header.
48+
*
49+
* @param array $data
50+
* @return array
51+
*/
52+
private function addShowTotalRecords(array $data): array
53+
{
54+
if (key_exists('totalRecords', $data)) {
55+
if ($this->scopeConfig->getValue('admin/grid/calculate_approximate_total_number_of_products')
56+
&& $data['totalRecords'] >= ProductCollection::RECORDS_LIMIT) {
57+
$data['showTotalRecords'] = false;
58+
} else {
59+
$data['showTotalRecords'] = true;
60+
}
61+
}
62+
63+
return $data;
64+
}
65+
}

app/code/Magento/Catalog/Ui/DataProvider/Product/ProductCollection.php

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class ProductCollection extends \Magento\Catalog\Model\ResourceModel\Product\Col
1717
/**
1818
* Limit to display/hide total number of products in grid
1919
*/
20-
private const RECORDS_LIMIT = 20000;
20+
public const RECORDS_LIMIT = 20000;
2121

2222
/**
2323
* Disables using of price index for grid rendering
@@ -38,31 +38,35 @@ protected function _productLimitationJoinPrice()
3838
*/
3939
public function getSize()
4040
{
41-
if ($this->_scopeConfig->getValue('admin/grid/show_approximate_total_number_of_products')) {
42-
$sql = $this->getSelectCountSql();
43-
$estimatedCount = $this->analyzeCount($sql);
41+
if ($this->_totalRecords === null) {
42+
if ($this->_scopeConfig->getValue('admin/grid/calculate_approximate_total_number_of_products')) {
43+
$sql = $this->getSelectCountSql();
44+
$estimatedCount = $this->analyzeCount($sql);
4445

45-
if ($estimatedCount < self::RECORDS_LIMIT) {
46-
$columns = $sql->getPart(Select::COLUMNS);
47-
$sql->reset(Select::COLUMNS);
46+
if ($estimatedCount > self::RECORDS_LIMIT) {
47+
$columns = $sql->getPart(Select::COLUMNS);
48+
$sql->reset(Select::COLUMNS);
4849

49-
foreach ($columns as &$column) {
50-
if ($column[1] instanceof \Zend_Db_Expr && $column[1] == "COUNT(DISTINCT e.entity_id)") {
51-
$column[1] = new \Zend_Db_Expr('DISTINCT e.entity_id');
50+
foreach ($columns as &$column) {
51+
if ($column[1] instanceof \Zend_Db_Expr && $column[1] == "COUNT(DISTINCT e.entity_id)") {
52+
$column[1] = new \Zend_Db_Expr('e.entity_id');
53+
}
5254
}
55+
$sql->setPart(Select::COLUMNS, $columns);
56+
$sql->limit(self::RECORDS_LIMIT);
57+
$query = new \Zend_Db_Expr('SELECT COUNT(*) FROM (' . $sql->assemble() . ') AS c');
58+
$this->_totalRecords = (int)$this->getConnection()->query($query)->fetchColumn();
59+
if ($this->_totalRecords === self::RECORDS_LIMIT) {
60+
$this->_totalRecords = $estimatedCount;
61+
}
62+
} else {
63+
return parent::getSize();
5364
}
54-
$sql->setPart(Select::COLUMNS, $columns);
55-
$sql->limit(self::RECORDS_LIMIT);
56-
57-
$query = new \Zend_Db_Expr('SELECT COUNT(*) FROM (' . $sql->assemble() . ') AS c');
58-
59-
return $this->getConnection()->query($query)->fetchColumn();
60-
} else {
61-
return self::RECORDS_LIMIT;
65+
return $this->_totalRecords;
6266
}
67+
return parent::getSize();
6368
}
64-
65-
return parent::getSize();
69+
return $this->_totalRecords;
6670
}
6771

6872
/**

app/code/Magento/Catalog/etc/adminhtml/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,4 +290,7 @@
290290
</argument>
291291
</arguments>
292292
</type>
293+
<type name="Magento\Catalog\Ui\DataProvider\Product\ProductDataProvider">
294+
<plugin name="add_show_total_records_to_catalog_product_data" type="Magento\Catalog\Plugin\Ui\DataProvider\Product\ProductDataProvider"/>
295+
</type>
293296
</config>

app/code/Magento/Ui/view/base/web/js/grid/data-storage.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ define([
189189
result = {
190190
items: this.getByIds(request.ids),
191191
totalRecords: request.totalRecords,
192+
showTotalRecords: request.showTotalRecords,
192193
errorMessage: request.errorMessage
193194
};
194195

@@ -218,6 +219,7 @@ define([
218219
ids: this.getIds(data.items),
219220
params: params,
220221
totalRecords: data.totalRecords,
222+
showTotalRecords: data.showTotalRecords,
221223
errorMessage: data.errorMessage
222224
});
223225

app/code/Magento/Ui/view/base/web/js/grid/paging/paging.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ define([
2020
template: 'ui/grid/paging/paging',
2121
totalTmpl: 'ui/grid/paging-total',
2222
totalRecords: 0,
23+
showTotalRecords: true,
2324
pages: 1,
2425
current: 1,
2526
selectProvider: 'ns = ${ $.ns }, index = ids',
@@ -36,6 +37,7 @@ define([
3637
imports: {
3738
totalSelected: '${ $.selectProvider }:totalSelected',
3839
totalRecords: '${ $.provider }:data.totalRecords',
40+
showTotalRecords: '${ $.provider }:data.showTotalRecords',
3941
filters: '${ $.provider }:params.filters',
4042
keywordUpdated: '${ $.provider }:params.keywordUpdated'
4143
},
@@ -59,6 +61,7 @@ define([
5961
'pages': 'onPagesChange',
6062
'pageSize': 'onPageSizeChange',
6163
'totalRecords': 'updateCounter',
64+
'showTotalRecords': 'updateShowTotalRecords',
6265
'${ $.provider }:params.filters': 'goFirst',
6366
'${ $.provider }:params.search': 'onSearchUpdate'
6467
},
@@ -91,6 +94,7 @@ define([
9194
.track([
9295
'totalSelected',
9396
'totalRecords',
97+
'showTotalRecords',
9498
'pageSize',
9599
'pages',
96100
'current'
@@ -231,6 +235,16 @@ define([
231235
return this;
232236
},
233237

238+
/**
239+
* Updates number of pages.
240+
*/
241+
updateShowTotalRecords: function () {
242+
if (this.showTotalRecords === undefined) {
243+
this.showTotalRecords = true;
244+
}
245+
return this;
246+
},
247+
234248
/**
235249
* Calculates new page cursor based on the
236250
* previous and current page size values.

app/code/Magento/Ui/view/base/web/js/grid/provider.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ define([
7878
clearData: function () {
7979
this.setData({
8080
items: [],
81-
totalRecords: 0
81+
totalRecords: 0,
82+
showTotalRecords: true,
8283
});
8384

8485
return this;

0 commit comments

Comments
 (0)