Archive for May 28th, 2010

A Drupal tip: Adding taxonomy vocabulary description to a Views header

This is a how-to tip for Drupal 6, which I’m documenting because I couldn’t find this answer anywhere and it took me a day of scratching my head to figure it out. Drupalistas might find this useful, the rest of you can move along, there’s nothing for you to see here.

I was creating a View that listed all the nodes that have a given vocabulary term assigned to them, where the vocabulary term is passed in as the argument (e.g. http://mysite.myschool.edu/sitename/type/Basic ), where “type” is the path to the View, and “Basic” is the vocabulary term).

I wanted to include the description of the vocabulary term appear at the top of the View. How to do that?

The short answer is to put a short snippet of PHP code in the header of the View. Step by step:

  1. Make sure that the PHP filter module is enabled in the Core – optional section of Modules.
  2. Edit the Header item of your VIew (in the Basic Settings). If you’re using a WYSIWYG editor, make sure your input format is set to PHP Code.
  3. Paste this code into the Header box:
    <?php
    $term = taxonomy_get_term_by_name(arg(1));
    print (filter_xss_admin($term[0]->description));
    ?>
  4. Update the View, then Save it. You won’t necessarily see the result in the Live Preview under the Views menus, but it should work in your site.

The slightly longer story here is that I think there’s a bug in the taxonomy_get_term_by_name() function that makes it so you have to reference $term[0]->description instead of $term->description. I filed that bug on the Drupal.org site at http://drupal.org/node/812164.

Hope that helps other folks besides me – leave a comment if it works or doesn’t work for you.


subscribe

Pages

Latest tweets

interesting links

What I’m listening to

 

May 2010
M T W T F S S
« Mar   Jul »
 12
3456789
10111213141516
17181920212223
24252627282930
31  

Follow

Get every new post delivered to your Inbox.