You are here

function tac_lite_node_get_terms in Taxonomy Access Control Lite 7

Same name and namespace in other branches
  1. 8 tac_lite.module \tac_lite_node_get_terms()

In Drupal 6.x, there was taxonomy_node_get_terms(). Drupal 7.x should provide the same feature, but doesn't. Here is our workaround, based on https://drupal.org/comment/5573176#comment-5573176.

We organize our data structure by vid and tid.

1 call to tac_lite_node_get_terms()
_tac_lite_get_terms in ./tac_lite.module
Gets terms from a node that belong to vocabularies selected for use by tac_lite

File

./tac_lite.module, line 567
Control access to site content based on taxonomy, roles and users.

Code

function tac_lite_node_get_terms($node) {
  $terms =& drupal_static(__FUNCTION__);
  if (!isset($terms[$node->nid])) {

    // Get tids from all taxonomy_term_reference fields.
    $fields = field_info_fields();
    foreach ($fields as $field_name => $field) {

      // Our goal is to get all terms, regardless of language, associated with the node.  Does the code below do that?
      if ($field['type'] == 'taxonomy_term_reference' && field_info_instance('node', $field_name, $node->type)) {
        if (($items = field_get_items('node', $node, $field_name)) && is_array($items)) {
          foreach ($items as $item) {

            // Sometimes $item contains only tid, sometimes entire term.  Thanks Drupal for remaining mysterious!
            // We need to term to determine the vocabulary id.
            if (!empty($item['taxonomy_term'])) {
              $term = $item['taxonomy_term'];
            }
            else {
              $term = taxonomy_term_load($item['tid']);
            }
            if ($term) {
              $terms[$node->nid][$term->vid][$term->tid] = $term;
            }
          }
        }
      }
    }
  }
  return isset($terms[$node->nid]) ? $terms[$node->nid] : FALSE;
}