Skip to content

Commit

Permalink
Add attribute in autocomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreGauthier committed Dec 4, 2024
1 parent 1d506ca commit 5d80331
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/Resources/config/services/search.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,11 @@ services:
- '@Gally\OroPlugin\Config\ConfigManager'
tags:
- { name: kernel.event_listener, event: Oro\Bundle\ProductBundle\Event\ProcessAutocompleteDataEvent, method: onProcessAutocompleteData }

Gally\OroPlugin\Search\Autocomplete\Attribute:
arguments:
- '@Gally\OroPlugin\Service\ContextProvider'
- '@Gally\OroPlugin\Config\ConfigManager'
- '@router'
tags:
- { name: kernel.event_listener, event: Oro\Bundle\ProductBundle\Event\ProcessAutocompleteDataEvent, method: onProcessAutocompleteData }
17 changes: 17 additions & 0 deletions src/Resources/public/default/templates/search-autocomplete.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,23 @@
</a>
</li>
<% }) %>
<%_.each(attributes, function(attribute) { %>
<li class="search-autocomplete__item" role="option">
<a href="<%- attribute.url %>" class="search-autocomplete-category">
<div class="search-autocomplete-category__image">
<span class="fa-tag fa--no-offset"></span>
</div>
<div class="search-autocomplete-category__info">
<div class="search-autocomplete-category__head">
<%= attribute.field %>
</div>
<div class="search-autocomplete-category__body">
<%= attribute.label %>
</div>
</div>
</a>
</li>
<% }) %>
<li class="search-autocomplete__item" role="option">
<button class="btn btn--link btn--no-offset search-autocomplete__submit"
type="submit"
Expand Down
3 changes: 3 additions & 0 deletions src/Resources/views/layouts/default/config/jsmodules.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ aliases:
dynamic-imports:
orodatagrid:
- oro/filter/gally-filter
map:
'*':
oroproduct/templates/search-autocomplete.html: gallyoro/default/templates/search-autocomplete.html
68 changes: 68 additions & 0 deletions src/Search/Autocomplete/Attribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Gally to newer versions in the future.
*
* @package Gally
* @author Gally Team <[email protected]>
* @copyright 2024-present Smile
* @license Open Software License v. 3.0 (OSL-3.0)
*/

declare(strict_types=1);

namespace Gally\OroPlugin\Search\Autocomplete;

use Gally\OroPlugin\Config\ConfigManager as GallyConfigManager;
use Gally\OroPlugin\Service\ContextProvider;
use Oro\Bundle\ProductBundle\Event\ProcessAutocompleteDataEvent;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

/**
* Adds category aggregation to product autocomplete.
*/
class Attribute
{
public function __construct(
private ContextProvider $contextProvider,
private GallyConfigManager $gallyConfigManager,
private UrlGeneratorInterface $urlGenerator,
) {
}

public function onProcessAutocompleteData(ProcessAutocompleteDataEvent $event): void
{
$websiteId = $this->contextProvider->getCurrentWebsite()->getId();
if (!$this->gallyConfigManager->isGallyEnabled($websiteId)) {
return;
}

$searchQuery = $this->contextProvider->getRequest()->getSearchQuery();
$attributeData = [];
foreach ($this->contextProvider->getResponse()->getAggregations() as $aggregationData) {
foreach ($aggregationData['options'] as $option) {
$params = [
'f' => [$aggregationData['field'] => ['value' => [$option['value']]]],
'g' => ['search' => $searchQuery],
];
$url = $this->urlGenerator->generate(
'oro_product_frontend_product_search',
[
'search' => $searchQuery,
'grid' => ['frontend-product-search-grid' => http_build_query($params)],
]
);
$attributeData[] = [
'field' => $aggregationData['label'],
'label' => $option['label'],
'url' => $url,
];
}
}

$data = $event->getData();
$data['attributes'] = $attributeData;
$event->setData($data);
}
}
7 changes: 5 additions & 2 deletions src/Search/SearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,12 @@ protected function doSearch(Query $query, array $context = [])
{
$request = $this->requestBuilder->build($query, $context);
$response = $this->searchManager->search($request);
$this->contextProvider->setResponse($response);

$results = [];
if ('product' === $request->getMetadata()->getEntity()) {
$this->contextProvider->setRequest($request);
$this->contextProvider->setResponse($response);
}

foreach ($response->getCollection() as $item) {
$item['id'] = (int) basename($item['id']);

Expand Down
12 changes: 12 additions & 0 deletions src/Service/ContextProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

use Gally\OroPlugin\Indexer\Provider\CatalogProvider;
use Gally\Sdk\Entity\LocalizedCatalog;
use Gally\Sdk\GraphQl\Request;
use Gally\Sdk\GraphQl\Response;
use Oro\Bundle\ConfigBundle\Config\ConfigManager;
use Oro\Bundle\LocaleBundle\Entity\Localization;
Expand All @@ -28,6 +29,7 @@
class ContextProvider
{
private bool $isGallyContext = false;
private Request $request;
private Response $response;
private ?string $priceFilterUnit = null;
private bool $isAutocompleteContext = false;
Expand Down Expand Up @@ -77,6 +79,16 @@ public function getCurrentContentNode(): ?ContentNodeInterface
return $isCategoryNode ? $this->requestWebContentVariantProvider->getContentVariant()?->getNode() : null;
}

public function getRequest(): Request
{
return $this->request;
}

public function setRequest(Request $request): void
{
$this->request = $request;
}

public function getResponse(): Response
{
return $this->response;
Expand Down

0 comments on commit 5d80331

Please sign in to comment.