Skip to content

Commit 0c8b8c4

Browse files
committed
ACPT-677: Catalog product grid improvement
- Rename configuration options; - Refactor get collection size logic;
1 parent 6080365 commit 0c8b8c4

File tree

10 files changed

+39
-66
lines changed

10 files changed

+39
-66
lines changed

app/code/Magento/Backend/ViewModel/CalculateApproximateProductsNumber.php renamed to app/code/Magento/Backend/ViewModel/LimitTotalNumberOfProductsInGrid.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
use Magento\Framework\View\Element\Block\ArgumentInterface;
1212

1313
/**
14-
* Get configuration values related to approximate number of products calculation for grid.
14+
* Get configuration values related to limit total number of products in grid collection.
1515
*/
16-
class CalculateApproximateProductsNumber implements ArgumentInterface
16+
class LimitTotalNumberOfProductsInGrid implements ArgumentInterface
1717
{
1818
/**
1919
* @var ScopeConfigInterface
@@ -30,22 +30,22 @@ public function __construct(
3030
}
3131

3232
/**
33-
* Check if configuration setting to calculate approximate total number of products in grid is enabled.
33+
* Check if configuration setting to limit total number of products in grid is enabled.
3434
*
3535
* @return bool
3636
*/
37-
public function calculateApproximateProductsTotalNumber(): bool
37+
public function limitTotalNumberOfProducts(): bool
3838
{
39-
return (bool)$this->scopeConfig->getValue('admin/grid/calculate_approximate_total_number_of_products');
39+
return (bool)$this->scopeConfig->getValue('admin/grid/limit_total_number_of_products');
4040
}
4141

4242
/**
43-
* Get records threshold for approximate total number of products calculation.
43+
* Get records threshold for limit total number of products in collection.
4444
*
4545
* @return int
4646
*/
47-
public function getRecordsThreshold(): int
47+
public function getRecordsLimit(): int
4848
{
49-
return (int)$this->scopeConfig->getValue('admin/grid/records_threshold');
49+
return (int)$this->scopeConfig->getValue('admin/grid/records_limit');
5050
}
5151
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -482,18 +482,18 @@
482482
</group>
483483
<group id="grid" translate="label" type="text" sortOrder="45" showInDefault="1">
484484
<label>Admin Grids</label>
485-
<field id="calculate_approximate_total_number_of_products" translate="label comment"
485+
<field id="limit_total_number_of_products" translate="label comment"
486486
type="select" sortOrder="1" showInDefault="1" canRestore="1">
487-
<label>Calculate approximate total number of products</label>
487+
<label>Limit number of products in grid</label>
488488
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
489-
<comment>Calculate approximate total number of products in products grid.</comment>
489+
<comment>Limit total number of products in grid collection.</comment>
490490
</field>
491-
<field id="records_threshold" translate="label comment" type="text" sortOrder="2" showInDefault="1" canRestore="1">
492-
<label>Records Threshold</label>
491+
<field id="records_limit" translate="label comment" type="text" sortOrder="2" showInDefault="1" canRestore="1">
492+
<label>Records Limit</label>
493493
<validate>validate-greater-than-zero validate-number</validate>
494-
<comment>Calculate approximate total number of products if their number is greater than this threshold.</comment>
494+
<comment>Limit total number of products in grid collection if their number is greater than this value.</comment>
495495
<depends>
496-
<field id="calculate_approximate_total_number_of_products">1</field>
496+
<field id="limit_total_number_of_products">1</field>
497497
</depends>
498498
</field>
499499
</group>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
</system>
3434
<admin>
3535
<grid>
36-
<calculate_approximate_total_number_of_products>1</calculate_approximate_total_number_of_products>
37-
<records_threshold>20000</records_threshold>
36+
<limit_total_number_of_products>1</limit_total_number_of_products>
37+
<records_limit>20000</records_limit>
3838
</grid>
3939
</admin>
4040
<general>

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use Magento\Catalog\Ui\DataProvider\Product\ProductCollection;
2121
/**
2222
* @var \Magento\Backend\Block\Widget\Grid\Extended $block
2323
* @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer
24-
* @var \Magento\Backend\ViewModel\CalculateApproximateProductsNumber $viewModel
24+
* @var \Magento\Backend\ViewModel\LimitTotalNumberOfProductsInGrid $viewModel
2525
*/
2626
$numColumns = count($block->getColumns());
2727
$viewModel = $block->getViewModel();
@@ -74,8 +74,8 @@ $viewModel = $block->getViewModel();
7474
<?php $countRecords = $block->getCollection()->getSize(); ?>
7575
<div class="admin__control-support-text">
7676
<?php if (!$block->getCollection() instanceof ProductCollection
77-
|| !$viewModel->calculateApproximateProductsTotalNumber()
78-
|| $countRecords < $viewModel->getRecordsThreshold()): ?>
77+
|| !$viewModel->limitTotalNumberOfProducts()
78+
|| $countRecords < $viewModel->getRecordsLimit()): ?>
7979
<span id="<?= $block->escapeHtml($block->getHtmlId()) ?>-total-count"
8080
<?= /* @noEscape */ $block->getUiId('total-count') ?>>
8181
<?= /* @noEscape */ $countRecords ?>
@@ -134,8 +134,8 @@ $viewModel = $block->getViewModel();
134134
</button>
135135
<?php endif; ?>
136136
<?php if (!$block->getCollection() instanceof ProductCollection
137-
|| !$viewModel->calculateApproximateProductsTotalNumber()
138-
|| $countRecords < $viewModel->getRecordsThreshold()): ?>
137+
|| !$viewModel->limitTotalNumberOfProducts()
138+
|| $countRecords < $viewModel->getRecordsLimit()): ?>
139139
<input type="text"
140140
id="<?= $block->escapeHtml($block->getHtmlId()) ?>_page-current"
141141
name="<?= $block->escapeHtmlAttr($block->getVarNamePage()) ?>"

