[Solved] set views exposed filters default to empty result set

NickFranceschina - January 14, 2009 - 04:08

I have searched for hours and cant find this... and I have to believe its possible and my search skillz are inadequate and/or I just dont understand how to make it work in Views

I have a view setup with two exposed filters... which appear as drop-downs on my form. I have them set as "optional" so that the first item in the list is "-ANY-". When I go to the form it automatically runs the filters as "-ANY-"... but I dont want the view to run until the user clicks the "apply" button.

put simply: I want the default page to be empty, and then only do the filtering when the user initiates it. but it seems that views wants to default to "show all"

I cannot figure out how to do this... someone must know!!

Edited by WorldFallz - added '[Solved]'.

Same issue

MaineWebFX - February 12, 2009 - 02:27

I am having the same issue ... does anyone know a solution to this?

Default to no results for textbox exposed filter

johnnytyranno - March 2, 2009 - 18:39

I am also interested in a default to empty results but with an empty text box exposed filter. Hopefully, the solution will be the same as for drop downs.

I must be missing something ...

frames - March 2, 2009 - 19:41

I have not played much with exposed filters before, so I set a couple of them as you describe on a test server. Both filters are optional. Both show up as "Any" by default.

I get a blank page when I visit that page. And get no results until I click on the Apply button.

I even set the first filter to "Remember". The selection is actually there (instead of Any) the next time I visit that page, but I do not get any results until I hit Apply.

¿?

This worked for me

lbdan - March 4, 2009 - 02:27

Nick, this worked for me:

Add an argument of type "Global: Null". (This argument doesn't alter any behaviour directly).

Configure the new argument to:

  • display all values if the argument is not present
  • display empty text if the argument does not validate

Now change the validator to the following PHP code (without the opening and closing PHP tags of course):

<?php
// The exposed_input array has values only when
// the exposed filters have been submitted.
if (count($view->exposed_input)) {
  return
TRUE;
}
?>

When you visit your view with any argument (e.g. mysite.com/myview/start) the validator will run, find exposed_input empty and display your view's empty text. Once you hit submit on the filter it'll run as usual.

In my case I can live with the extra argument. Hopefully you can too, or at least this'll give you something to start from.

A Solution Based on Ibdan's

lourenzo - March 8, 2009 - 15:26

I had to configure a view like this too, and this solution above was the simplest and easiest.

But I had to append and random argument to the URL.
As well, when I submitted empty filters the $view->exposed_input was populated with empty values, and a many-to-many filter with a select widget was sending an 'All' value. So, all results were shown again.
In addition, the empty message was something like 'No nodes match this filtering', and again the goal was to set the empty message to nothing when no filter was set.

And I hanted all this stuff to work different.

So, I added an argument of type "Global: Null" too, but configured it to:

  • Use a default argument when not present - and by setting this to any value you enable the validation PHP hack on the actual view URL
  • display empty text if the argument does not validate

Than I've changed the validator to the following

<?php
foreach ($view->get_exposed_input() as $filter=>$value){
 
// the key field_SOMETHING_value_many_to_one only represents a random many-to-one Filter
 
if ($value!=='' && ($key!='field_SOMETHING_value_many_to_one' && $value!='All'))
    return
TRUE;
}
// This sets a blank value to the page_1 display's empty text when no filter is set
// Remember to modify it if reusing this code on a display with a different ID
$view->display['page_1']->handler->set_option('empty','');
return
FALSE;
?>

That's it!

--
Lourenzo Ferreira
http://lourenzo.blog.br

Solution worked!

johnnytyranno - March 25, 2009 - 06:34

Thanks lourenzo and lbdan! Lourenzo's solution worked on my site.

Does anyone know how to make

yrre7 - April 6, 2009 - 23:51

Does anyone know how to make this work for drupal 5's view?

"Use a default argument when

yrre7 - May 27, 2009 - 18:08

"Use a default argument when not present - and by setting this to any value you enable the validation PHP hack on the actual view URL"

I don't quite get this, is it possible to explain it more?

Thanks so much for your time.

The following will work with

gbrussel - April 18, 2009 - 20:30

The following will work with drop down filters:

Use the "Global: Null" argument as lbdan mentioned, but have these changes:

If the argument is not present, provide a default argument of PHP Code and put this in there (no

<?php

?>
tags):

<?php
if (count($view->exposed_input)) {
  return
FALSE;
}
?>

Otherwise his post is correct. This leaves an empty result set on page load...but selecting any filter in the drop down will give you the results. Hope this helps someone.

I am not sure if this work

yrre7 - April 21, 2009 - 06:32

I am not sure if this work for Drupal 5. Can someone clarify this?
Thanks,

These solutions of putting

jbfp - July 6, 2009 - 19:43

These solutions of putting arguments into the view don't seem to be working for my own view in Drupal 6. I am using multiple exposed filters and displaying multiple fields in the view results--will this have any effect on whether or not the codes work?

And for those who are editing the template.php file is this a secure alternative to the "arguments" solution?

Thank you,

Jessica :-)

Another solution is to create

leenwebb - April 24, 2009 - 18:18

Another solution is to create a custom display tpl for your view, and then put an if() around the part that displays the rows. Like so:

if ($rows){
if ($_GET['field_that_is_in_my_filter']){?>
    <div class="city-matches">
      <?php print $rows; ?>
    </div>
  <?php }

The first time the page is displayed that value won't exist in $_GET, but it will once the filter is run.

Thanks

izzyw222 - May 20, 2009 - 18:09

Thanks. I just implemented leenwebb's solution and it worked great...

FYI -- For some reason that

leenwebb - May 20, 2009 - 19:48

FYI -- For some reason that if() worked perfectly unless the field-I-was-filtering-on *wasn't* set to "ANY". Which is totally weird, but I was under a deadline and was too lazy to figure it out properly.

I fixed it by making it if (field1 || field2), as sort of a failsafe situation. Just in case you try using the if() and then at some point you mysteriously get no results, that might be why.

Or, for Drupal 6; in

mrfelton - June 12, 2009 - 09:39

Or, for Drupal 6; in template.php

<?php
function mytheme_preprocess_views_view_list(&$vars) {
  if (
$vars['view']->name == 'my_view') {
    if (empty(
$vars['view']->exposed_input)) {
     
$vars['rows'] = array();
    }
  }
}
?>

EDIT... the above only apples to the view if it's display style is set to list. You can use mytheme_preprocess_views_view to do the same for any display style:

Mrfelton, I'm not seeing the

jbfp - July 6, 2009 - 20:29

Mrfelton, I'm not seeing the alternative code for different view types. Do you have template code that would do the same thing, but for tables?

 
 

Drupal is a registered trademark of Dries Buytaert.