4.6 taxonomy.module taxonomy_node_get_terms($nid, $key = 'tid')
4.7 taxonomy.module taxonomy_node_get_terms($nid, $key = 'tid')
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()
taxonomy_form_alter in modules/taxonomy/taxonomy.module
Implementation of hook_form_alter(). Generate a form for selecting terms to associate with a node. We check for taxonomy_override_selector before loading the full vocabulary, so contrib modules can intercept before hook_form_alter and provide scalable…
taxonomy_nodeapi in modules/taxonomy/taxonomy.module
Implementation of hook_nodeapi().
taxonomy_node_save in modules/taxonomy/taxonomy.module
Save term associations for a given node.

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.

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;
}
?>

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!

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.

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.

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

+1, how do we do this in 7?

+1 on me too.

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

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

I have been playing with a query that looks like this. This function assumes you know the node id and the vocabs machine name(s).

function _mymodule_node_terms ($nid, $vocabs) {
  $output = array();
  foreach($vocabs as $voc) {
    if (db_table_exists('field_data_field_'. $voc)) {
      $query = db_select('field_data_field_'. $voc, $voc);
      $query->join('taxonomy_term_data', 'term', 'term.tid = '. $voc .'.field_'. $voc .'_tid');
      $query
      ->fields('term')
      ->condition($voc .'.entity_id', $nid, '=')
      ->orderBy('term.name', 'DESC');
      $results = $query->execute();
      foreach ($results as $t) {
        $output[$voc][] = $t;
      }
    }
  }
  return $output;
}

My solution looks as follows:

<?php
/**
 * Fetchs all Taxonomy Term IDs from Entity Object. All fields of field type "taxonomy_term_reference" will be included.
 *
 * @param String $entity_type
 * @param $string $bundle
 * @param Object $entity
 *
 * @return array
 *   Array with tids of entity
 */
function _get_entity_terms($entity_type, $bundle, $entity) {
 
$tids = array();
  foreach (
field_info_field_map() as $field_name => $field_info) {
    if (
$field_info['type'] <> "taxonomy_term_reference") {
      continue;
    }
    if (
array_key_exists($entity_type, $field_info['bundles'])) {
      if (
in_array($bundle, $field_info['bundles'][$entity_type])) {
        if (isset(
$entity->{$field_name})) {
         
$values = field_get_items($entity_type, $entity, $field_name);
          foreach ((array)
$values as $tid) {
           
$tids[] = $tid['tid'];
          }
        }
      }
    }
  }
  return
array_unique($tids);
}
?>

Thanks HydroZ, just used this today.

<?php
/**
 * Fetch all taxonomy terms from an entity.
 * All fields of field type "taxonomy_term_reference" will be included.
 *
 * @param String $entity_type
 * @param $string $bundle
 * @param Object $entity
 *
 * @return array
 *   Array with tids of entity.
 */
function _myModule_get_entity_terms($entity_type, $bundle, $entity) {
 
$terms = array();
 
// Use very lightweight field info list to find relevant fields.
 
foreach (field_info_field_map() as $field_name => $field_info) {
    if (
$field_info['type'] <> "taxonomy_term_reference") {
      continue;
    }
    if (
array_key_exists($entity_type, $field_info['bundles'])) {
      if (
in_array($bundle, $field_info['bundles'][$entity_type])) {
        if (isset(
$entity->{$field_name})) {
         
// Collect terms from fields for return.
         
$values = field_get_items($entity_type, $entity, $field_name);
          foreach ((array)
$values as $tid) {
            if (
$tid) {
             
$term = taxonomy_term_load($tid['tid']);
             
$terms[$term->vocabulary_machine_name][$term->tid] = $term->name;
            }
          }
        }
      }
    }
  }

  return
$terms;
}
?>

Yet another improvement for this solid gold utility function.

<?php
/**
 * Fetch all taxonomy term IDs from an entity object. All fields of field
 *   type 'taxonomy_term_reference' will be included.
 *
 * @param $entity_type
 *   The entity type; e.g. 'node' or 'user'.
 * @param $bundle
 *   A string indicating the bundle type of the entity.
 * @param $entity
 *   The entity to retrieve taxonomy terms from.
 * @param $keyed_by_vocabulary
 *   A boolean indicating how the return values should be organized.
 *
 * @return array
 *   If $keyed_by_vocabulary is TRUE, the term IDs and names will be grouped
 *   into sub-arrays keyed by their vocabulary machine name. If FALSE, return
 *   a basic array of term IDs.
 */
function utility_get_entity_terms($entity_type, $bundle, $entity, $keyed_by_vocabulary = TRUE) {
 
$terms = array();
 
// Use very lightweight field info list to find relevant fields.
 
foreach (field_info_field_map() as $field_name => $field_info) {
    if (
$field_info['type'] <> "taxonomy_term_reference") {
      continue;
    }
    if (
array_key_exists($entity_type, $field_info['bundles'])) {
      if (
in_array($bundle, $field_info['bundles'][$entity_type])) {
        if (isset(
$entity->{$field_name})) {
         
// Collect terms from fields for return.
         
$values = field_get_items($entity_type, $entity, $field_name);
          foreach ((array)
$values as $tid) {
            if (
$tid) {
              if (
$keyed_by_vocabulary) {
               
$term = taxonomy_term_load($tid['tid']);
               
$terms[$term->vocabulary_machine_name][$term->tid]
                  =
$term->name;
              }
              else {
               
$terms[] = $tid['tid'];
              }
            }
          }
        }
      }
    }
  }
  if (
$keyed_by_vocabulary) {
    return
$terms;
  }
  else {
    return
array_unique($terms);
  }
}
?>