Prerequisite
Install Drupal core on standard profile.

Problem
In View, Keyword Search filter's "On empty input" setting appears to have no effect.
This is discovered in issue https://www.drupal.org/project/drupal/issues/2733293#comment-11260399 by @jhodgdon. I am moving it to a new issue so it can be fixed properly

Steps to reproduce
a) Installed with Standard profile.
b) Created two Basic Page content items, with title/body "My first item" and "My second item".
c) Went to admin/config/system/cron and clicked Run cron (to make sure search index is updated).
c) Created a new view showing Content, with a Page display.
d) Added a filter to the view, added a Search Keywords filter. Chose "On empty input" -> "Show none". [This seems to be specific to just Search filters.] Did not check Required.
e) Cloned the page, and checked Required for the filters.
f) Saved the view.
g) Visit the pages:

1) Search filter / not required. all content is shown, no error message, when I first visit the page. If I enter nothing and click Apply for the filter, still all content is shown.

2) Search filter / required. when I visit the page, the filter field is outlined in red, with no error message. No nodes are shown. If I attempt to submit the form without entering something in the field, I get an error box under the field that says "Please fill out the field".

Expected behavior
The Search filter's "On empty input" setting appears to have no effect. I think this setting is just misleading and should be removed. Empty input is never recognized by Views filters -- the filter won't be invoked at all, as far as I can tell.

Comments

Weilinggu created an issue. See original summary.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

scott weston’s picture

Assigned: Unassigned » scott weston
StatusFileSize
new1.37 KB

I have confirmed that these methods in docroot/core/modules/search/src/Plugin/views/filter/Search.php are *NOT* called if no keyword(s) provided:

  • init()
  • queryParseSearchExpression()
  • query()

Therefore the filter options have no effect.

I concur that "On empty input" filter form configuration options can be removed, and rely on the exposed form Required field checkbox instead to ensure that a keyword is populated before showing results.

The attached patch removes the filter form configuration options.

scott weston’s picture

Assigned: scott weston » Unassigned
Status: Active » Needs review
hardik_patel_12’s picture

StatusFileSize
new488.16 KB

After removing the filter form configuration options UI of configuration is not so good , kindly find screenshot. We need to work on that also.

hardik_patel_12’s picture

And in query() function logic for "$required = TRUE;" There is implementation of if else but i think if search textbox is empty or non empty , whether string matches or non-matches $required variable never set as TRUE. @Scott Weston can you verify that , view is never entering into following condition

if ($required) {
if ($this->operator == 'required') {
$this->query->addWhere($this->options['group'], 'FALSE');
}
}

in query() function.

lendude’s picture

Priority: Minor » Normal
Issue tags: +Needs tests
+++ b/core/modules/search/src/Plugin/views/filter/Search.php
@@ -147,9 +122,7 @@ public function query() {
     if ($required) {

As you can see this would only be used if you set this to being a required field, but if you make the filter required and leave the input empty the form api will trigger an error. So I don't see this working without some way to override the default 'required' handling.

But I'm not 100% convinced we should remove this functionality. I've had plenty of projects where there was a requirement for search to show up either filled or empty depending on if a term was provided. So maybe we should look into fixing it before we decide to rip it out (maybe ripping it out is the best plan but lets make sure we can't just fix it first)

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

After reading the description this appears to be a duplicate of https://www.drupal.org/project/drupal/issues/2568889

If you feel this is a separate issue please reopen.