Search code examples
drupaldrupal-7drupal-theming

How to print the node taxonomy in a block?


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?


Solution

  • I've come across the solution I was looking for:

    mytheme_preprocess_block() in template.php*

    $node_content = node_view(node_load(arg(1)));
    $vars['node_content'] = $node_content;
    

    .

    block.tpl.php

    print render($node_content['field_tags']);