Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DataProvider.php
Go to the documentation of this file.
1 <?php
8 
15 
20 {
24  const CONFIG_AUTOCOMPLETE_LIMIT = 'catalog/search/autocomplete_limit';
25 
31  protected $queryFactory;
32 
38  protected $itemFactory;
39 
45  protected $limit;
46 
52  public function __construct(
55  ScopeConfig $scopeConfig
56  ) {
57  $this->queryFactory = $queryFactory;
58  $this->itemFactory = $itemFactory;
59 
60  $this->limit = (int) $scopeConfig->getValue(
61  self::CONFIG_AUTOCOMPLETE_LIMIT,
63  );
64  }
65 
69  public function getItems()
70  {
71  $collection = $this->getSuggestCollection();
72  $query = $this->queryFactory->get()->getQueryText();
73  $result = [];
74  foreach ($collection as $item) {
75  $resultItem = $this->itemFactory->create([
76  'title' => $item->getQueryText(),
77  'num_results' => $item->getNumResults(),
78  ]);
79  if ($resultItem->getTitle() == $query) {
80  array_unshift($result, $resultItem);
81  } else {
82  $result[] = $resultItem;
83  }
84  }
85  return ($this->limit) ? array_splice($result, 0, $this->limit) : $result;
86  }
87 
93  private function getSuggestCollection()
94  {
95  return $this->queryFactory->get()->getSuggestCollection();
96  }
97 }
__construct(QueryFactory $queryFactory, ItemFactory $itemFactory, ScopeConfig $scopeConfig)