Skip to content

Commit 114349f

Browse files
committed
MAGE-986 Expose InstantSearch redirect config to frontend
1 parent 8dc49d2 commit 114349f

File tree

3 files changed

+59
-15
lines changed

3 files changed

+59
-15
lines changed

Block/Configuration.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
66
use Algolia\AlgoliaSearch\Helper\InsightsHelper;
77
use Algolia\AlgoliaSearch\Model\Source\AutocompleteRedirectMode;
8+
use Algolia\AlgoliaSearch\Model\Source\InstantSearchRedirectOptions;
89
use Magento\Framework\App\Request\Http;
910
use Magento\Framework\Data\CollectionDataSourceInterface;
1011
use Magento\Framework\DataObject;
@@ -199,7 +200,14 @@ public function getConfiguration()
199200
'categoryPageIdAttribute' => $config->getCategoryPageIdAttributeName(),
200201
'isCategoryNavigationEnabled' => self::IS_CATEGORY_NAVIGATION_ENABLED,
201202
'hidePagination' => $config->hidePaginationInInstantSearchPage(),
202-
'isDynamicFacetsEnabled' => $config->isDynamicFacetsEnabled()
203+
'isDynamicFacetsEnabled' => $config->isDynamicFacetsEnabled(),
204+
'redirects' => [
205+
'enabled' => $config->isInstantRedirectEnabled(),
206+
'onPageLoad' => in_array(InstantSearchRedirectOptions::REDIRECT_ON_PAGE_LOAD, $config->getInstantRedirectOptions()),
207+
'onSearchAsYouType' => in_array(InstantSearchRedirectOptions::REDIRECT_ON_SEARCH_AS_YOU_TYPE, $config->getInstantRedirectOptions()),
208+
'showSelectableRedirect' => in_array(InstantSearchRedirectOptions::SELECTABLE_REDIRECT, $config->getInstantRedirectOptions()),
209+
'openInNewWindow' => in_array(InstantSearchRedirectOptions::OPEN_IN_NEW_WINDOW, $config->getInstantRedirectOptions())
210+
]
203211
],
204212
'autocomplete' => [
205213
'enabled' => $config->isAutoCompleteEnabled(),

Helper/ConfigHelper.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ class ConfigHelper
4747
public const INFINITE_SCROLL_ENABLE = 'algoliasearch_instant/instant_options/infinite_scroll_enable';
4848
public const HIDE_PAGINATION = 'algoliasearch_instant/instant_options/hide_pagination';
4949

50+
public const IS_INSTANT_REDIRECT_ENABLED = 'algoliasearch_instant/instant_redirects/enable';
51+
public const INSTANT_REDIRECT_OPTIONS = 'algoliasearch_instant/instant_redirects/options';
52+
5053
public const IS_POPUP_ENABLED = 'algoliasearch_autocomplete/autocomplete/is_popup_enabled';
5154
public const NB_OF_PRODUCTS_SUGGESTIONS = 'algoliasearch_autocomplete/autocomplete/nb_of_products_suggestions';
5255
public const NB_OF_CATEGORIES_SUGGESTIONS = 'algoliasearch_autocomplete/autocomplete/nb_of_categories_suggestions';
@@ -1055,7 +1058,7 @@ public function getAutocompleteDebounceMilliseconds($storeId = null): int
10551058
);
10561059
}
10571060

1058-
public function getAutocompleteMinimumCharacterLength($storeId = null): int
1061+
public function getAutocompleteMinimumCharacterLength(?int $storeId = null): int
10591062
{
10601063
return (int) $this->configInterface->getValue(
10611064
self::AUTOCOMPLETE_MINIMUM_CHAR_LENGTH,
@@ -1064,12 +1067,12 @@ public function getAutocompleteMinimumCharacterLength($storeId = null): int
10641067
);
10651068
}
10661069

1067-
public function isAutocompleteRedirectEnabled($storeId = null): bool
1070+
public function isAutocompleteRedirectEnabled(?int $storeId = null): bool
10681071
{
10691072
return $this->configInterface->isSetFlag(self::IS_AUTOCOMPLETE_REDIRECT_ENABLED, ScopeInterface::SCOPE_STORE, $storeId);
10701073
}
10711074

