Skip to content

Commit

Permalink
Merge pull request #35 from Smile-SA/master
Browse files Browse the repository at this point in the history
merge master
  • Loading branch information
Fanny DECLERCK authored Aug 19, 2020
2 parents cd07d0d + 2132bbc commit 0630f3e
Showing 1 changed file with 33 additions and 20 deletions.
53 changes: 33 additions & 20 deletions Model/Autocomplete/Page/DataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Smile\ElasticsuiteCore\Helper\Autocomplete as ConfigurationHelper;
use Smile\ElasticsuiteCms\Model\ResourceModel\Page\Fulltext\CollectionFactory as CmsCollectionFactory;
use Smile\ElasticsuiteCore\Model\Autocomplete\Terms\DataProvider as TermDataProvider;
use Magento\Framework\Event\ManagerInterface as EventManager;

/**
* Catalog product autocomplete data provider.
Expand Down Expand Up @@ -71,6 +72,11 @@ class DataProvider implements DataProviderInterface
*/
protected $storeManager;

/**
* @var EventManager
*/
protected $eventManager;

/**
* @var string Autocomplete result type
*/
Expand All @@ -85,6 +91,7 @@ class DataProvider implements DataProviderInterface
* @param CmsCollectionFactory $cmsCollectionFactory Cms collection factory.
* @param ConfigurationHelper $configurationHelper Autocomplete configuration helper.
* @param StoreManagerInterface $storeManager Store manager.
* @param EventManager $eventManager Event dispatcher
* @param string $type Autocomplete provider type.
*/
public function __construct(
Expand All @@ -94,13 +101,15 @@ public function __construct(
CmsCollectionFactory $cmsCollectionFactory,
ConfigurationHelper $configurationHelper,
StoreManagerInterface $storeManager,
EventManager $eventManager,
$type = self::AUTOCOMPLETE_TYPE
) {
$this->itemFactory = $itemFactory;
$this->queryFactory = $queryFactory;
$this->termDataProvider = $termDataProvider;
$this->cmsCollectionFactory = $cmsCollectionFactory;
$this->configurationHelper = $configurationHelper;
$this->eventManager = $eventManager;
$this->type = $type;
$this->storeManager = $storeManager;
}
Expand All @@ -118,17 +127,26 @@ public function getType()
public function getItems()
{
$result = [];
$pageCollection = $this->getCmsPageCollection();
if ($pageCollection) {
foreach ($pageCollection as $page) {
$result[] = $this->itemFactory->create(
[
'title' => $page->getTitle(),
'url' => $this->storeManager->getStore()->getBaseUrl(). $page->getIdentifier(),
'type' => $this->getType(),
]
);
}
if ($this->configurationHelper->isEnabled($this->getType())) {
$pageCollection = $this->getCmsPageCollection();
if ($pageCollection) {
foreach ($pageCollection as $page) {
$item = $this->itemFactory->create(
[
'title' => $page->getTitle(),
'url' => $this->storeManager->getStore()->getBaseUrl(). $page->getIdentifier(),
'type' => $this->getType(),
]
);

$this->eventManager->dispatch(
'smile_elasticsuite_cms_search_autocomplete_page_item',
['page' => $page, 'item' => $item]
);

$result[] = $item;
}
}
}

return $result;
Expand Down Expand Up @@ -159,17 +177,12 @@ function (\Magento\Search\Model\Autocomplete\Item $termItem) {
*/
private function getCmsPageCollection()
{
$pageCollection = null;
$suggestedTerms = $this->getSuggestedTerms();
$terms = [$this->queryFactory->get()->getQueryText()];

if (!empty($suggestedTerms)) {
$terms = array_merge($terms, $suggestedTerms);
}

$pageCollection = $this->cmsCollectionFactory->create();
$pageCollection->addSearchFilter($terms);

$pageCollection->setPageSize($this->getResultsPageSize());

$queryText = $this->queryFactory->get()->getQueryText();
$pageCollection->addSearchFilter($queryText);

return $pageCollection;
}
Expand Down

0 comments on commit 0630f3e

Please sign in to comment.