Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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
Comment #2
knalstaaf CreditAttribution: knalstaaf commentedIf this looks like too much work for you, the Views Accordion module may catch your interest.
Comment #3
josephcheekUnforch, 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
Comment #4
jibranThis works in D7
Comment #5
rachelf CreditAttribution: rachelf commentedThis is another way for D7:
Follow the original instructions, but copy and paste the code below into views-view-unformatted.tpl.php
Comment #6
jibranOr you can use Views fieldset style plugin
Comment #7
Poieo CreditAttribution: Poieo commentedThe sandbox in #6 seems to be missing. However, there is a new project here: http://drupal.org/project/views_fieldsets
Comment #8
jibranCode 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.
Comment #8.0
jibranUpdated issue summary.
Comment #9
socialform CreditAttribution: socialform commented#5 worked great. Thanks rachelf!
Comment #10
PhilY#5 is great.
However, is it possible to collapse the collapse-processed fieldset when uncollapsing a new one (like for Accordion) ?
Comment #11
Yuri CreditAttribution: Yuri commentedhttps://www.drupal.org/project/views_nested_accordion may be a solution for the request in #3
Comment #12
lroedal CreditAttribution: lroedal commentedHow would I go about doing this in Drupal 8?
Comment #13
xamount CreditAttribution: xamount as a volunteer commented#5 worked, but there is an extra
</legend>
in the code.