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]'.

Comments

Surf New Media’s picture

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

johnnytyranno’s picture

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.

frames’s picture

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.

¿?

lbdan’s picture

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):

// 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.

lourenzo’s picture

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

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

johnnytyranno’s picture

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

yrre7’s picture

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

yrre7’s picture

"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.

jw100’s picture

great solution!

purabdk’s picture

Using following two hooks we can change the defalut value of filter Programatically.

/**
 * hook_views_pre_view
 * @param type $view
 * @param type $display_id
 * @param type $args
 */
function MODULE_NAME_views_pre_view(&$view, &$display_id, &$args) {
  if ($view->name == 'VIEW_NAME') {
    $filters = $view->display_handler->get_option('filters');    
    $view->display_handler->override_option('filters', $filters);
  }
}



/**
 * hook__views_pre_build
 * @param type $view
 * @return type
 */
function MODULE_NAME_views_pre_build($view) { 
  if ($view->name=='VIEW_NAME') {    
    $view->display['page']->handler->handlers['filter']['filter_field']->value['value'] = 8;
    return $view;
  }     
}
gbrussel’s picture

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 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.

yrre7’s picture

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

jbfp’s picture

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 :-)

Bartezz’s picture

Thanx, this worked for me in D6!

________________
Live fast die young

charlysole’s picture

I have the same problem, but the argument Global: Null is not avaiable on arguments.
Any suggestions?

Bartezz’s picture

Should be....

Next to 'arguments' click +
Then under 'groups' click the select box and select Global, then a checkbox apears which you can check....

Cheers

________________
Live fast die young

cafescott’s picture

hi, i also can't see the Global: Null argument. I've asked my host for help, but they say it will cost extra for them to look into it.

when I expand 'Arguments' i see: Argument type, title and wildcard boxes. Below that is the Add Argument dropdown. My dropdown has many values, but the only ones that resemble 'Global: Null' are 'OG: Group Name' and 'OG group nids'. I don't think these are what you're referring to.

any help appreciated. thanks.

WorldFallz’s picture

Sounds like you might be using d5-- this is strictly views2/d6 functionality.

cafescott’s picture

thanks. sounds like i must have views2/d6.

orjantorang’s picture

Anyone know how to get empty result on first view when the view is embedded?

I have tried the above if (count($view->exposed_input))... and it works fine on the page view, but when you embed the view it never shows anything.

I have embedded it like this to make it stay on the same page view:

<?php
    $keyword_view = views_get_view('keyword_view', 'page_1', $project['id']);
    $keyword_view->override_path = $_GET['q'];
    $keyword_all = $keyword_view->preview();

    return $keyword_all;
?>

Anyone that have any idea?

capaneus’s picture

Just wanted to add that this method worked for me (Drupal 6, Views 2.12).

capaneus’s picture

This and Lourenzo's solution accomplished exactly what I needed.

Drupal 6.20/Views2

leenwebb’s picture

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.

swolynski’s picture

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

leenwebb’s picture

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.

kruser’s picture

Unfortunitly this method doesn't work with Use AJAX enabled.

-----------------------------------------------------
Bob @ Drupal Aid (https://www.drupalaid.com)

mrfelton’s picture

Or, for Drupal 6; in template.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:

--
Tom
www.systemseed.com - drupal development. drupal training. drupal support.

jbfp’s picture

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?

aangel’s picture

IIRC, the table format uses rows as well so you should just be able to change the suffix to _table, like this:

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

In my case, using the method above the results spilled to multiple pages thus triggering the pager at the bottom...but with no results atop.

This did the trick just after the $vars['rows'] = array() line:

<?php
      $vars['view']->pager['use_pager'] = "0"; // turn off pager
?>
tristanmatthews’s picture

I'm using he solution above, with the added turn pager off line, its hiding the results but not the pager, although if I set the pager to mini it seems to set it back to full... weird.

I think my problem is coming from the fact that I'm inserting the view into a node with views_embed_view, but I can't find a solution any where.

Any ideas?

Bartezz’s picture

I'm using the solution by gbrussel on a gmap view. It took me a while to find out but if you use this solution make sure to add +dynmarkers to your macro so markers are loaded (they aren't loaded be default when a view is empty). I had to patch gmap.module as well: http://drupal.org/node/315236

