You are here

function hs_taxonomy_term_count_nodes in Hierarchical Select 7.3

Same name and namespace in other branches
  1. 5.3 modules/hs_taxonomy.module \hs_taxonomy_term_count_nodes()
  2. 6.3 modules/hs_taxonomy.module \hs_taxonomy_term_count_nodes()

Drupal core's taxonomy_term_count_nodes() is buggy. See http://drupal.org/node/144969#comment-843000.

File

modules/hs_taxonomy.module, line 1222
Implementation of the Hierarchical Select API for the Taxonomy module.

Code

function hs_taxonomy_term_count_nodes($tid, $type = 0) {
  static $count;
  $tids = array(
    $tid,
  );
  if ($term = taxonomy_term_load($tid)) {
    $tree = _hs_taxonomy_hierarchical_select_get_tree($term->vid, $tid);
    foreach ($tree as $descendant) {
      $tids[] = $descendant->tid;
    }
  }
  if (!isset($count[$type][$tid])) {
    $query = db_select('taxonomy_term_node', 't');
    $query
      ->join('node', 'n', 't.nid = n.nid');
    $query
      ->addExpression('COUNT(DISTINCT(n.nid))', 'count')
      ->condition('n.status', 1)
      ->condition('t,tid', $tids);
    if (!is_numeric($type)) {
      $query
        ->condition('n.type', $type);
    }
    $query
      ->addTag('hs_taxonomy_term_count_nodes');
    $query
      ->addTag('term_access');
    $result = $query
      ->execute();
    $count[$type][$tid] = $result
      ->fetchField();
  }
  return $count[$type][$tid];
}