Suppressing Views Exposed Filter Output Until User Enters Search Terms

The exposed filter option in the Drupal Views module works well for creating specialized searches, but there’s a challenge. A typical search doesn’t display any results until the user enters the search terms. Views filters, as the term filter implies, displays all results until the filter values are entered. Is there a simple way of making Exposed filters behave more like search and display nothing until search/filter terms are entered? Yes.This solution is a Views 3 update of the solution provided in these Drupal Forum comments: http://drupal.org/node/358546#comment-1314862, http://drupal.org/node/358546#comment-1330806. It works well for text fields, you’ll need to adapt it if you’re using drop down select widgets or you have something else returning GET parameters.View output is suppressed or displayed using the Views Null contextual filter and custom PHP code. The Null filter uses the Views input validation feature to control whether the regular search output or the “No results” page is displayed; returning TRUE displays the output, returning FALSE displays the “No results” page.This is how it’s done:Add a Global: Null contextual filterConfigure when the filter value is NOT in the URL as follows:Select:ÊProvide default valueType: Fixed valueFixed value: 1Configured when the filter value IS in the URL or a default is provided as follows:Check: Specify validation criteriaValidator: PHP CodePHP validate code: foreach ($_GET as $key => $value) { if ($key == ‘q’) continue; if ($value != ”) return TRUE; } Action to take if filter value does not validate: Display contents of “No results found”The GET parameters are used to determine if the user has entered filter/search terms. If the user has not entered anything, the output is suppressed.You may also want to use different text in the “No results found”Êtext based on no search terms or no results. Using the “PHP code” input format and the following code accomplishes this: $value) { if ($key == ‘q’) continue; if ($value != ”) $user_input = TRUE; } if ($user_input): ?>

No results were found for your search.

Enter the search terms above and click submit.

There is alternative PHP code, shown below, that can be used as the PHP validation code. It uses data from the view object to determine if the user has entered input. If your user input is not exposed in the GET parameters this option will work. It only works as validation code, it does not work for detecting user input in the “No results found” text block. get_exposed_input() as $field => $value) { if ($value != ”) return TRUE; } ?> To have consistent code in both the validation and the “No results found” section I chose to use the $_GET array for determining if there was input.

 

  • Author: Dale McGladdery

© 2023 Agentic Digital Media

You can enable/disable right click