Hope this helps others who want to use a default empty view with gmap style display...

Cheers

________________
Live fast die young

dagmar’s picture

This behavior was recently included into views 3. You can select: Exposed Form Style: Input Required.

Bartezz’s picture

Thanx for that information. Can't wait to play with Views3 :)

________________
Live fast die young

bib_boy’s picture

I found some of the options (e.g. Ibdan's) with not quite enough details...so here is what I think works. thanks to http://www.metaltoad.com/blog/drupal-views-and-custom-search-features

In the view we add an argument of type Global: Null
In the argument configuration choose "Provide default argument"
Set the argument type to "Fixed entry"
Set the Validator to "PHP code"
PHP validate code:

if (count($view->exposed_input)) {
return TRUE;
}

The value of "$view->exposed_input" will be 0 if the form has not been submitted yet.

then:
Action to take if argument does not validate: set to 'Display Empty text'

marcushenningsen’s picture

Thanks for the well-described solution. This works for me, even with another argument in front of this NULL argument.

brad.bulger’s picture

see, that's just the opposite of what i am seeing - i had to make the Global Null argument first to get this to work. the other argument is a Taxonomy Term that defaults to all rows if no argument is given - i wonder if that's the conflict.

i am also finding that i have to expose the filter operator to get this to work - the filter is an optional multivalue select field. if i set it to only choose a single value then that works with the operator exposed or not.

the odd thing is that the "live preview" in the view edit screen always does just what i'm trying to achieve...

jfox77’s picture

bib_boy's solution (posted 12 16, 2009) worked for me but I also had to add PHP code in my Empty Text setting. So, the entire working solution for D6, Views 2 is:

In the view we add an argument of type Global: Null
In the argument configuration choose "Provide default argument"
Set the argument type to "Fixed entry"
Set the Validator to "PHP code"
PHP validate code:

if (count($view->exposed_input)) {
return TRUE;
}

The value of "$view->exposed_input" will be 0 if the form has not been submitted yet.

then:
Action to take if argument does not validate: set to 'Display Empty text'

In the Empty Text setting, use input type = PHP code and paste this:

$view = views_get_current_view(); 
  if (count($view->exposed_input)) { 
    return "Sorry, no results were found.  Please adjust your search criteria and try again."; 
} 
cancerian7’s picture

Thank you so much, Your solution worked perfectly.

Danny Englander’s picture

@jfox77 -- I had this same issue and your method worked great for me, thanks!

arisaves’s picture

Works like a charm ;p But for some reason, the message "Sorry, no results..." is being displayed twice.

weekbeforenext’s picture

Great solution and great explanation.

I also wanted to provide a link to display all results if the user wanted. By setting my link to include ?Global=All, I was able to display all of the records.

Thanks!

dchatry’s picture

Great solution, works like a charm !
Thanks!

pankajshr_jpr’s picture

Thanks a lot.. it worked like charm :)

volobar’s picture

Thanks for the link. It is very well explained :-).

neelaj82’s picture

This is exactly what I needed. Thanks !!

Amruta_dani’s picture

thanks this is really simple solution and it works.

Drupal developer

subadmin’s picture

Yes, with selection of views better exposed filter it works nicely as required.

using views 3 and better exposed filter module

Thank you

sderrick’s picture

I tried all 3 solutions given above and none result in an empty text result.

I have a view with a input field and 4 drop down selections, all exposed.

Search: Search Terms optional
Settings
Taxonomy: Term exposed
Settings
Taxonomy: Term exposed
Location: Country
(Location) Location: Province

I'm using Drupal 6.15 Views 6.x-2.8

No matter which solution I apply, I get 26 pages of results when I first load the view.

Scott

tejbir’s picture

I had also tried many things, a very simple solution is
Just put Anonymous in Usernames: box this will give only one result at least that will not annoyed with to many result and Anonymous user is always available.

Boobaa’s picture

Just added my solution to the reference Views2 snippets.

mansspams’s picture

your hanbook snippet is not working. views does not execute that php code at all :( tested by adding totaly wrong syntax which would result in major whitescreen, nothing....

Updated handbook with comment :)

