Skip to content

Commit e147c68

Browse files
author
Oleksii Korshenko
authored
MAGETWO-82423: Add The Ability To Limit Autocomplete Results #11572
2 parents 04ce39f + ecfdca1 commit e147c68

File tree

4 files changed

+44
-3
lines changed

4 files changed

+44
-3
lines changed

app/code/Magento/CatalogSearch/Model/Autocomplete/DataProvider.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,16 @@
1010
use Magento\Search\Model\QueryFactory;
1111
use Magento\Search\Model\Autocomplete\DataProviderInterface;
1212
use Magento\Search\Model\Autocomplete\ItemFactory;
13+
use Magento\Framework\App\Config\ScopeConfigInterface as ScopeConfig;
14+
use Magento\Store\Model\ScopeInterface;
1315

1416
class DataProvider implements DataProviderInterface
1517
{
18+
/**
19+
* Autocomplete limit
20+
*/
21+
const CONFIG_AUTOCOMPLETE_LIMIT = 'catalog/search/autocomplete_limit';
22+
1623
/**
1724
* Query factory
1825
*
@@ -27,16 +34,29 @@ class DataProvider implements DataProviderInterface
2734
*/
2835
protected $itemFactory;
2936

37+
/**
38+
* Limit
39+
*
40+
* @var int
41+
*/
42+
protected $limit;
43+
3044
/**
3145
* @param QueryFactory $queryFactory
3246
* @param ItemFactory $itemFactory
3347
*/
3448
public function __construct(
3549
QueryFactory $queryFactory,
36-
ItemFactory $itemFactory
50+
ItemFactory $itemFactory,
51+
ScopeConfig $scopeConfig
3752
) {
3853
$this->queryFactory = $queryFactory;
3954
$this->itemFactory = $itemFactory;
55+
56+
$this->limit = (int) $scopeConfig->getValue(
57+
self::CONFIG_AUTOCOMPLETE_LIMIT,
58+
ScopeInterface::SCOPE_STORE
59+
);
4060
}
4161

4262
/**
@@ -58,7 +78,7 @@ public function getItems()
5878
$result[] = $resultItem;
5979
}
6080
}
61-
return $result;
81+
return ($this->limit) ? array_splice($result, 0, $this->limit) : $result;
6282
}
6383

6484
/**

app/code/Magento/CatalogSearch/Test/Unit/Model/Autocomplete/DataProviderTest.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ class DataProviderTest extends \PHPUnit\Framework\TestCase
3030
*/
3131
private $suggestCollection;
3232

33+
/**
34+
* @var integer
35+
*/
36+
private $limit = 3;
37+
3338
protected function setUp()
3439
{
3540
$helper = new ObjectManager($this);
@@ -60,11 +65,20 @@ protected function setUp()
6065
->setMethods(['create'])
6166
->getMock();
6267

68+
$scopeConfig = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class)
69+
->setMethods(['getValue'])
70+
->disableOriginalConstructor()
71+
->getMockForAbstractClass();
72+
$scopeConfig->expects($this->any())
73+
->method('getValue')
74+
->willReturn($this->limit);
75+
6376
$this->model = $helper->getObject(
6477
\Magento\CatalogSearch\Model\Autocomplete\DataProvider::class,
6578
[
6679
'queryFactory' => $queryFactory,
67-
'itemFactory' => $this->itemFactory
80+
'itemFactory' => $this->itemFactory,
81+
'scopeConfig' => $scopeConfig
6882
]
6983
);
7084
}
@@ -103,8 +117,10 @@ public function testGetItems()
103117
->will($this->returnValue($expected));
104118

105119
$this->itemFactory->expects($this->any())->method('create')->willReturn($itemMock);
120+
106121
$result = $this->model->getItems();
107122
$this->assertEquals($expected, $result[0]->toArray());
123+
$this->assertEquals($this->limit, count($result));
108124
}
109125

110126
private function buildCollection(array $data)

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
<comment>Number of popular search terms to be cached for faster response. Use “0” to cache all results after a term is searched for the second time.</comment>
3333
<validate>validate-digits</validate>
3434
</field>
35+
<field id="autocomplete_limit" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
36+
<label>Autocomplete Limit</label>
37+
<validate>validate-digits</validate>
38+
</field>
3539
</group>
3640
</section>
3741
</system>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<min_query_length>1</min_query_length>
1717
<max_query_length>128</max_query_length>
1818
<max_count_cacheable_search_terms>100</max_count_cacheable_search_terms>
19+
<autocomplete_limit>8</autocomplete_limit>
1920
</search>
2021
</catalog>
2122
</default>

0 commit comments

Comments
 (0)