Skip to content

Fix minisearch not appear when disable suggestions search #30634

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
<element name="searchMiniForm" type="input" selector="#search_mini_form"/>
<element name="searchDropDownSuggestion" type="text" selector="//div[@id='search_autocomplete']/ul/li/span"/>
<element name="searchDropDownName" type="text" selector="//div[@id='search_autocomplete']//span[contains(., '{{searchQuery}}')]" parameterized="true"/>
<element name="searchMagnifierIcon" type="text" selector="//*[@id='search_mini_form']//label[@data-role='minisearch-label']"/>
</section>
</sections>
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->

<tests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/testSchema.xsd">
<test name="StorefrontVerifySearchFieldVisibilityWhenSuggestionDisabledTest">
<annotations>
<stories value="Search Term"/>
<title value="Mini search field appears if suggestions was disabled"/>
<description value="Mini search field appears if suggestions was disabled"/>
<severity value="AVERAGE"/>
<testCaseId value="MC-39443"/>
<group value="mtf_migrated"/>
</annotations>

<before>
<!-- Login as admin -->
<actionGroup ref="AdminLoginActionGroup" stepKey="loginAsAdmin"/>
<!-- Disable search suggestion -->
<magentoCLI command="config:set catalog/search/search_suggestion_enabled 0" stepKey="disableSearchSuggestion"/>

<actionGroup ref="CliCacheCleanActionGroup" stepKey="cleanCacheFirst">
<argument name="tags" value="config full_page"/>
</actionGroup>
</before>

<after>
<!-- Enable search suggestion back -->
<magentoCLI command="config:set catalog/search/search_suggestion_enabled 1" stepKey="disableSearchSuggestion"/>

<actionGroup ref="CliCacheCleanActionGroup" stepKey="cleanCacheSecond">
<argument name="tags" value="config full_page"/>
</actionGroup>

<!-- Admin logout -->
<actionGroup ref="AdminLogoutActionGroup" stepKey="logout"/>
</after>

<!-- Go to storefront home page -->
<actionGroup ref="StorefrontOpenHomePageActionGroup" stepKey="openStoreFrontHomePage"/>

<resizeWindow width="767" height="720" stepKey="resizeWindowToMobileView"/>

<click selector="{{StorefrontQuickSearchSection.searchMagnifierIcon}}" stepKey="clickOnMagnifierSearchIcon"/>

<waitForElementVisible selector="{{StorefrontQuickSearchSection.searchPhrase}}" after="clickOnMagnifierSearchIcon" stepKey="seeInputSearchActive"/>

</test>
</tests>
21 changes: 20 additions & 1 deletion app/code/Magento/Search/ViewModel/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\View\Element\Block\ArgumentInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Search\Helper\Data as SearchHelper;

/**
* View model for search
Expand All @@ -26,13 +27,31 @@ class ConfigProvider implements ArgumentInterface
*/
private $scopeConfig;

/**
* @var SearchHelper
*/
private $searchHelper;

/**
* @param ScopeConfigInterface $scopeConfig
* @param SearchHelper $searchHelper
*/
public function __construct(
ScopeConfigInterface $scopeConfig
ScopeConfigInterface $scopeConfig,
SearchHelper $searchHelper
) {
$this->scopeConfig = $scopeConfig;
$this->searchHelper = $searchHelper;
}

/**
* Retrieve search helper instance for template view
*
* @return SearchHelper
*/
public function getSearchHelperData(): SearchHelper
{
return $this->searchHelper;
}

/**
Expand Down
44 changes: 23 additions & 21 deletions app/code/Magento/Search/view/frontend/templates/form.mini.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,42 @@
* See COPYING.txt for license details.
*/