UPDATE. and still, does not work with date field exposed...

5t4rdu5t’s picture

Your solution at Views 2 snippets worked like a breeze to me... I'm not using exposed date fields though.

spacereactor’s picture

try #77 and http://drupal.org/node/701144
It work for the no display content until a search is carry out but i can't get it to display "Sorry, no results were found. Please adjust your search criteria and try again." if search return empty. And how to add translate string to "Sorry, no results were found. Please adjust your search criteria and try again."

Need help for a non programmer?

aniebel’s picture

You would add that text under Basic Settings>Empty Text. This is what displays when the View returns nothings.

salientknight’s picture

I'm using CCK Field Indexer. I have tried all of the above mentioned solutions and I still get piles of crap data... not even complete sets.

Any other ideas?

amal850720’s picture

subscribe

vishalkhialani’s picture

Hi,
I am unable to solve this problem as I think its because my view is a used as a block with ajax.

Has anyone managed to get it done with a similar setup ?

Cheers,
vishal

redcrackle.com

fultonchain’s picture

I too am interested if anyone has had any luck with Views 3.

jbova’s picture

For D7, I already had a custom module configured to implement hook_views_pre_render. I was doing this to set the text to show if a result set was empty. This could also be used to display an empty result set when the page is no filters have been set.

For example, in sites/all/modules/my_module/my_module.module

function my_module_views_pre_render(&$view) {
  if ($view->name == 'my_view_name' && ( $view->current_display == 'one_of_my_displays' || $view->current_display == 'another_of_my_displays' ) && empty($view->exposed_raw_input['my_input']['filter_field'])) {
    $view->result = null;
}

My working example was for views location proximity filters, the actual code used is as follows:

function locator_alter_views_pre_render(&$view) {
  /* Define the message for an empty result set if filters have been entered */
  if ($view->name == 'dealer_locator' && $view->current_display == 'page' && count($view->result) == 0 && !empty($view->exposed_raw_input['distance']['postal_code'])) {
    $message = 'Sorry there are no dealer locations within the given proximity of that postal code. You may buy products on the official store at ..., or from any of our familiar web retailers at ...';
    drupal_set_message($message, 'status', TRUE);
  }
  /* Avoid showing all nodes if filters are not set */
  elseif ($view->name == 'dealer_locator' && ( $view->current_display == 'attachment_1' || $view->current_display == 'page' ) && empty($view->exposed_raw_input['distance']['postal_code'])) {
    $view->result = null;
  }
}
WorldFallz’s picture

Not sure what all the problems are-- for views3 on d6 the 'Input Required' option works great-- with and without ajax and whether or not the exposed form is in a separate block. I've not had any problems.

mikeaja’s picture

This thread was primarily about Views 2 I think, and the issue is relevant, just a couple of later posters mentioned Views 3.

Views 3 is in Alpha, so it is not a solution as such.

WorldFallz’s picture

alpha/beta/dev designations are relative-- views devs and alphas are a lot more stable than many modules' official releases.

FilipWitkowski’s picture

I've got a few filters exposed, such a node type, location:distance zip code,.. Normally, user could choose only some to display results, and it was working. I wanted to have blank map on the start page, so I tried to use Global:Null argument in different configurations given on this page. Now it's displaying empty text on the start page as well as any of the search filter is chosen.
I don't know, what I'm doing wrong.
I've got AJAX:no set up.
in Global: Null:
Action to take if argument is not present:Display empty text
Validator: PHP code
PHP validate code:
if (count($view->exposed_input)) {
return TRUE;
}
Action to take if argument does not validate:Display empty text

Tilo’s picture

Try: Action to take if argument is not present: Provide default argument -> Fixed entry

kabarca’s picture

Try using jQuery to hide the table by default, then show it after hitting the filter trigger.
[Using Drupal 7, Views 3]

dgastudio’s picture

This is now fixed in views 3 - there are 2 kinds of filters 'basic' and 'input required'; i believe the latter is the one you want

patrickfgoddard’s picture

In Views 3, see this comment:

http://drupal.org/node/292077#comment-2366010

amuda.narayana’s picture

Select GLOBAL : NULL field in the arguments and select the "provide default argument"
place the below code in that textarea

and select the "Display Empty Text" in the below dropdown "Action to take if argument does not validate: "
if (count($view->exposed_input)) {
return FALSE;
}

This worked for me....

NenadP’s picture

I have no arguments , i was advised arguments are now Contextual filters? However, there is no "Display Empty Text" in Contextual Filter options, when GLOBAL:NULL is selected ...

I am using 7.x-3.3 (Drupal 7)

NenadP’s picture

Solved by Dagmar suggestion: Exposed Form Style: Input Required.

Thanos Lappas’s picture

So simple!!!
Thanks!

heydemo’s picture

Works for me :)

