Show result list of a view with exposed filters only when filters set

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 drupal.org's 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.
- Choose PHP Code from the Validator dropdown.
- Copy this code to the PHP validate code textarea:
$is_filtered = FALSE; foreach ($view->filter as $filter) { if ($filter->options['exposed']) { if (!empty($view->display[$view->current_display]->handler->handlers['filter'][$filter->options['field']]->value)) { $is_filtered = TRUE; break; } } } return $is_filtered; - Set Action to take if argument does not validate dropdown to Display empty text.
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.)
- 1114 Budapest, Kosztolányi Dezső tér 12. II/1a.
- +36 20 3891634, +36 30 2995579
- info@kybest.hu

Comments
Drupal.org documentation
This article has been added to drupal.org documentation, too.