// phpcs:disable Magento2.Templates.ThisInTemplate.FoundThis
?>
<?php
/** @var $block \Magento\Framework\View\Element\Template */
/** @var $helper \Magento\Search\Helper\Data */
/** @var $escaper \Magento\Framework\Escaper */
/** @var $configProvider \Magento\Search\ViewModel\ConfigProvider */
$helper = $this->helper(\Magento\Search\Helper\Data::class);
$configProvider = $block->getData('configProvider');
/** @var $helper \Magento\Search\Helper\Data */
$helper = $configProvider->getSearchHelperData();
$allowedSuggestion = $configProvider->isSuggestionsAllowed();
$quickSearchUrl = $allowedSuggestion ? $escaper->escapeUrl($helper->getSuggestUrl()) : '';
?>
<div class="block block-search">
<div class="block block-title"><strong><?= $block->escapeHtml(__('Search')) ?></strong></div>
<div class="block block-title"><strong><?= $escaper->escapeHtml(__('Search')) ?></strong></div>
<div class="block block-content">
<form class="form minisearch" id="search_mini_form"
action="<?= $block->escapeUrl($helper->getResultUrl()) ?>" method="get">
action="<?= $escaper->escapeUrl($helper->getResultUrl()) ?>" method="get">
<div class="field search">
<label class="label" for="search" data-role="minisearch-label">
<span><?= $block->escapeHtml(__('Search')) ?></span>
<span><?= $escaper->escapeHtml(__('Search')) ?></span>
</label>
<div class="control">
<input id="search"
<?php if ($configProvider->isSuggestionsAllowed()):?>
data-mage-init='{"quickSearch":{
"formSelector":"#search_mini_form",
"url":"<?= $block->escapeUrl($helper->getSuggestUrl())?>",
"destinationSelector":"#search_autocomplete",
"minSearchLength":"<?= $block->escapeHtml($helper->getMinQueryLength()) ?>"}
}'
<?php endif;?>
data-mage-init='{
"quickSearch": {
"formSelector": "#search_mini_form",
"url": "<?= /* @noEscape */ $quickSearchUrl ?>",
"destinationSelector": "#search_autocomplete",
"minSearchLength": "<?= $escaper->escapeHtml($helper->getMinQueryLength()) ?>"
}
}'
type="text"
name="<?= $block->escapeHtmlAttr($helper->getQueryParamName()) ?>"
name="<?= $escaper->escapeHtmlAttr($helper->getQueryParamName()) ?>"
value="<?= /* @noEscape */ $helper->getEscapedQueryText() ?>"
placeholder="<?= $block->escapeHtmlAttr(__('Search entire store here...')) ?>"
placeholder="<?= $escaper->escapeHtmlAttr(__('Search entire store here...')) ?>"
class="input-text"
maxlength="<?= $block->escapeHtmlAttr($helper->getMaxQueryLength()) ?>"
maxlength="<?= $escaper->escapeHtmlAttr($helper->getMaxQueryLength()) ?>"
role="combobox"
aria-haspopup="false"
aria-autocomplete="both"
Expand All @@ -49,11 +51,11 @@ $configProvider = $block->getData('configProvider');
</div>
<div class="actions">
<button type="submit"
title="<?= $block->escapeHtml(__('Search')) ?>"
class="action search"
aria-label="Search"
title="<?= $escaper->escapeHtml(__('Search')) ?>"
class="action search"
aria-label="Search"
>
<span><?= $block->escapeHtml(__('Search')) ?></span>
<span><?= $escaper->escapeHtml(__('Search')) ?></span>
</button>
</div>
</form>
Expand Down
113 changes: 58 additions & 55 deletions app/code/Magento/Search/view/frontend/web/js/form-mini.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ define([
selectClass: 'selected',
template:
'<li class="<%- data.row_class %>" id="qs-option-<%- data.index %>" role="option">' +
'<span class="qs-option-name">' +
' <%- data.title %>' +
'</span>' +
'<span aria-hidden="true" class="amount">' +
'<%- data.num_results %>' +
'</span>' +
'<span class="qs-option-name">' +
' <%- data.title %>' +
'</span>' +
'<span aria-hidden="true" class="amount">' +
'<%- data.num_results %>' +
'</span>' +
'</li>',
submitBtn: 'button[type="submit"]',
searchLabel: '[data-role=minisearch-label]',
Expand Down Expand Up @@ -300,60 +300,63 @@ define([

if (value.length >= parseInt(this.options.minSearchLength, 10)) {
this.submitBtn.disabled = false;
$.getJSON(this.options.url, {
q: value
}, $.proxy(function (data) {
if (data.length) {
$.each(data, function (index, element) {
var html;

element.index = index;
html = template({
data: element
});
dropdown.append(html);
});

this._resetResponseList(true);

this.responseList.indexList = this.autoComplete.html(dropdown)
.css(clonePosition)
.show()
.find(this.options.responseFieldElements + ':visible');

this.element.removeAttr('aria-activedescendant');
if (this.options.url !== '') { //eslint-disable-line eqeqeq
$.getJSON(this.options.url, {
q: value
}, $.proxy(function (data) {
if (data.length) {
$.each(data, function (index, element) {
var html;

element.index = index;
html = template({
data: element
});
dropdown.append(html);
});

if (this.responseList.indexList.length) {
this._updateAriaHasPopup(true);
this._resetResponseList(true);

this.responseList.indexList = this.autoComplete.html(dropdown)
.css(clonePosition)
.show()
.find(this.options.responseFieldElements + ':visible');

this.element.removeAttr('aria-activedescendant');

if (this.responseList.indexList.length) {
this._updateAriaHasPopup(true);
} else {
this._updateAriaHasPopup(false);
}

this.responseList.indexList
.on('click', function (e) {
this.responseList.selected = $(e.currentTarget);
this.searchForm.trigger('submit');
}.bind(this))
.on('mouseenter mouseleave', function (e) {
this.responseList.indexList.removeClass(this.options.selectClass);
$(e.target).addClass(this.options.selectClass);
this.responseList.selected = $(e.target);
this.element.attr('aria-activedescendant', $(e.target).attr('id'));
}.bind(this))
.on('mouseout', function (e) {
if (!this._getLastElement() &&
this._getLastElement().hasClass(this.options.selectClass)) {
$(e.target).removeClass(this.options.selectClass);
this._resetResponseList(false);
}
}.bind(this));
} else {
this._resetResponseList(true);
this.autoComplete.hide();
this._updateAriaHasPopup(false);
this.element.removeAttr('aria-activedescendant');
}

this.responseList.indexList
.on('click', function (e) {
this.responseList.selected = $(e.currentTarget);
this.searchForm.trigger('submit');
}.bind(this))
.on('mouseenter mouseleave', function (e) {
this.responseList.indexList.removeClass(this.options.selectClass);
$(e.target).addClass(this.options.selectClass);
this.responseList.selected = $(e.target);
this.element.attr('aria-activedescendant', $(e.target).attr('id'));
}.bind(this))
.on('mouseout', function (e) {
if (!this._getLastElement() &&
this._getLastElement().hasClass(this.options.selectClass)) {
$(e.target).removeClass(this.options.selectClass);
this._resetResponseList(false);
}
}.bind(this));
} else {
this._resetResponseList(true);
this.autoComplete.hide();
this._updateAriaHasPopup(false);
this.element.removeAttr('aria-activedescendant');
}
}, this));
}, this));
}
} else {
this._resetResponseList(true);
this.autoComplete.hide();
Expand Down