There is a (Views2) view listing any type of elements and with at least one exposed filter. The task is to show the empty text instead of the list if the filter is not set. One part of the solution can be found at forum, but is not perfect. It works fine while every part of the exposed filters are textfields - but there are views with optional filters shown as dropdowns/selects with the first, "empty" element being <All>. This value is being sent to PHP as All, so the array checked in the above code will not be empty, so the list will appear when one of the exposed filters was shown as dropdown/select, and was optional. Here is a solution which works fine even in such cases.
- Add a Global: Null argument at view edit cockpit.
- Set Action to take if argument is not present to Provide default argument.
- Set Default argument type to Fixed entry and leave textarea empty.
- Choose PHP Code from the Validator dropdown and new textarea will show up.
- Copy this code to the PHP validate code textarea (without the
<?php
and?>
):
$is_filtered = FALSE; foreach ($view->filter as $filter) { if ($filter->options['exposed']) { if (!empty($view->display[$view->current_display]->handler->handlers['filter'][$filter->options['id']]->value)) { $is_filtered = TRUE; break; } } } return $is_filtered;
- Set Action to take if argument does not validate dropdown to Display empty text.
- Set a value for Empty text in the main views dialog, for example this (with input-type = PHP code):
$view = views_get_current_view(); if (count($view->exposed_input)) { return "Sorry, no results were found. Please adjust your search criteria and try again."; }
Some explanations for the pros: the above code iterates through all the filters attached to the view, and when it finds an exposed one, it checks whether the filter's condition is set in the view's actual display. This approach adds one tiny bit to the above-mentioned one: it works fine as expected with optional, exposed filters shown as dropdowns/selects - additionally it handles the case when a the user/browser gets a field with a different name than it's real name (eg. the node's title is a person's name, so it was renamed from title to name.)
Comments
I think that instead
I think that instead of
should be
because if there are, for example, two taxonomy filters, the field option is the same for them (tid) and the value will be always from the first one - so we'll never check the second taxonomy filter.
This is correct and I changed
This is correct and I changed it in the code. With $filter->options['field'] one of my exposed filters did not work anymore.
KOBA - Drupal Webdesign & Webdevelopment
Min and Max date
When you use the filter with the between operator, you don't get a single value with
Instead you get an array.
So using your solution I just changed the conditional so it validates if one of them has data to return values, since having min or max values alone works too.
Hope it helps.
Thank you. This is just what
Thank you. This is just what I needed.
Thanks
Thanks for posting this. It helped me out today!
Thanks!
Great post - thanks for posting it, definitely saved me some time!
Toronto Website Developer
Use with HS
I am getting a list of errors when trying to use this in my view along the HS module?
This is the error:
user warning: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') count_alias' at line 1 query: SELECT COUNT(*) FROM () count_alias in /sites/all/modules/hierarchical_select/modules/hs_taxonomy_views.module on line 535.
I'm getting the same error
I'm getting the same error except line 556. :/
The default <Any> selection does not work
Great Solution:
One Problem:
The default "Any" selector does not work. If "Any" is selected by default and I click Apply, the results should show all results for that field. Currently nothing displays. I get "Sorry, no results were found. Please adjust your search criteria and try again.". If I select one of the drop down values and click Apply, this works fine by showing results that match the selection.
The solution works great except for this problem. Please help.
Did I miss something? Thanks.
YEAH! this worked!
This worked thank you so much! One modification however, it was not running the PHP code from my Empty Text, even though it was set to PHP. It just showed the code -- come to think of it, I probably needed a <?php at the start ... anyway... this worked with normal text. A standard No Search Results ... message.
Thanks again...
Oh I see what you're
Oh I see what you're saying... same here. I wonder why?
$view->get_exposed_input()
get_exposed_input() might be nicer to use as this gives us all exposed filters. And you can immideately can set an empty text.
--
Best regards
Karsten Frohwein
Place of Php Code
Where should I place this code exactly. Please explain step by step. New to drupal. Thanks.
subscribe
subscribe
dependent exposed filters
sorry to post here this but i didnt found the help any where and i think this is a little related to my following problem.
in my project i have 3 cck fields for Country,state ,and city
city is controlled by state and state is controlled by country..
i use these fields 2 times first for from address and second for to address.
now i use them in views as exposed filter for searching but there these fields are not dependent and showing the whole data .so i want to make them dependent in views filter so that when user change the value of country state list should be updated and the cities list too also is there any method or any patch to do this .
plz help this is the last point which i have to complete and getting no idea about it :(
if i can do this by custom coding to make these fields dependent plz suggest how.
Did you find a solution
I am having similar problem...i too want to conditionally expose some fields in the views filter...so did you find any solution??
you can use
you can use hook_views_pre_render to conditionally changed the exposed filter form - http://api.lullabot.com/hook_views_pre_render
pete
Toronto Website Developer
Check out the Views Hacks
Check out the Views Hacks module. Specifically, the Views Selective Exposed Filters submodule. It apparently "Restricts exposed filter values to those present in the result set." Sounds like what you're looking for.
Filter of type Select: turn "Any" into "Please select"
Very useful post!
Now the last step for filters of type Select would be to turn the default value "Any" to something that applies more in this case, like "Please select".
Checking a select list field and a number field
Hi, this is my code for dealing with this issue. I call this function in my template for a specific view in order to render $rows and $pager only if this functions returns true.
Note 1: that I use this view (the term page override one which comes with Views) both for a specific term and for a general search page of the site (using 'all' as argument). This is why I have added the first condition - I need this trick only for the second use. It may not be needed in your case.
Note 2: Not all types of filters are checked. Works for text fields, number fields and select list fields.