Four Kitchens | We make content go.

kufeiko’s picture

'Input required' does the trick, but here's a situation:

I've exposed a filter on Taxonomy term:name and set it to 'Contains'. Activated Ajax, Input required and the behaviour is like this: in the beginning, nothing is shown and is perfect. Once the user types something, the results show, but if the user delete the string and leave the textarea blank - all the results are shown and I don't like that.
Anyone run into this?

Anonymous’s picture

But, i got set this view as frontpage on my site ,and if i do it this way then i am unable to log on the site through login form block ... im able to logon only through the login page... i dont know why ..

For me is the best HoverFusion solution

mark_johnson’s picture

I noticed the same thing happening in a similar situation. I have a view (Views 7.x-3.5) where I have the "Exposed Form Style" set to "Input Required". This works great as it does not show any results on page load (as expected).

However, I noticed that when I click "Apply" without typing anything into the filter (which would be similar to someone typing and then deleting what they typed), it shows results.

It would be great to have a way to only show results if there is something typed in the filter (when there is nothing in the filter, no results show - whether someone typed something and deleted it or typed nothing at all).

markconroy’s picture

Hi Mark,

You can probably achieve this, but setting the "Exposed" setting to "Input Required" and also on the filter itself, set that to "required", so no input = no output.

============

Drupal Core Maintainer for "Out of the Box" Initiative
Annertech - Web Agency of the Year.

vacho’s picture

in template.php file in theme

function my_theme_name_preprocess_views_view(&$vars) {

if ($vars['view']->name == 'my_view_name') {
if (empty($vars['view']->exposed_input) || ($vars['view']->exposed_input['field_name_field1_nid'] == 'All' && $vars['view']->exposed_input['field_name_field2_nid'] == 'All' && .... )) {
$vars['rows'] = array();
$vars['empty'] = '';
$vars['pager'] = '';
}
}
}

k_zoltan’s picture

This is IMPLEMENTED in Views you don't have to do anything

If anybody gets here by search, to save yourself some headache.

Go to the advanced settings of the views and look at the EXPOSED FORM section.
You will have there a link called Exposed form style: and as default BASICis selected.
If you click on that link, you will be able to select Input required from the popup.

And you are DONE.

PS: don't forget to save the views.

For more info module / website development visit http://drupal.reea.net

vaibhavjain’s picture

Many thanks,

This worked like a charm. Never ever explored that area.
Thanks again.

Vaibhav Jain

colan’s picture

And if you're using Better Exposed Filters, take a look at #1447730: Add 'input required' option/functionality.

Marko B’s picture

I would also expect that, but for some reason when I click apply on empty field I get "field_zip_postal=" variable in url and I get all values.

ladybug_3777’s picture

There are still some areas of views I have not yet explored and "Exposed Form" is one of them. I appreciate you pointing out the option of "Input Required". Simple and easy! Thanks

joaomachado’s picture

Setting "Input Required" changes all of the filters to "actual values will be available at runtime", and cannot get any results? Setting the Exposed form back to basic, shows all of the filters and everything works, only I get 1500 results in pages of 15. Can't seem to figure out why?

"when the only tool you have is a hammer, all problems start to look like a nail"

jemisond’s picture

I've tried numerous variations from the Global:NULL to the input required- results are STILL showing before form submission.

kalidasan’s picture

If you want to display default page to be empty,
Under EXPOSE FORM , you will find Exposed form style: Click here
Then select input required.

Thats it, by default you wont get any result until you search it using your filter.

rtackett’s picture

See this comment where Merlin provides the solution to no text showing on demand when input required:

https://drupal.org/node/1754378#comment-7362662