Drupal 7 - get all terms for a node
Blog
Written with a loving hand by kitt some time around 19:19 on 9 November 2014
Okay, you can use api.drupal.org/field_view_field, if you know all the various fields a node is using for terms and term references (reference):
$terms = field_view_field('node', $node, 'field_tags');If you don't, load them all in one fell swoop (with lots of caching, please):
/** * Likely in hook_preprocess_node. */ function mymodule_preprocess_node(&$vars) { $multi = array(); // get all the terms for this node, add status if desired $results = db_query('SELECT tid FROM {taxonomy_index} WHERE nid = :nid', array(':nid' => $vars['node']->nid)); // loop over the terms for the tids foreach ($results as $result) { $tids[] = $result->tid; } // load all the terms at once if (count($tids) > 0) { $terms = taxonomy_term_load_multiple($tids); // and do shit with them. foreach ($terms as $tid => $term) { // in this particular case, push them onto an array to track in google analytics. $multi[] = array('_trackEvent', 'terms', $term->vocabulary_machine_name, $term->name); } } drupal_add_js(array('multi' => $multi), 'setting'); }
Add new comment