Drupal tip #5: Displaying terms by facet (vocabulary)

Here's a tip that only an information geek would care about. If you've ever looked at my taxonomies, you'll know that I describe nodes by facet, e.g. file format, person, subject. Years ago, in another version of Drupal, I used to show terms applied to each node by facet, but with the upgrades and changes to the taxonomy module, I gave up trying to keep that up to date. Luckily, I found in the PHP-Template documentation, that someone posted a snippet for sorting/displaying terms by vocabulary. So I'm using this snippet to display my terms by facet again.

Here's a screenshot of terms in action:

Like I said, that's the kind display of metadata that only a info geek would want. But I think it also demonstrates to people new to Drupal the kind of description/categorization of content that might be useful in different environments, e.g. a corporate intranet.

So to do this, use the following code in your node.tpl.php (snarfed from drupal.org):

<?php if ($terms): ?>
<?php /* sort taxonomy links by vocabulary 27 */
$terms27 = taxonomy_node_get_terms_by_vocabulary($node->nid, 27);
if (
$terms27) {
  print
'<div class="terms">Forums: ';
     foreach (
$terms27 as $key => $term27) {
    
$lterm27 = l($term27->name, 'taxonomy/term/'.$term27->tid);
  print
$lterm27.' - ';
     }
  print
'</div>';
}
?>

<?php endif; ?>

You would repeat that code block sandwhiched between "if($terms) ... endif" for each set of facets you want to display.

Comments

01 Boris Mann@bryg...
03/22/06 @ 12:10

I just saw a site yesterday that implemented faceted browsing using separate vocabularies to filter nodes.

See this drupal forum post for some code, and the site is at http://knowledge.ilign.com/.

02 jibbajabba
03/22/06 @ 12:23

I like that. It's a simple filtering implementatiion that's intuitive. Wouldn't work for facets on my site though because facets are actually vocabularies. Would work for filtering child terms, though. Will have to remember this for future client projects. Thanks, Boris!

Advertisement
03 Boris Mann@bryg...
03/22/06 @ 14:45

Err....yes, the facets ARE vocabularies. They have one vocabulary that is Business Alignment, Culture, etc., and then the second that is Implementation, Product Information, etc. and even a third for the target user.

04 jibbajabba
03/22/06 @ 15:15

Oh I see. I should have read the Drupal forum entry more slowly. I couldn't tell if they were done as separate vocabularies. Very nice. I'll have to try that one out then in one of my sites.

05 Dave Cohen
03/22/06 @ 22:58
06 Boris Mann@bryg...
03/24/06 @ 01:16

Yes, that's nice, Dave. And of course, I'd like to get things like free tagging vocabularies separately.

Can you give an example of the snippet you would use in your node.tpl.php to display this?

07 Gustavo
08/14/06 @ 17:16
08 E. Gawater
02/13/07 @ 14:29

I cant get this to work anyone else having problems?

09 jibbajabba
02/13/07 @ 15:16

Works for me. Do you have sample code?

10 mpare
02/14/07 @ 04:51

No I didn't have any trouble. Though I didn't use the code as presented. One way to tell where you are having trouble is just to print the output step by step. So you could use print_r($yourvar) and test to make sure each step is working properly. Use print_r if output is an array or object. print_r is my best friend when doing anything in drupal. Hope that helps if not you can hit me up in the irc channels, #drupal-dojo or #drupal-support, under mpare.

Peace!

Matthew Pare

Advertisement
11 Anonymous
06/05/07 @ 13:11

große Informationen, Dank.

12 online
08/10/07 @ 05:06

Great tip, thanks.

13 youtube
09/12/07 @ 18:06

Is the code working Gustavo?

14 Alexei
12/03/07 @ 13:12

Good tips. Thank you.

15 prank
01/03/08 @ 14:53

nice trick, thanks

16 dainu zodziai
01/04/08 @ 06:15

this nice one. thnx

17 deludedian
01/20/08 @ 00:20

Great tip - keep them coming.