Hi, I am converting NAT module to Drupal 7 and would like to know if there is some easy (good performing) way how to get terms which are associated with node.

Currently I found these two ways:

1) Make db_query and get associated terms for specific node and vocabulary.

2) Use field_info_fields() and find what is the name of field associated with specific vocabulary. Then read value from that field ($node->field_xxxxx) to get term ids.

Those two methods doesn't seem practical as there is already field_xxxxxxx in $node object which contain wanted information, but I cannot find existing function to get name of the field for specific taxonomy vocabulary.

Comments

duckzland’s picture

can this article help? http://drupal.org/node/76392

--------------------------------------------------------------------------------------------------------
if you can use drupal why use others?
VicTheme.com

anantagati’s picture

Can help, but not for Drupal 7.

anantagati’s picture

So, I went with 2) to get fieldname and then get values directly from $node object.

WorldFallz’s picture

sammermpc1’s picture

Has anyone got a solution for this? It seems a common question and there are a number of different Drupal 6.x solutions that seem to work well. The main complication, is that all of the taxonomy functions relating to nodes have been removed, and seem to have been replaced either with the complicated Fields API, or with Views.

The main question is, how can you just do what taxonomy_node_get_terms did in 6 in 7? Must be simple, but I can't figure it out.

@WorldFallz http://api.drupal.org/api/function/taxonomy_term_load_multiple/7 seems like it might work, but can you pass a condition that selects based on NID?

UPDATE -- I realize this is a crazy thing to do, but I slapped together a module which tweaks taxonomy_select_nodes() a bit, and instead of selecting all of the nodes connected with a term, it selects the terms connected with a node. I tried to follow all the debate about the Fields API, but couldn't really figure it out. I'd be grateful if someone sketched out the correct way to do this sort of thing. I have a hunch as more and more people start rolling things into Drupal 7, people will be asking.

Anyhow, here's the tweaked code:

function mymodule_node_select_terms($nid, $pager = TRUE, $limit = FALSE, $order = array('t.sticky' => 'DESC', 't.created' => 'DESC')) {
  if (!variable_get('taxonomy_maintain_index_table', TRUE)) {
    return array();
  }
  $query = db_select('taxonomy_index', 't');
  $query->addTag('node_access');
  $query->condition('nid', $nid); // select nid rather than tid.
  if ($pager) {
    $count_query = clone $query;
    $count_query->addExpression('COUNT(t.nid)');

    $query = $query->extend('PagerDefault');
    if ($limit !== FALSE) {
      $query = $query->limit($limit);
    }
    $query->setCountQuery($count_query);
  }
  else {
    if ($limit !== FALSE) {
      $query->range(0, $limit);
    }
  }
  $query->addField('t', 'tid');
  foreach ($order as $field => $direction) {
    $query->orderBy($field, $direction);
    // ORDER BY fields need to be loaded too, assume they are in the form
    // table_alias.name
    list($table_alias, $name) = explode('.', $field);
    $query->addField($table_alias, $name);
  }
  return $query->execute()->fetchCol();
}
matt v.’s picture

A variation on the following worked for me, (credit goes to page 355 of Pro Drupal 7 Development):

  $nid = 2;
  $node = node_load($nid);
  $result = field_view_field('node', $node, 'field_tags', array('default'));
  print render($result);
tcowin’s picture

I didn't have a field named 'field_tags', so this call didn't return anything for me. I was trying to use a series of taxonomy relationships that did survive a D6 upgrade. I noticed that my node variable did have 'taxonomy_vocabulary_N' fields, so I put this hack together:


function connection_exists($nid, $term){
    if (module_exists('taxonomy') && $nid > 0 && $term) {
        $node = node_load($nid);
        $vocabs_exist = TRUE;
        $vocab_index = 1;
        while($vocabs_exist){
            $field_name = 'taxonomy_vocabulary_' . $vocab_index;
            $term_index = 0;
            $terms_exist = TRUE;
            if(!empty($node->{$field_name})){
                while($terms_exist){
                    if(isset($node->{$field_name}['und'][$term_index]['taxonomy_term'])) {
                        $term_obj = $node->{$field_name}['und'][$term_index++]['taxonomy_term'];
                        if($term_obj){
                            if(strcasecmp($term_obj->name, $term) == 0){
                                return TRUE;
                            }
                        }
                    }
                    else{
                        $terms_exist = FALSE;
                    }
                }
            }
            else{
                $vocabs_exist = FALSE;
            }
            $vocab_index++;
        }
    }
    return FALSE;
}