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.
| Comment | File | Size | Author |
|---|---|---|---|
| #5 | views_ui.png | 488.16 KB | hardik_patel_12 |
Comments
Comment #3
scott weston commentedI have confirmed that these methods in docroot/core/modules/search/src/Plugin/views/filter/Search.php are *NOT* called if no keyword(s) provided:
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.
Comment #4
scott weston commentedComment #5
hardik_patel_12 commentedAfter removing the filter form configuration options UI of configuration is not so good , kindly find screenshot. We need to work on that also.
Comment #6
hardik_patel_12 commentedAnd 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.
Comment #7
lendudeAs 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)
Comment #13
smustgrave commentedAfter 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.