Blog / 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 filter
    Global Null contextual filter
  • Configure when the filter value is NOT in the URL as follows:
    Global Null dialog 1
    • Select: Provide default value
    • Type: Fixed value
    • Fixed value: 1
  • Configured when the filter value IS in the URL or a default is provided as follows:
    Global Null dialog 2
    • Check: Specify validation criteria
    • Validator: PHP Code
    • PHP 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:

<?php
  $user_input = FALSE;
  foreach ($_GET as $key => $value) {
    if ($key == 'q') continue;
    if ($value != '') $user_input = TRUE;
  }
  if ($user_input):
?>
  <p>No results were found for your search.</p>
<?php else: ?>
  <p>Enter the search terms above and click submit.</p>
<?php endif; ?>

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.

<?php
  foreach ($view->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.

Comments

  • Views 3 has an 'input

    Views 3 has an 'input required' exposed form plugin.
    Just choose it.

  • Much easier

    Under Advanced

    EXPOSED FORM
    Exposed form in block:No
    Exposed form style:Input required | Settings

  • I don't believe "input

    I don't believe "input required" works if you wish to use something like Better Exposed Filters? So this was helpful.

  • Solution for Better Exposed Filters

    If you're using Better Exposed Filters, there's a patch available over at https://drupal.org/node/1447730, but by the time you read this, it may already be committed. ;)

    colans.net

  • Could you explain me where i

    Could you explain me where i have to insert the php code?

  • Thanks for the article, this

    Thanks for the article, this helped me work around the broken View Hacks "Composite" of "Better Exposed Filters" and "Input Required".

  • Thank you!

    I am using the location module for a proximity search, and "input required" was not working at all. Your code above was the only solution that worked!

  • Unfortunately doesn't work for me

    I use Page manager and Panels to override taxonomy term page. With the help of Views content panes I created a View that has some exposed filters and a contextual filter (Has taxonomy term ID (with depth)). The view works fine, but I still get "no results" page untill I submit the exposed form. Can anyone help? Thanks.

  • forgot to mention, I also use

    forgot to mention, I also use Better exposed filters.

  • Entities

    Does anyone have any experience working with Entities? Would the process be different? I can't seem to get it to work.

Leave a comment

(If you're a human, don't change the following field)
Your first name.
(If you're a human, don't change the following field)
Your first name.
(If you're a human, don't change the following field)
Your first name.

Sign up for updates