How to print the node taxonomy in a block?

Michał Pękała picture Michał Pękała · Jan 11, 2011 · Viewed 11.8k times · Source

I'd like to print taxonomy terms (from field field_tags) in a block on a node view page (in a Zen subtheme).

So what I did was.

template.php

function michal_preprocess_block(&$vars, $hook) {
 if ( arg(0) == 'node' && is_numeric(arg(1)) ) {
   $node = node_load(arg(1));
   $vars['node'] = $node;
   $vars['node_field_tags'] = $node->field_tags;
   $vars['node_content_field_tags'] = $node->content['field_tags'];
 }
}

However, when I try to print it in block.tpl.php, neither of these 2 variables outputs taxonomy terms from the field.

print render($node_content_field_tags);
print render($node_field_tags);

Do You know a Drupal function to render a taxonomy terms field?


EDIT 13.01.2011, 00:21

As far as I understood (from this, this and that) the process the code should look more/less like this

 $node = node_load(arg(1));
 $node_view($node) // Generates an array for rendering a node, see http://api.drupal.org/api/drupal/modules--node--node.module/function/node_view/7
 $vars['node'] = $node;

and then in the block.tpl.php:

render($node->content['field_tags']);

The $node->content is null, however.

Do You know what I'm missing?

Answer

Dave Reid picture Dave Reid · Jan 17, 2011

Actually what may be easier is the following code in your preprocess:

if ($node = menu_get_object()) {
  $vars['node_field_tags'] = field_view_field('node', $node, 'field_tags', 'full');
}

And then use the following in your template:

print render($node_field_tags);