Front-end engineer with a passion for learning something new every day

Using Drupal Views Arguments to Show Results Only When a Filter is Used

Note this applies to Views 2 for Drupal 6, this may be easier for Views 3 / Drupal 7 but I have not tried it yet.

Recently I was configuring a Drupal view for a client's website that consisted of the user inputting a zip code and distance to find a store near them. I was using an exposed proximity search filter with a postal code in combination with the Gmap and Location Modules.

Using Drupal Views Arguments to Show Results Only When a Filter is Used

Using Drupal Views Arguments to Show Results Only When a Filter is Used

One thing I noticed was that store location listings showed in the view results as a default when the page was first loaded. I thought this would be confusing for the user as I wanted to show no results until the user entered a zip code and distance, then clicked search.

How it's done

I discovered a nice little trick to accomplish this using PHP and a views argument called "Global Null". Essentially the argument hides any results until the user acts upon the exposed filters, in this case a zip code and distance is entered and searched. If there are no results after the user searches, we also display a message in the View's empty text telling the user to refine their search.

Here is the way I configured the argument for the view, you can also look at the screen captures in this post. Note this assumes you have some kind of exposed filter for the user to act upon such as a drop down menu or a text box. In my case I have text boxes to enter a zip code and proximity distance.


  1. Add a new argument in your view
  2. Choose type "Global: Null"
  3. "Action to take if argument is not present:" - Provide default argument
  4. "Default argument type:" - Fixed entry
  5. "Validator:" – PHP Code – enter the php code below but do not use the tag wrapped around the code. if (count($view->exposed_input)) { return TRUE; }
  6. "Action to take if argument does not validate:" – Display empty text
  7. Now click to update this argument
  8. paste this code into the " Empty text:" area under basic settings for the view. (set the input format the "PHP" and this time you would use the tag wrapped around the code below.

$view = views_get_current_view();
  if (count($view->exposed_input)) {
    return "<h3>Sorry, no results were found.  Please adjust your search criteria and try again.</h3>";

This post was based upon Views 2 in combination with Drupal 6 so your mileage may vary depending on what versions you are using.



Read other blog posts