You are here

function hs_taxonomy_hierarchical_select_entity_count in Hierarchical Select 7.3

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

Implementation of hook_hierarchical_select_entity_count().

File

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

Code

function hs_taxonomy_hierarchical_select_entity_count($item, $params) {
  $num_entities = 0;
  $selected_bundles = $params['entity_count']['settings']['entity_types'];
  $count_children = $params['entity_count']['settings']['count_children'];

  // Maybe this needs some more caching and value-updates on entity_save()/
  // _update()/delete().
  if (empty($num_entities)) {
    $index_table = 'taxonomy_index';
    if (module_exists('taxonomy_entity_index')) {
      $index_table = 'taxonomy_entity_index';
    }

    // Count entities associated to this term.
    $query = db_select($index_table, 'ti');
    $query
      ->fields('ti');
    $query
      ->condition('ti.tid', $item);
    if (module_exists('taxonomy_entity_index')) {
      _hs_taxonomy_add_entity_bundles_condition_to_query($query, $selected_bundles);
    }
    $result = $query
      ->execute();
    $num_entities = $result
      ->rowCount();
    if ($count_children) {
      $tids = array();
      $tree = taxonomy_get_tree($params['vid'], $item);
      foreach ($tree as $child_term) {
        $tids[] = $child_term->tid;
      }
      if (count($tids)) {

        // Count entities associated to child terms.
        $query = db_select($index_table, 'ti');
        $query
          ->fields('ti');
        $query
          ->condition('ti.tid', $tids, 'IN');
        if (module_exists('taxonomy_entity_index')) {
          _hs_taxonomy_add_entity_bundles_condition_to_query($query, $selected_bundles);
        }
        $result = $query
          ->execute();
        $num_entities += $result
          ->rowCount();
      }
    }
  }
  return $num_entities;
}