For D7 check #4 This solution is for D6.
This is how you make a Grouping Field and its content collapsible:

  • Go the configuration of the View on which you want to apply this method.
  • After clicking Theme: Information ("Basic Settings" box), click on "Style output".
  • Copy paste the provided code, and save it in your theme folder as "views-view-unformatted.tpl.php" or another filename as suggested next to the Style output link.
  • The code you just pasted looks like this:

<?php
// $Id: views-view-unformatted.tpl.php,v 1.6 2008/10/01 20:52:11 merlinofchaos Exp $
/**
 * @file views-view-unformatted.tpl.php
 * Default simple view template to display a list of rows.
 *
 * @ingroup views_templates
 */
?>
<?php if (!empty($title)): ?>
  <h3><?php print $title; ?></h3>
<?php endif; ?>
<?php foreach ($rows as $id => $row): ?>
  <div class="<?php print $classes[$id]; ?>">
    <?php print $row; ?>
  </div>
<?php endforeach; ?>

Modify this code to this (copy paste it if you like):

<?php
// $Id: views-view-unformatted.tpl.php,v 1.6 2008/10/01 20:52:11 merlinofchaos Exp $
/**
 * @file views-view-unformatted.tpl.php
 * Default simple view template to display a list of rows.
 *
 * @ingroup views_templates
 */
?>

<?php drupal_add_js('misc/collapse.js', 'core'); ?>

<fieldset class="collapsible collapsed">
    <legend class="collapse">
		<?php if (!empty($title)): ?> 	
            <a href="#"><?php print $title; ?></a>
        <?php endif; ?>
    </legend>   
    <?php foreach ($rows as $id => $row): ?>
        <div class="<?php print $classes[$id]; ?>">
        <?php print $row; ?>
        </div>
    <?php endforeach; ?>
</fieldset>

And you should be ready to go. The displayed View should like this now:

  • Grouping Field 1 (expanded after mouseclick)
    • Title linked to node 1
    • Title linked to node 2
    • Title linked to node 3
  • Grouping Field 2 (collapsed)
  • Grouping Field 3 (collapsed)

Comments

Status: Fixed » Closed (fixed)

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

knalstaaf’s picture

If this looks like too much work for you, the Views Accordion module may catch your interest.

josephcheek’s picture

Unforch, views accordion won't allow a field that is being grouped to be collapsible. If it did, that would solve my problem, but it looks like this template will work for me.

Thanks!

Joseph Cheek

jibran’s picture

Version: 6.x-2.12 » 7.x-3.5

This works in D7

/**
 * Display a single views grouping.
 */
function mytheme_views_view_grouping($vars) {
  $view = $vars['view'];
  $title = $vars['title'];
  $content = $vars['content'];

  $output = theme('ctools_collapsible', array('handle' => $title, 'content' => $content, 'collapsed' => TRUE));

  return $output;
}
rachelf’s picture

This is another way for D7:

Follow the original instructions, but copy and paste the code below into views-view-unformatted.tpl.php

<?php
// $Id: views-view-unformatted.tpl.php,v 1.6 2008/10/01 20:52:11 merlinofchaos Exp $
/**
* @file views-view-unformatted.tpl.php
* Default simple view template to display a list of rows.
*
* @ingroup views_templates
*/
?>

<?php drupal_add_js('misc/form.js'); ?>
<?php drupal_add_js('misc/collapse.js'); ?>
 
<fieldset class="collapsible collapsed">
  <legend><span class="fieldset-legend"><?php if (!empty($title)) : ?><?php print $title; ?><?php endif; ?></span></legend>
<div class="fieldset-wrapper">
<?php if (!empty($title)): ?>
            <a href="#"><?php print $title; ?></a>
        <?php endif; ?>
    </legend>  
    <?php foreach ($rows as $id => $row): ?>
        <div class="<?php print $classes[$id]; ?>">
        <?php print $row; ?>
        </div>
    <?php endforeach; ?>
</div>
</fieldset>
jibran’s picture

Poieo’s picture

The sandbox in #6 seems to be missing. However, there is a new project here: http://drupal.org/project/views_fieldsets

jibran’s picture

Code is there in 7.x-1.x branch.

if you like the module you can review #1925466: [D7] Views fieldset style plugin and help in promoting it to full module.

jibran’s picture

Issue summary: View changes

Updated issue summary.

socialform’s picture

#5 worked great. Thanks rachelf!

PhilY’s picture

#5 is great.
However, is it possible to collapse the collapse-processed fieldset when uncollapsing a new one (like for Accordion) ?

Yuri’s picture

https://www.drupal.org/project/views_nested_accordion may be a solution for the request in #3

lroedal’s picture

How would I go about doing this in Drupal 8?

xamount’s picture

#5 worked, but there is an extra </legend> in the code.