How to print the node taxonomy in a block?

11,826

Solution 1

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']);

Solution 2

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);

Solution 3

First of all, you should check that the preprocess function is being run and that cache is not playing with you. Then you could try to inspect the variables. I don't think you can use render() on the $node->field_tags and I'm not too sure about $node->content['field_tags'] either.

Inspecting the variables will help you figure it out, devel works fine for Drupal 7 and can help you there.

Solution 4

You might also check out the CCK Blocks module. It creates a sidebar block that displays alongside each node (if it has content), and adds that block to the list of rendering destinations for each field, just like 'teaser' and 'full' and 'rss'.

It may not have all the control you're looking for but it could be a good place to start.

Share:
11,826
Michał Pękała
Author by

Michał Pękała

"I refuse to join any club that would have me as a member" - Groucho Marx

Updated on June 04, 2022

Comments

  • Michał Pękała
    Michał Pękała almost 2 years

    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?