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.

11 thoughts on “A Drupal tip: Adding taxonomy vocabulary description to a Views header”

  1. Hi, Matthieu – I haven’t messed around with Drupal 7 at all yet, so can’t really comment. But if you figure it out, please leave a note on how you make it work!

    Like

  2. Found it :
    as the url is formatted like this :
    /taxonomy/term/1
    arg(1) returns ‘term’
    So you have to use arg(2) to get the actual term id.
    And next, as i got the id instead of the name, I can use the taxonomy_term_load function to load the term.
    So the full code is :

    &lt;?php
    $term = taxonomy_term_load(arg(2));
    print (filter_xss_admin($term-&gt;description));
    ?&gt;
    

    And I found in the doc the reason why taxonomy_get_term_by_name returns the result as an array element. It’s because as several terms can have the same name, it’s possible to have multiple results for this function. It’s not the case with taxonomy_term_load, beacause terms id are unique, of course.
    http://api.drupal.org/api/drupal/modules–taxonomy–taxonomy.module/function/taxonomy_get_term_by_name/7

    Like

  3. I think this method is a little cleaner.

    <?php

    if (arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2))) {
    $tid = (int)arg(2);
    $term = taxonomy_term_load($tid);
    print ('’ . filter_xss_admin($term->description) . ”);
    }
    ?>

    Like

  4. Hey! Thank you very much for your tips.
    It`s very helpfull.

    I’m wondering if there any possibility to print other (additional) cck fields from taxonomy (Drupal 7) instead the description field. It would be very powerfull.

    Thanks in advance for your suggestion!

    Like

  5. Thank you.

    This helped me directly with my immediate problem, but also helped me gain a deeper understanding of php and Drupal.

    For some reason I had to use taxonomy_get_term_by_name(arg(2)) for it to work. I guess this is because my custom url looks like: site.com/skincare/brands/eltamd where “brands” is a vocab and “eltamd” is the term.

    Anyway, it worked great so thankyou.

    Like

  6. Great Article – Thanks.
    I have a view that bring back info based on the Term which is the argument. The url looks like this: ../product-gallery/Chairs
    Now I have included the Term description in my list of fields but am excluding it from display. I want to put the Term Description in my Header for the view. How can I do that?

    Like

  7. hmmm got the following error when using LEternitys code:

    Warning: Unexpected character in input: ”’ (ASCII=39) state=1 in /home/username/public_html/drupal/modules/php/php.module(74) : eval()’d code on line 5

    Like

  8. Jive01, I got LEternity’s code working, but I think the sample above had some quote marks instead of plain ‘ and ” . I think that is where the “unexpected characer” errors came from.

    Like

Leave a comment