According to this book page the $terms variable should still be available in Drupal 7 for when someone like me wants to do something like spit out a list of taxonomy terms associated with a particular node. However like the user that commented on that page, I get a php notice:

Notice: Undefined variable: terms in include()

followed by the line number of the node--type.tpl.php file the code is in.

So...seeing as the pattern seems to be

echo $content['field_something'];

I tried that with the term reference field (unimaginatively labelled field_categories) and got:

Array

The next logical step (to me anyway) was echo print_r($content['field_categories']); which spat out a seething nest of arrays. I tried working my way through the arrays to get it to echo what I wanted but I'm not convinced I'm doing it right.

So do I have to do one of those slightly long winded load $node and write a script for it in a template.php or do a micromodule or is there something wonderfully simple like a checkbox that I've managed to miss in a week or so of overthinking it? :)

Thanks!

Comments

JeremyFrench’s picture

Content in D7 is a 'renderable array' rather than a string, so you need to pass it to the render function.

print render($content['field_something']); 

if you are printing afte the main content be sure to hide($content['field_something']) first.

ryivhnn’s picture

Of course! Well I feel sheepish *turns into a sheep* XD

I'm not sure how I managed to completely miss that for ages, considering I've been echo rendering $content['body']s in various node--type.tpl.phps :|

So got categories showing up, now to work out what is wrong with my free tagging vocabulary, it's not showing up with echo render $content['field_tags'], it's slightly demented in Views and it spits an appreciable PDOException when I try to edit a node that has tags (though creating a node with tags isn't a drama).

Thank you, very much obliged!

works at bekandloz | plays at technonaturalist

Scott J’s picture

But this assumes that we know the name of the field in advance, which is fine if writing a theme for yourself, but impossible if writing a general purpose theme for anyone to use.

Is there really no D7 version of $terms ?

ryivhnn’s picture

Three months later, I'm wondering if there is a way to send anything that identifies as a term reference through a preprocess in template.php to spit out as a $terms variable. Seeing if it's actually do-able and doing it are a bit beyond my current capabilities though :)

Edit: I just checked Bartik's template.php, they have:

 function bartik_field__taxonomy_term_reference($variables) {
  $output = '';

  // Render the label, if it's not hidden.
  if (!$variables['label_hidden']) {
    $output .= '<h3 class="field-label">' . $variables['label'] . ': </h3>';
  }

  // Render the items.
  $output .= ($variables['element']['#label_display'] == 'inline') ? '<ul class="links inline">' : '<ul class="links">';
  foreach ($variables['items'] as $delta => $item) {
    $output .= '<li class="taxonomy-term-reference-' . $delta . '"' . $variables['item_attributes'][$delta] . '>' . drupal_render($item) . '</li>';
  }
  $output .= '</ul>';

  // Render the top-level DIV.
  $output = '<div class="' . $variables['classes'] . (!in_array('clearfix', $variables['classes_array']) ? ' clearfix' : '') . '">' . $output . '</div>';

  return $output;
}

So do-able, just long winded :)

works at bekandloz | plays at technonaturalist

Jeff Burnz’s picture

That is not what this code is doing - all its doing is rewriting the standard field markup to use list code (ul, li) instead of div's (for taxonomy reference fields), Bartik still prints the actual field via $content, take my word for it, I wrote it ;)

ryivhnn’s picture

Yeesh, I keep forgetting about my old topics. Thank you for clarifying. Still learning :)

works at bekandloz | plays at technonaturalist

ryivhnn’s picture

Yeesh, I keep forgetting about my old topics. Thank you for clarifying. Still learning :)

works at bekandloz | plays at technonaturalist

shashank5563’s picture

Thank you, very much your code is working for me.

I have change in given line it work perfectly for me.

foreach ($variables['items'] as $delta => $item) {
         $output .= '<li class="taxonomy-term-reference-' . $delta . '"' . $variables['item_attributes'][$delta] . '>' . $item['#title'] . '</li>';
    
  }

Thank you again

Scott J’s picture

No answer yet, but someone else trying to get terms for a node:
taxonomy_node_get_terms() in Drupal 7

Jeff Burnz’s picture

Scott J’s picture

Well, the first comment suggests

print render($content['field_something']);

but that will print the content of one field only, not all term reference fields. I see that Zen theme uses

      <?php if (!empty($content['links']['terms'])): ?>
        <div class="terms terms-inline"><?php print render($content['links']['terms']); ?></div>
      <?php endif; ?>

so I will try that. Another way to add markup to your terms, is to use custom templates in your theme folder for field--taxonomy_term_reference.tpl.php and/or taxonomy-term.tpl.php files.

GustavoGalvan’s picture

Maybe this can help. With this code you can get all terms name from taxonomy field:

<?php
$myterms = array();
foreach($YOUR_FIELD_NAME as $tmp_field_term) {
  $myterms[] = $tmp_field_term['taxonomy_term']->name;
}
print implode(', ', $myterms); // or whatever you want to do..
?>