taxonomy_node_get_terms

5 taxonomy.module taxonomy_node_get_terms($nid, $key = 'tid')
6 taxonomy.module taxonomy_node_get_terms($node, $key = 'tid', $reset = FALSE)

Find all terms associated with the given node, ordered by vocabulary and term weight.

3 calls to taxonomy_node_get_terms()

File

modules/taxonomy/taxonomy.module, line 632
Enables the organization of content into categories.

Code

function taxonomy_node_get_terms($node, $key = 'tid', $reset = FALSE) {
  static $terms;

  if ($reset) {
    unset($terms[$node->vid]);
  }

  if (!isset($terms[$node->vid][$key])) {
    $result = db_query(db_rewrite_sql('SELECT t.*,v.weight AS v_weight_unused FROM {term_node} r INNER JOIN {term_data} t ON r.tid = t.tid INNER JOIN {vocabulary} v ON t.vid = v.vid WHERE r.vid = %d ORDER BY v.weight, t.weight, t.name', 't', 'tid'), $node->vid);
    $terms[$node->vid][$key] = array();
    while ($term = db_fetch_object($result)) {
      $terms[$node->vid][$key][$term->$key] = $term;
    }
  }
  return $terms[$node->vid][$key];
}

Comments

View taxonomy terms in node sorted by multiple group

View taxonomy terms in node sorted by multiple group.

For example:

(Freetagging)
Tags:
books
authors
etc.

and

(Defined category)
Category:
Books

..................................................................................................

<?php
if (arg(0) == 'node' && is_numeric(arg(1)) && !arg(2)) {
 
$node = node_load(arg(1));
  foreach (
$node->taxonomy as $vid => $term) {
 
$vn = taxonomy_vocabulary_load($term->vid);
 
$t[$vn->name][$term->tid]['name'] = $term->name;
}
 
$output = '';
  foreach (
$t as $key => $value){
 
$output .= '<fieldset class="fieldgroup collapsible"><legend>'.$key.'</legend>';
  foreach(
$value as $b => $a){
 
$output .= '<div class="field-item odd">';
 
$output .= l($a['name'], 'taxonomy/term/' . $b) . ' ';
 
$output .= '</div>';
}
 
$output .= '</fieldset>';
}
  print
$output;
}
?>

one more check

You also need to check if the node has any terms, otherwise it will print out an error.

Here's the fixed code:

<?php
if (arg(0) == 'node' && is_numeric(arg(1)) && !arg(2)) {
 
$node = node_load(arg(1));
  if(
$node->taxonomy) {
    foreach (
$node->taxonomy as $vid => $term) {
     
$vn = taxonomy_vocabulary_load($term->vid);
     
$t[$vn->name][$term->tid]['name'] = $term->name;
    }
   
$output = '';
    foreach (
$t as $key => $value){
     
$output .= '<fieldset class="fieldgroup collapsible"><legend>'.$key.'</legend>';
      foreach(
$value as $b => $a){
       
$output .= '<div class="field-item odd">';
       
$output .= l($a['name'], 'taxonomy/term/' . $b) . ' ';
       
$output .= '</div>';
      }
     
$output .= '</fieldset>';
    }
    print
$output;
  }
  else {
    return;
  }
}

?>

Cheers!

Is there a reason why $node isn't passed in by reference?

I wish the signature were "taxonomy_node_get_terms(&$node, $key = 'tid')". Seems a bit wasteful to pass that much data when a reference is sufficient.

Negligible optimization

Passing $node by reference would suggest that this function modifies it. Clarity of the function's intent is more important than an optimization which likely wouldn't be noticeable.

same in drupal 7

What drupal 7 function do i need to manage the same thing ?

+1, how do we do this in 7?

+1, how do we do this in 7?

+1 on me too.

+1 on me too.

Good thread with a few solutions for D7

There are a few solutions presented in this thread. I'm still comparing but the plain reimplementation of the function at the end seems reasonable. http://drupal.org/node/909968

I found an easy solution

As taxonomy is linked to entities through fields:

$terms = field_view_field('node', $node, $field_name);

See: http://www.sagetree.net/news/getting-nodes-taxonomy-terms-drupal-7

Login or register to post comments