1

I found an article that can collapse exposed filters into fieldset. But this will change all Views. How can I set below code for specific Views only? For example, I wanna use this on ViewsA only.

<?php
/**
 * hook_preprocess_views_view
 */
function YOUR_MODULE_NAME_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);
  }
}
?>
0

This code will work for specific view. Example for "ViewsA"(reaplce ViewsA for your Views name and "my_module" for your module name).

<?php
    /**
     * Implements hook_preprocess_views_view().
     */
    function my_module_preprocess_views_view(&$vars) {
     // Assign to your specific view.
      if ($vars['view']->name === 'ViewsA') {
        // 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);
        }
      }
    }
?>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.