Skip to content

Separating Taxonomy Terms, with Vocabulary Names (in Drupal)

September 23, 2010

In the spirit of Angie Byron’s recent blog post on Documenting your way to understanding, I’d like to share something I’ve been working on recently. Hopefully it will prove useful for people who come after me.

The task at hand is to change the way that Drupal displays the taxonomy terms which have been applied to a given node. The site I’m working on – a digital image library – has 21 different vocabularies which are applied across 12 different content types. The two main content types each take terms from around a dozen vocabularies. As you can imagine, this makes the display of those terms less than comprehensible:

Terms like “interior views”, “American”, and “brick” have nothing to do with one another – so the challenge was to separate this display a bit, similar to how Drupal separates field->value pairings like at the top of the image above.

A quick bit of Googling turned up Tim Kamanin‘s recent blog post on How to Separate Drupal Taxonomy Terms Output By Vocabulary – which helped a great deal, both for my understanding of theme function overrides, and how taxonomies are dealt with in the theming layer.

The problem which I ran into was that in Tim’s tutorial, he puts code in the node template like this:

<div>Category: <?php print $terms[1]; ?></div>
<div>Tags: <?php print $terms[2]; ?> </div>

Seeing this, I didn’t want to have to create node templates for each content type, hard code each with lines of code for every vocabulary that was applied, and then keep track of any necessary changes as we introduce more vocabularies down the road.

Ideally, I would have taken Tim’s yourthemename_separate_terms() function and extended it to pull vocabulary names, and output a pretty nested array that has been rendered by the theming functions. Unfortunately, I’m nowhere near that level of Drupal ninja – so what I did was this:

  1. Followed Tim’s tutorial exactly
  2. Inside my node.tpl.php file, make the following code change:

From:

<div class="terms">
<?php print $terms; ?>
</div>

To:

<div class="terms">
<?php
//this is a hack - but it starts to work
//first, we use taxonomy_get_vocabularies to find out which vocabs this node type -might- take
$vocabularies = taxonomy_get_vocabularies($node->type);
foreach ($vocabularies AS $blah) {
//then, while we loop through this list, we check whether any terms from the current vocabulary -have- been applied
//the $terms collection has been produced via zen_ninesixty_separate_terms, in template.php
if($terms[$blah->vid]):
//if they have, then we output those terms.
//ideally, this whole thing would be in a function, and we'd theme in the function using Drupal's built-in functions. But, sadly, I don't know how to do this yet.
print '<div class="grid-2 alpha"><strong>'.$blah->name.':</strong></div>';
print '<div class="grid-10 omega">'.$terms[$blah->vid].'</div>';
endif;
}
?>
</div>

Essentially, I make a call to taxonomy_get_vocabularies() for the given node type, to find out which vocabularies might be applied. Then, looping through that list, I check each one via Tim’s function to see if any terms have actually been applied. For those which have been used, I print out the vocabulary name and the list of terms. The result, after some CSS changes, looks like this:

Much better, at least for what I need to do here.

Up Next

From here, the next challenge will be to add a display of the term hierarchies, so that something like “19th Century” will be displayed in the context of a tree. My hope here is that the hierarchical_select module can show up on the public side of the node, rather than just the edit screen.

3 Comments leave one →
  1. November 28, 2010 1:40 am

    Hi,
    I seem to be having problems with displaying the terms, the vocabulary names work just fine though. I want to display them as links, but have them separated, rather than tabs.

    So the line
    print ”.$terms[$blah->vid].”;
    breaks my node.tpl.php (I tried removing the div class as I don’t need it) and I still get errors. Any suggestions?

    Thanks!

  2. March 29, 2011 2:22 am

    Hi:

    Did you ever make any progress on the next step – getting terms to display in hierarchy? ie: “add a display of the term hierarchies, so that something like “19th Century” will be displayed in the context of a tree.”

    This is what I need to accomplish, and trying to explore ideas / solutions about how to do so.

Leave a comment