The preprocess function below worked fine is D6 (thanks to @anrikun) but am having problems getting it to work in D7. Have tried a few things but with no success so far. Any suggestions...?
This is the thread solving it for D6: http://drupal.org/node/199991
Thanks for any help.

<?php
function MYTHEME_preprocess_views_view(&$vars) {
  // Wrap exposed filters in a fieldset.
  if ($vars['exposed']) {
    $element = array(
      '#title' => t('Filter'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#value' => $vars['exposed'],
    );
    $vars['exposed'] = theme('fieldset', $element);
  }
}
?> 

Comments

dawehner’s picture

So what does this code snippet do for you at the moment? Perhaps you have to call a drupal_render with #theme here.

cajmcmahon’s picture

The code allows me to expand and collapse a fieldset containing three exposed filters. It works perfectly.

I think I might have a javascript problem however, as I noticed I can no longer collapse the fieldsets on the Modules page (/admin/modules/list), nor can I expand/collapse Collapsiblocks.

In case it's a theme issue, I tried with Bartik, Seven and Zen.

I'm going deactivate any modules that I've installed in the last few days as the Modules page fieldsets did work last week.

I'll report back with any findings...

cajmcmahon’s picture

Got javascript functionality back. I just de/re-activated JS modules (also moved them out of sites/all/modules folder while doing so). Didn't learn much unfortunately.

Will continue to experiment with the theme-based preprocess function (though I think Collapsible blocks is the way to go for me on this one).

dawehner’s picture

Status: Active » Fixed

That's basically just a support question how to use the drupal7 api.
Once you would have fixed it, this isn't views specific.

cajmcmahon’s picture

That's true about D7 usage. The solution for D6 appeared in Views queue so I followed that lead. Thanks.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Vote_Sizing_Steve’s picture

Here's some (sloppy) code I put together, which seems to do the job:

<?php
function mymodule_preprocess_views_view( &$Variables ){
	if( $Variables[ 'exposed' ]){
		$Fieldset_Exposed[ 'element' ] = array(
			'#title' => t( 'Filter' ),
			'#attributes' => array( 
				'class' => array( 
					'collapsed', 
					'collapsible' )),
			'#value' => $Variables[ 'exposed' ]);
		$Variables[ 'exposed' ] = theme_fieldset( $Fieldset_Exposed ); }}
?>

... it needs some tidying in order to make it drupal compliant.

davidwhthomas’s picture

This is what worked for me in the end, in Drupal 7:

<?php
/**
 * hook_preprocess_views_view
 */
function EXAMPLE_preprocess_views_view(&$vars) {
  // Wrap exposed filters in a fieldset.
  if ($vars['exposed']) {
    // Ensure required js libs are added
    drupal_add_js('misc/form.js');
    drupal_add_js('misc/collapse.js');
      // Build fieldset element, using correct array nesting for theme_fieldset
      $fieldset['element'] = array(
        '#title' => t('Filter'),
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,      
        '#attributes' => array('class' => array('collapsible', 'collapsed')),
        '#value' => $vars['exposed'],
      );
      // Reassign exposed filter tpl var to fieldset value
      $vars['exposed'] = theme('fieldset', $fieldset);
  }
}
?>
Yuri’s picture

the code in #8 gives me this error:
Notice: Undefined index: #children in theme_fieldset() (line 2673 of /home/office/public_html/includes/form.inc).

RaF7’s picture

#7 #8: Unfortunately, to be XHTML compliant, a fieldset must be in a form, instead of around it.

Any other idea's anyone?

Yuri’s picture

As a workaround i applied the patch mentioned in http://drupal.org/node/1004852
which resulted in the #9 error message not showing up any more.

meba’s picture

Status: Closed (fixed) » Active

I think this should actually be re-opened, it is not only an issue of using D7 Forms API. The problem is that some of the exposed filter form parts such as titles are not generated as part of Forms API.

Maybe this? http://drupal.org/project/better_exposed_filters

tbrowninfinitymark’s picture

Is this a standard feature now in Drupal Commons? I do not wish to have collapsible filters (collapsed by default) but can't seem to locate a way to turn it off in D6 Commons. Can anyone point me to a solution? My searches thus far have turned up nothing.

Thanks!

ahwebd’s picture

Based on #8 here's how I done it for specific views and only for a part of the exposed filters:

/**
 * Putting advanced filters in fieldset
 */
function MYTHEME_preprocess_views_view(&$vars) {

  $target_views = array('view1', 'view2', 'view3');
  if (($vars['exposed']) && in_array($vars['view']->name, $target_views)) {
    switch ($vars['view']->name) {
      case 'view1':
      case 'view2':
        $advanced_filters_start = '
'; break; case 'view3': $advanced_filters_start = '
'; break; } if (strpos($vars['exposed'], $advanced_filters_start) !== FALSE) { $filter_parts = explode($advanced_filters_start, $vars['exposed']); $filter_parts_2 = explode($advanced_filters_end, $filter_parts[1]); $basic_filters = $filter_parts[0]; $advanced_filters = $advanced_filters_start.$filter_parts_2[0]; $filters_submit = $advanced_filters_end.$filter_parts_2[1]; // Build fieldset element $fieldset['element'] = array( '#title' => t('Advanced search'), '#attributes' => array('class' => array('avdanced-filters', 'collapsible', 'collapsed')), '#value' => $advanced_filters, '#children' => '', ); $vars['exposed'] = $basic_filters.theme('fieldset', $fieldset).$filters_submit; } } }

adding: '#children' => '', will remove the error described in #9

davidwhthomas’s picture

Here's my adjusted version from #8 to avoid the unset #children notice error:

<?php
/**
 * hook_preprocess_views_view
 */
function EXAMPLE_preprocess_views_view(&$vars) {
  // Wrap exposed filters in a fieldset.
  if ($vars['exposed']) {
    drupal_add_js('misc/form.js');
    drupal_add_js('misc/collapse.js');
    // Default collapsed
    $collapsed = TRUE;
    $class = array('collapsible', 'collapsed');
    if (count($_GET) > 1){
      // assume other get vars are exposed filters, so expand fieldset
      // to show applied filters
      $collapsed = FALSE;
      $class = array('collapsible');
    }
    $fieldset['element'] = array(
      '#title' => t('Filter'),
      '#collapsible' => TRUE,
      '#collapsed' => $collapsed,
      '#attributes' => array('class' => $class),
      '#children' => $vars['exposed'],
    );
    $vars['exposed'] = theme('fieldset', $fieldset);
  }
}
?>

Change EXAMPLE to your theme name, or module name and clear all caches.

mstef’s picture

* EXAMPLE_preprocess_views_view (remove "_content")

davidwhthomas’s picture

Thanks for the tip _content string removed from function name.

levakon’s picture

The code from #14 works nice for me, any ideas on how to add the option for filters to be expanded by default if one of them is set (for example if there are 5 filters and user inputs something in one of them, when the form is submitted the filters are not hidden again but stay open so he can simply change what he entered -> or add some other text to other filters).
and how would one go about making more than one fieldset, for grouping similar filters together.
thank you for any info

dawehner’s picture

You could store the status of the fieldset in a cookie and open this automatically via javascript once the page is loaded.jjj

mstef’s picture

The code already does that levakon. See:

 if (count($_GET) > 1){
    // assume other get vars are exposed filters, so expand fieldset
    // to show applied filters
    $collapsed = FALSE;
    $class = array('collapsible');
  }
dawehner’s picture

@mikesteff
Oh this is a nice trick! Thanks for sharing.

mstef’s picture

yea, it's a good one - thanks davidwhthomas.

Should we turn this into a feature request? Seems like a cool option for views..

levakon’s picture

mikestefff, thnx for that, i took the code from #14 and added that so i can group only some part of filters not all of them, that works well when there is only one fieldset, i'm also wondering if there is a simple way to make more than one fieldset (for example to group filters in 3 categories and can be collapsed/expanded independent from each other) and then when the results are shown to have expanded field set if at least one of the filters inside that group/fieldset was set.
(i'm guessing it would work with storing the status in a cookie or by looping the list of $_GET variables and checking which ones are set and then either set $collapsed to FALSE or TRUE.
is there a better/simpler/nicer way? (i'm always surprised how simple some things can be solved and go "oh, why didn't i think of that way...")

Yanxi’s picture

Could anybody tell me how to implement the code in #14 pleae? I have tried the followings without success.

1. Change MYTHEME to my theme's name and put these codes in my theme's template.php file;
2. As I only need to add this fieldset to one views exposed filter, so I replace ('view1', 'view2', 'view3') with ('my_view_name').
p.s- I am not sure if it is 'my_view_name' or 'my view name' or 'my-view-name' etc but I have tried them all without success.
3. remove the codes for case 'view1' and case 'view3'
4. Change 'view2' to 'my_view_name' and change "edit-myfilter2-wrapper"' to my filter's id.

Am I missing anything?

davidwhthomas’s picture

Yanxi, something like my_view_name for the view name, you'll need to clear cache to register the new preprocess function, e.g visit your theme settings page in admin and save it.

abtm’s picture

I have the same problem and after clear the cache it still doesn't work.

Yanxi’s picture

@davidwhthomas, thanks for your prompt reply. I did clear the cache and save the theme settings page, it still doesn't work. :( I put the following codes in my theme's template.php file, could you see anything wrong in it?

function mythemename_preprocess_views_view(&$vars) {

  $target_views = array('my_view_name');
  if (($vars['exposed']) && in_array($vars['view']->name, $target_views)) {
    switch ($vars['view']->name) {
      case 'my_view_name':
        $advanced_filters_start = '<div id="edit-myfirstfilter-wrapper"';
        $advanced_filters_end = '<div class="views-exposed-widget views-submit-button">';
        break;
    }
   
    if (strpos($vars['exposed'], $advanced_filters_start) !== FALSE) {
      $filter_parts = explode($advanced_filters_start, $vars['exposed']);
      $filter_parts_2 = explode($advanced_filters_end, $filter_parts[1]);
     
      $basic_filters = $filter_parts[0];
      $advanced_filters = $advanced_filters_start.$filter_parts_2[0];
      $filters_submit = $advanced_filters_end.$filter_parts_2[1];
     
      // Build fieldset element
      $fieldset['element'] = array(
        '#title' => t('Advanced search'),
        '#attributes' => array('class' => array('avdanced-filters', 'collapsible', 'collapsed')),
        '#value' => $advanced_filters,
        '#children' => '',
      );

      $vars['exposed'] = $basic_filters.theme('fieldset', $fieldset).$filters_submit;
    }
  }
}
Yanxi’s picture

Still can't make it work...Any help would be appreciated.

davidwhthomas’s picture

@Yanxi

I use the code I posted in #15

Can't comment further on the other author's snippet besides to say check your view machine name ( my_view ) is correct.

Good luck,

DT

Yanxi’s picture

@davidwhthomas: Thanks again for your reply. I tried your code before and it didn't work either. Just tried it again. Guess what I discovered......

Your code works on the exposed filter that is not exposed in block. The exposed filter I was trying to have a fieldset in was exposed in block. I also went back to try the code at #14 and it displays a fieldset but the "Advanced search" title is not collapsible. I then put your code drupal_add_js('misc/form.js'); drupal_add_js('misc/collapse.js'); in the #14 and the "Advanced search" title becomes collapsible.

So I think both your code and the code in #14 might not work on the filter exposed in block, right? If so do you know how to make it work? Thanks.

aacraig’s picture

Building on #15, I've modified it a bit so that the fieldset is inside the form, so that the resulting HTML should validate:


function EXAMPLE_preprocess_views_view(&$vars) {
  // move exposed filters into a fieldset
  if ($vars['exposed']) {
    drupal_add_js('misc/form.js');
    drupal_add_js('misc/collapse.js');
    
    // Default collapsed
    $collapsed = TRUE;
    $class = array('collapsible', 'collapsed');
    
    if (count($_GET) > 1){
      // assume other get vars are exposed filters, so expand fieldset
      // to show applied filters
      $collapsed = FALSE;
      $class = array('collapsible');
    }
    
    $matches = array();
    if (!preg_match('/(<form.+?>)(.+?)(<\/form>)/', str_replace("\n", ' ', $vars['exposed']), $matches)) {
      return;
    }

    $fieldset['element'] = array(
      '#title'        => t('Filter activities'),
      '#collapsible'  => TRUE,
      '#collapsed'    => $collapsed,
      '#attributes'   => array('class' => $class),
      '#children'     => $matches[2],
    );
    $vars['exposed'] = $matches[1] . theme('fieldset', $fieldset) . $matches[3];
  }
}
KiLLAH89’s picture

Hello,

in my theme Bootstrap the fieldset dont open. When i change $collapsed = TRUE; to $collapsed = FALSE; the fieldset dont close. Can anyone please help me out?

Thanks.

KiLLAH89’s picture

Fixed my problem by myself. Now my exposed filters, are in a modal window with bootstrap theme. See this post https://drupal.org/node/2160177 for a simple guide and code.

remoraxxx’s picture

Solved #32

Just add an id to the fieldset in order to collapse/expand it properly ( and optionally remove collapse.js and form.js in order to use the bootstrap functions instead of Drupal ones)

/**
 * Implements hook_preprocess_views_view().
 */
function mymodule_preprocess_views_view(&$vars) {
        // Wrap exposed filters in a fieldset.
        if ($vars['exposed']) {
            // Default collapsed
            $collapsed = TRUE;
            $class = array('collapsible', 'collapsed', 'view_advaced_search');
            if (count($_GET) > 1){
                // assume other get vars are exposed filters, so expand fieldset
                // to show applied filters
                $collapsed = FALSE;
                $class = array('collapsible', 'view_advaced_search');
            }
            $fieldset['element'] = array(
                '#title' => t('Filter results'),
                '#id' => 'view_advaced_search',
                '#collapsible' => TRUE,
                '#collapsed' => $collapsed,
                '#attributes' => array('class' => $class),
                '#children' => $vars['exposed'],
            );
            $vars['exposed'] = theme('fieldset', $fieldset);
        }
}
kumkum29’s picture

Hello,

is it possible to reorder exposed filters in several fieldsets, inside the master fieldset?
How to create some fieldsets, inside master fieldset? How to include exposed filters in those new fieldsets ?

Thanks.

KiLLAH89’s picture

I dont know, how to create other fieldset in a fieldset for exposed filter. But i know that exposed filters can you create in your view. To order the filters, click on the arrow by exposed filters an then on reorder. Voila you can reorder your exposed filters ;)

Hope you understand me and i hope i understand you correctly :D

Regards,

KiLLAH89

mgrim’s picture

Issue summary: View changes

When using exposed filters in a block, the $vars['exposed'] array is empty. Where are the fields contained in this situation?

Anybody’s picture

This is still an important thing for further development of views I think. I've had thousands of situation where a collapsible fieldset had helped me a lot :(

Could you please consider to add this to a future views version or make it possible via module simply?

StevenMouret’s picture

For Drupal 7

<?php
function MYTHEME_preprocess_views_view(&$vars) {
  // Wrap exposed filters in a fieldset.
  if ($vars['exposed']) {
    $element = array(
      '#title' => t('Filter'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#value' => $vars['exposed'],
      '#children' => '',
    );
    $vars['exposed'] = theme('fieldset', $element);
  }
}
?>
Pol’s picture

You can also use this module: https://www.drupal.org/project/views_ef_fieldset

It allows you to configure the title, description and attributes of the fieldset too.

osopolar’s picture

See also the issue #2450043: Submit button inside secondary fieldset of the Better Exposed Filter module if you don't need multiple fieldsets.

bwaindwain’s picture

Slight improvement on #31 and #34, change

if (count($_GET) > 1){

to

if (isset($_GET['title']) || isset($_GET['type'])) {

(or whatever your filter values are), so that clicking a table column header to sort won't expand the filter