You are here

function shs_node_count_term_get_node_count in Simple hierarchical select 7

Helper function to count number of nodes associated to a term.

Parameters

object $term: The term object.

bool $count_children: If set to TRUE, nodes in child terms are counted also.

Return value

int Number of nodes within the term.

1 call to shs_node_count_term_get_node_count()
shs_node_count_shs_term_get_children_alter in modules/shs_node_count/shs_node_count.module
Implements hook_shs_term_get_children_alter().

File

modules/shs_node_count/shs_node_count.module, line 74
Node count functionality for Simple hierarchical select.

Code

function shs_node_count_term_get_node_count($term, $count_children = FALSE) {
  $num_nodes =& drupal_static(__FUNCTION__, array());

  // Maybe this needs some more caching and value-updates on node_save()/
  // _update()/delete().
  if (empty($num_nodes["{$term->tid}:{$count_children}"])) {
    $index_table = 'taxonomy_index';
    if (module_exists('taxonomy_entity_index')) {
      $index_table = 'taxonomy_entity_index';
    }

    // Count nodes associated to this term.
    $num_nodes["{$term->tid}:{$count_children}"] = db_select($index_table, 'ti')
      ->fields('ti')
      ->condition('tid', $term->tid)
      ->execute()
      ->rowCount();
    if ($count_children) {
      $tids = array();
      $tree = taxonomy_get_tree($term->vid, $term->tid);
      foreach ($tree as $child_term) {
        $tids[] = $child_term->tid;
      }
      if (count($tids)) {
        $num_nodes["{$term->tid}:{$count_children}"] += db_select($index_table, 'ti')
          ->fields('ti')
          ->condition('tid', $tids, 'IN')
          ->execute()
          ->rowCount();
      }
    }
  }
  return isset($num_nodes["{$term->tid}:{$count_children}"]) ? $num_nodes["{$term->tid}:{$count_children}"] : 0;
}