Update 27 june 2007: RobertDouglass has released a module to do what is described in this article: CCK Taxonomy Fields.
In a case where you have a lot of taxonomy vocabularies and terms, the $terms list can get cluttered, chaotic and might even turn your beautiful your lay-out into an ugly dragon. One option is to make the taxonomy part of your content. This works extremely well with both the general page and story content types, as with custom-created types with both the content and the CCK modules. The result will look like this:

All the magic will happen in hook_nodeapi. If you are unfamiliar with the hook or would like to learn more about it, there is an excellent article on NodeAPI on Drupal Dojo.
First you start with a general hook_nodeapi with a view $op since we will only be altering how the taxonomy gets displayed when viewing a node.
<?php
/**
* Implementation of hook_nodeapi
*/
function taxonomycontent_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
switch ($op) {
case 'view':
// Your code here
break;
}
}
?>If you only want to include the taxonomy for certain content types, or you want different displaying options (or weights) for different types:
<?php
/**
* Implementation of hook_nodeapi
*/
function taxonomycontent_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
switch ($op) {
case 'view':
switch ($node->type) {
case 'story':
case 'page':
case 'yourcontent':
// Your code here
break;
}
break;
}
}
?>To get all the terms in the content, we will follow two steps: one will be to grab all information about the taxonomy: vocabulary and terms, and put them in $content, and step two will be theming the final result.
We will run through the taxonomy, group them by vocabulary in an array, and put them all together in $node->content[taxonomy]. To avoid throwing E_NOTICE messages, we put in an extra check.
<?php
foreach($node->taxonomy as $term) {
$vocab = taxonomy_get_vocabulary($term->vid);
if(isset($node->content[taxonomy][$vocab->name]['#value'])) {
$node->content[taxonomy][$vocab->name]['#value'] .= ", ".l($term->name,"taxonomy/term/".$term->tid);
}
else {
$node->content[taxonomy][$vocab->name]['#value'] = l($term->name,"taxonomy/term/".$term->tid);
}
}
?>All our data now sits nicely in one multi-level array $node->content[taxonomy]. We will now add a prefix and suffix to each vocabulary, and add a weight so it shows up before most of the content but after the title and summary. You can define the weight so it shows up below the title (which is -5 by default) or at the very end of the content (by putting it at +50 for instance). In the following snippet $index contains the name of the vocabulary.
<?php
foreach($node->content[taxonomy] as $index => $term) {
$node->content[taxonomy][$index]['#prefix']='<div class="field field-type-text field-field-'.$index.'">
<div class="field-label">'.ucfirst($index).':</div>
<div class="field-items"><div class="field-item">';
$node->content[taxonomy][$index]['#suffix']='</div></div></div>';
$node->content[taxonomy]['#weight']='-3';
}
?>Our final code looks like this. Do not forget to remove the $terms from your node[-type].tpl.php if you don't want them to show up twice.
<?php
/**
* Implementation of hook_nodeapi
*/
function taxonomycontent_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
switch ($op) {
case 'view':
switch ($node->type) {
case 'story':
case 'page':
case 'yourcontent':
foreach($node->taxonomy as $term) {
$vocab = taxonomy_get_vocabulary($term->vid);
if(isset($node->content[taxonomy][$vocab->name]['#value'])) {
$node->content[taxonomy][$vocab->name]['#value'] .= ", ".l($term->name,"taxonomy/term/".$term->tid);
}
else {
$node->content[taxonomy][$vocab->name]['#value'] = l($term->name,"taxonomy/term/".$term->tid);
}
}
foreach($node->content[taxonomy] as $index => $term) {
$node->content[taxonomy][$index]['#prefix']='<div class="field field-type-text field-field-'.$index.'"><div class="field-label">'.ucfirst($index).':</div>
<div class="field-items"><div class="field-item">';
$node->content[taxonomy][$index]['#suffix']='</div></div></div>';
$node->content[taxonomy]['#weight']='-3';
}
break;
}
break;
}
}
?>
Comments
A bit confused
What .tpl should this be declared in. I'm very new to drupal but I would like to output taxonomy terms for a custom content type. Essentially I want to output the related vocabulary terms as links to related content sorted through a "view".
cck taxonomy fields
This doesn't belong in a .tpl but in a custom module. But as you can see at the top of the article, Robert Douglass created a module that does exactly this: cck taxonomy fields.
Post new comment