1072-
public function getAutocompleteRedirectMode($storeId = null): int
1075+
public function getAutocompleteRedirectMode(?int $storeId = null): int
10731076
{
10741077
return (int) $this->configInterface->getValue(
10751078
self::AUTOCOMPLETE_REDIRECT_MODE,
@@ -1083,6 +1086,22 @@ public function isAutocompleteRedirectInNewWindowEnabled($storeId = null): bool
10831086
return $this->configInterface->isSetFlag(self::AUTOCOMPLETE_OPEN_REDIRECT_IN_NEW_WINDOW, ScopeInterface::SCOPE_STORE, $storeId);
10841087
}
10851088

1089+
1090+
public function isInstantRedirectEnabled(?int $storeId = null): bool
1091+
{
1092+
return $this->configInterface->isSetFlag(self::IS_INSTANT_REDIRECT_ENABLED, ScopeInterface::SCOPE_STORE, $storeId);
1093+
}
1094+
1095+
public function getInstantRedirectOptions(?int $storeId = null): array
1096+
{
1097+
$value = $this->configInterface->getValue(
1098+
self::INSTANT_REDIRECT_OPTIONS,
1099+
ScopeInterface::SCOPE_STORE,
1100+
$storeId
1101+
);
1102+
return empty($value) ? [] : explode(',', $value);
1103+
}
1104+
10861105
/**
10871106
* @param $originalIndexName
10881107
* @param $storeId

view/frontend/web/js/instantsearch.js

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ define([
185185

186186
return allWidgetConfiguration;
187187
},
188-
188+
189189
/**
190190
* Process a passed widget config object and add to InstantSearch
191191
* Dynamic widgets are deferred as they must be aggregated and processed separately
@@ -970,9 +970,7 @@ define([
970970
customWidgets.push(this.getSuggestionsWidget(this.minQuerySuggestions));
971971
}
972972

973-
// Placeholder for config
974-
const allowRedirects = true;
975-
if (allowRedirects) {
973+
if (algoliaConfig.instant.redirects.enabled) {
976974
customWidgets.push(this.getRedirectWidget());
977975
}
978976
return customWidgets;
@@ -1061,19 +1059,38 @@ define([
10611059
};
10621060
},
10631061

1062+
isAbleToRedirect() {
1063+
return (
1064+
!this.hasInteracted && algoliaConfig.instant.redirects.onPageLoad
1065+
||
1066+
this.hasInteracted && algoliaConfig.instant.redirects.onSearchAsYouType
1067+
);
1068+
},
1069+
1070+
getSelectableRedirect(results) {
1071+
let content = `<div class="instant-redirect">`;
1072+
content += `<a href="${results.renderingContent.redirect.url}"`;
1073+
if (algoliaConfig.instant.redirects.openInNewWindow) {
1074+
content += ` target="_blank"`;
1075+
}
1076+
content += `>${algoliaConfig.translations.redirectSearchPrompt} "${results.query}"</a>`;
1077+
content += `</div>`;
1078+
return content;
1079+
},
1080+
10641081
getRedirectWidget() {
1065-
const uiComponnent = this;
10661082
return {
1067-
render({ results }) {
1083+
render: ({ results }) => {
10681084
let content = '';
10691085
if (results && results.renderingContent) {
10701086
if (results.renderingContent.redirect) {
1071-
if (!uiComponnent.hasInteracted) {
1072-
window.location.href = results.renderingContent.redirect.url;
1087+
if (this.isAbleToRedirect()) {
1088+
window.location.assign(results.renderingContent.redirect.url);
1089+
}
1090+
1091+
if (algoliaConfig.instant.redirects.showSelectableRedirect) {
1092+
content = this.getSelectableRedirect(results);
10731093
}
1074-
content += `<div class="instant-redirect">`;
1075-
content += `<a href="${results.renderingContent.redirect.url}">${algoliaConfig.translations.redirectSearchPrompt} "${results.query}"</a>`;
1076-
content += `</div>`;
10771094
}
10781095
}
10791096
document.querySelector('#instant-redirect-container').innerHTML = content;

0 commit comments

Comments
 (0)