Views exposed filters in a fieldset

Public

Fill in your module name and optionally restrict by view name and have it's exposed filters appear in a collapsible fieldset that's open by default when there's active filters

Get raw version
php
  1. /**
  2.  * Implements hook_preprocess_views_view().
  3.  */
  4. function my_module_preprocess_views_view(&$vars) {
  5. if ($vars['view']->name === 'my_view') {
  6. // Wrap exposed filters in a fieldset.
  7. if ($vars['exposed']) {
  8. drupal_add_js('misc/form.js');
  9. drupal_add_js('misc/collapse.js');
  10. // Default collapsed
  11. $collapsed = TRUE;
  12. $class = array('collapsible', 'collapsed');
  13. if (count($_GET) > 1){
  14. // assume other get vars are exposed filters, so expand fieldset
  15. // to show applied filters
  16. $collapsed = FALSE;
  17. $class = array('collapsible');
  18. }
  19. $fieldset['element'] = array(
  20. '#title' => 'Refine your search',
  21. '#collapsible' => TRUE,
  22. '#collapsed' => $collapsed,
  23. '#attributes' => array('class' => $class),
  24. '#children' => $vars['exposed'],
  25. );
  26. $vars['exposed'] = theme('fieldset', $fieldset);
  27. }
  28. }
  29. }

Comments

Ken's picture

This isn't working for me. I'm current in Drupal and Views. I'm not getting any errors either.

alexweber's picture

Check your module name (line 4), your view's name (line 5) and flush caches :)

emil's picture

Thanks, helpful piece of code. Worked great for my D7 site.

oskylark's picture

Thanks. In my case I only wanted the fieldset around a few of the fields. With $vars['exposed'] being the formatted HTML I did some substring type splicing to separate out the HTML string of the required fields. $fieldset['#children'] was then defined as this string and then whole lot pieced back together e.g.
$vars['exposed'] = $fieldset_prefix_html_string . theme('fieldset', $fieldset) . $fieldset_suffix_html_string;

mgrim's picture

When your exposed filter is a block, the field html isn't contained in $vars['exposed'], where is it?