Collapsible content in Drupal

We recently had a client who requested their current static site to be integrated into the Drupal CMS. No worries, their current site had some cool ways of displaying some of their content which knew Drupal could handle, but didn't know if someone had already done this before, allowing me to use their notes for a quick integration for our client.

One way our client had content displaying was through the use of collapsible content. For example, there is a list of say 4 items, when you click on the 1st item, text relating to that item would then display underneath and the 2nd, 3rd, and 4th list items would move down to make way for the text. Now Drupal already uses this feature (like FAQ's) and in the backend, but not for custom created content added by the user (to my knowledge) so I found myself searching the Internet to find if someone had successfully integrated this feature, and had no luck.

So with a little bit of trial an error, I have successfully used Drupal's existing collapsible feature for my custom content, so it displayed like Drupal's FAQ's. I won't go into massive detail of how to install modules etc, so this is aimed at users who already have an understanding of Drupal and know what they are doing.

This solution is for Drupal 5.11
Modules Used: Content Construction Kit (CCK), Content Templates, Views

Step 1. Enable the above listed modules (if not already)

Step 2. Create a new content type

So this content type will be used as your collapsible content. The node title will be the link, and the body will become the content that is show when the title is clicked. For my solution, I created a content type called Latest News and kept the title label as “title” and body label as “body”.

Step 3. Create a view

A view now needs to be created to display your new content type. Go to site building>views>add. Add your view name, check “provide page view”, give it a url, select “list view” for view type, and give the page a title. Under fields, add Node: Body. For Filters I have Node: Publish equals yes, and Node: Type is one of Latest News. Now add the following code into the Header section of the view and select input format as PHP.



Then save your view.

Step 4. Create a content template for your content type

Go to Content Management>Content Templates and select “create template” next to your new content type. In the body section tick “affect body output” and in body template add the following code



And hit submit to save.

Step 5. Add some content

Now to test, add some content (more than one) to your new content type and test using the view url you set up. If done properly, your view should be displaying a list of node titles and clicking on one should expand/collapse the body of the node. If not, go back and retrace your steps to make sure everything is in order. If it works, yahoo!

Hope this was helpful. To see this in action, please visit http://www.newsawards.com.au/latest-news

For some nicer functionality, try adding some css to show a plus and minus sign when expanded/collapsed like in our example.

Stay tuned for an example of how we integrated the thickbox funtionality for custom content, as we have done on the http://www.newsawards.com.au/08-winners page (Click on a winner)

What do you think?

Please note: The captcha is case sensitive.

What others have said!

by:
sri20198

on:
25 May 2009
@ 9:06 pm

Thanx for the snippet.

Though, in Drupal 6 (I am using D6), the collapse/expand works in the particular node, even when no view is set. How do I go about getting the same effect in a 'view' with all the news listed in a single page?

Regards,
Sridhar

by:
dannz

on:
19 May 2009
@ 12:30 am

Hi - thanks for this, it was exactly what I was looking for! (and superbly written step by step guidance).

I got this to work with Drupal 6.12 (with a little bit of trial and error). The trick seems to be to use 'node' in the 'row style' settings in Views.

I found the node title and 'created by' came up above the collapsible title, which is not what I wanted, so I used 'Hide node title the easy way' as per nevets post here: http://drupal.org/node/225963

I'm no programmer and newbie to Drupal, so this may not be the best way to do it, but it seems to work well.

Thanks again.

Feedback Form