queryFactory = $queryFactory; $this->itemFactory = $itemFactory; $this->limit = (int) $scopeConfig->getValue( self::CONFIG_AUTOCOMPLETE_LIMIT, ScopeInterface::SCOPE_STORE ); } /** * @inheritdoc */ public function getItems() { $collection = $this->getSuggestCollection(); $query = $this->queryFactory->get()->getQueryText(); $result = []; foreach ($collection as $item) { $resultItem = $this->itemFactory->create([ 'title' => $item->getQueryText(), 'num_results' => $item->getNumResults(), ]); if ($resultItem->getTitle() == $query) { array_unshift($result, $resultItem); } else { $result[] = $resultItem; } } return ($this->limit) ? array_splice($result, 0, $this->limit) : $result; } /** * Retrieve suggest collection for query * * @return Collection */ private function getSuggestCollection() { return $this->queryFactory->get()->getSuggestCollection(); } }