app/code/Magento/Catalog/Plugin/Ui/DataProvider/Product/ProductDataProvider.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,16 @@ public function afterGetData(CatalogProductDataProvider $subject, array $data):
4343
}
4444

4545
/**
46-
* Add flag to display/hide total records found and pagination elements in product grid header.
46+
* Add flag to display/hide total records found and pagination elements in products grid header.
4747
*
4848
* @param array $data
4949
* @return array
5050
*/
5151
private function addShowTotalRecords(array $data): array
5252
{
5353
if (key_exists('totalRecords', $data)) {
54-
if ($this->scopeConfig->getValue('admin/grid/calculate_approximate_total_number_of_products')
55-
&& $data['totalRecords'] >= $this->scopeConfig->getValue('admin/grid/records_threshold')) {
54+
if ($this->scopeConfig->getValue('admin/grid/limit_total_number_of_products')
55+
&& $data['totalRecords'] >= $this->scopeConfig->getValue('admin/grid/records_limit')) {
5656
$data['showTotalRecords'] = false;
5757
} else {
5858
$data['showTotalRecords'] = true;

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

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@ protected function _productLimitationJoinPrice()
3434
public function getSize()
3535
{
3636
if ($this->_totalRecords === null) {
37-
if ($this->_scopeConfig->getValue('admin/grid/calculate_approximate_total_number_of_products')) {
37+
if ($this->_scopeConfig->getValue('admin/grid/limit_total_number_of_products')) {
3838
$sql = $this->getSelectCountSql();
39-
$estimatedCount = $this->analyzeCount($sql);
39+
$estimatedRowsCount = $this->analyzeRows($sql);
40+
$recordsLimit = $this->_scopeConfig->getValue('admin/grid/records_limit');
4041

41-
if ($estimatedCount > $this->_scopeConfig->getValue('admin/grid/records_threshold')) {
42+
if ($estimatedRowsCount > $recordsLimit) {
4243
$columns = $sql->getPart(Select::COLUMNS);
4344
$sql->reset(Select::COLUMNS);
4445

@@ -48,12 +49,9 @@ public function getSize()
4849
}
4950
}
5051
$sql->setPart(Select::COLUMNS, $columns);
51-
$sql->limit($this->_scopeConfig->getValue('admin/grid/records_threshold'));
52+
$sql->limit($recordsLimit);
5253
$query = new \Zend_Db_Expr('SELECT COUNT(*) FROM (' . $sql->assemble() . ') AS c');
5354
$this->_totalRecords = (int)$this->getConnection()->query($query)->fetchColumn();
54-
if ($this->_totalRecords === (int)$this->_scopeConfig->getValue('admin/grid/records_threshold')) {
55-
$this->_totalRecords = $estimatedCount;
56-
}
5755
} else {
5856
return parent::getSize();
5957
}
@@ -65,41 +63,16 @@ public function getSize()
6563
}
6664

6765
/**
68-
* Analyze amount of entities in DB.
66+
* Analyze number of rows to be examined to execute the query.
6967
*
70-
* @param $sql
71-
* @return int|mixed
72-
* @throws \Zend_Db_Select_Exception
68+
* @param Select $sql
69+
* @return mixed
7370
* @throws \Zend_Db_Statement_Exception
7471
*/
75-
private function analyzeCount($sql)
72+
private function analyzeRows(Select $sql)
7673
{
7774
$results = $this->getConnection()->query('EXPLAIN ' . $sql)->fetchAll();
78-
$alias = $this->getMainTableAlias();
79-
80-
foreach ($results as $result) {
81-
if ($result['table'] == $alias) {
82-
return $result['rows'];
83-
}
84-
}
85-
86-
return 0;
87-
}
8875

89-
/**
90-
* Identify main table alias or its name if alias is not defined.
91-
*
92-
* @return string
93-
* @throws \LogicException
94-
* @throws \Zend_Db_Select_Exception
95-
*/
96-
private function getMainTableAlias()
97-
{
98-
foreach ($this->getSelect()->getPart(Select::FROM) as $tableAlias => $tableMetadata) {
99-
if ($tableMetadata['joinType'] == 'from') {
100-
return $tableAlias;
101-
}
102-
}
103-
throw new \LogicException("Main table cannot be identified.");
76+
return max(array_column($results, 'rows'));
10477
}
10578
}

app/code/Magento/Sales/view/adminhtml/layout/sales_order_create_index.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
</block>
7474
<referenceBlock name="search_grid">
7575
<arguments>
76-
<argument name="view_model" xsi:type="object">Magento\Backend\ViewModel\CalculateApproximateProductsNumber</argument>
76+
<argument name="view_model" xsi:type="object">Magento\Backend\ViewModel\LimitTotalNumberOfProductsInGrid</argument>
7777
</arguments>
7878
</referenceBlock>
7979
<block class="Magento\Sales\Block\Adminhtml\Order\Create\Items" template="Magento_Sales::order/create/items.phtml" name="items">

app/code/Magento/Sales/view/adminhtml/layout/sales_order_create_load_block_data.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
</block>
4949
<referenceBlock name="search.grid">
5050
<arguments>
51-
<argument name="view_model" xsi:type="object">Magento\Backend\ViewModel\CalculateApproximateProductsNumber</argument>
51+
<argument name="view_model" xsi:type="object">Magento\Backend\ViewModel\LimitTotalNumberOfProductsInGrid</argument>
5252
</arguments>
5353
</referenceBlock>
5454
<block class="Magento\Sales\Block\Adminhtml\Order\Create\Items" template="Magento_Sales::order/create/items.phtml" name="items">

app/code/Magento/Sales/view/adminhtml/layout/sales_order_create_load_block_search.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
</block>
1414
<referenceBlock name="search.grid">
1515
<arguments>
16-
<argument name="view_model" xsi:type="object">Magento\Backend\ViewModel\CalculateApproximateProductsNumber</argument>
16+
<argument name="view_model" xsi:type="object">Magento\Backend\ViewModel\LimitTotalNumberOfProductsInGrid</argument>
1717
</arguments>
1818
</referenceBlock>
1919
</referenceContainer>

app/code/Magento/Sales/view/adminhtml/layout/sales_order_create_load_block_search_grid.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
</referenceContainer>
1313
<referenceBlock name="serach.grid">
1414
<arguments>
15-
<argument name="view_model" xsi:type="object">Magento\Backend\ViewModel\CalculateApproximateProductsNumber</argument>
15+
<argument name="view_model" xsi:type="object">Magento\Backend\ViewModel\LimitTotalNumberOfProductsInGrid</argument>
1616
</arguments>
1717
</referenceBlock>
1818
</body>

0 commit comments

Comments
 (0)