You are here

function nat_get_nids in Node Auto Term [NAT] 7

Same name and namespace in other branches
  1. 5 nat.module \nat_get_nids()
  2. 6.2 nat.module \nat_get_nids()
  3. 6 nat.module \nat_get_nids()
  4. 7.2 nat.module \nat_get_nids()

Gets node IDs/nodes associated with a term.

Parameters

$tids: An array of term IDs whose associated nodes are to be retrived.

$get_nodes: A boolean indicating if node_load operations are to be performed on the associated nodes.

Return value

$return An associative array of (nid => node) or (nid => title) depending on the value of $get_nodes.

1 call to nat_get_nids()
nat_field_formatter_view in ./nat.module
Implements hook_field_formatter_view().

File

./nat.module, line 412
NAT - node auto term - is a helper module that automatically creates a term using the same title as a node.

Code

function nat_get_nids($tids, $get_nodes = FALSE) {
  static $nid_cache = NULL;
  static $node_cache = NULL;
  $return = array();

  // Keep processing to a minimum for empty tid arrays.
  if (!empty($tids)) {

    // Sort tid array to ensure that the cache_string never suffers from order
    // issues.
    sort($tids);
    $cache_string = implode('+', $tids);
    if ($get_nodes) {
      if (isset($node_cache[$cache_string])) {
        return $node_cache[$cache_string];
      }
      elseif (isset($nid_cache[$cache_string])) {

        // If the nid cache stores the same string, node_load() each nid and
        // return them.
        $return = array();
        foreach (array_keys($nid_cache[$cache_string]) as $nid) {
          $return[$nid] = node_load($nid);
        }
        $node_cache[$cache_string] = $return;
        return $return;
      }
    }
    else {
      if (isset($nid_cache[$cache_string])) {
        return $nid_cache[$cache_string];
      }
      elseif (isset($node_cache[$cache_string])) {

        // If the node cache stores the same string, retrieve only the nids and
        // return them.
        foreach ($node_cache[$cache_string] as $nid => $node) {
          $return[$nid] = $node->name;
        }

        // Cache extracted results.
        $nid_cache[$cache_string] = $return;
        return $return;
      }
    }

    // Results have not been cached.
    $result = db_query("SELECT n.nid, t.name\n      FROM {nat} n\n      INNER JOIN {taxonomy_term_data} t USING (tid)\n      WHERE n.tid IN (:tids)", array(
      ':tids' => $tids,
    ));
    foreach ($result as $node) {
      if ($get_nodes) {
        $return[$node->nid] = node_load($node->nid);
      }
      else {
        $return[$node->nid] = $node->name;
      }
    }
    if ($get_nodes) {
      $node_cache[$cache_string] = $return;
    }
    else {
      $nid_cache[$cache_string] = $return;
    }
  }
  return $return;
}