You are here

function taxonomy_menu_term_count_nodes in Taxonomy menu 8

Same name and namespace in other branches
  1. 7.2 taxonomy_menu.module \taxonomy_menu_term_count_nodes()
  2. 7 taxonomy_menu.module \taxonomy_menu_term_count_nodes()

Calculates the number of nodes linked to a term. It can be either recursive and process all the children or just for this very term.

This is inspired by taxonomy_select_nodes from taxonomy.module.

@TODO Make function recursive.

Parameters

$tid: The term ID.

$recursive: Process all the children or not. Default is FALSE according to Drupal standard.

Return value

int The number of nodes attached to a term and optionally its children.

1 call to taxonomy_menu_term_count_nodes()
taxonomy_menu_menu_link_prepare in ./taxonomy_menu.module
Prepares a taxonomy item to be saved as a menu link.

File

./taxonomy_menu.module, line 534
Generates menu links for all selected taxonomy terms.

Code

function taxonomy_menu_term_count_nodes($tid, $recursive = FALSE) {
  if ($tid == 0 || !\Drupal::config('taxonomy.settings')
    ->get('maintain_index_table') ?: TRUE) {
    return FALSE;
  }
  if ($recursive) {

    //@TODO Make it recursive.
  }
  else {
    $query = db_select('taxonomy_index', 't');
    $query
      ->condition('tid', $tid);
    $query
      ->addField('t', 'nid');
    $query
      ->addField('t', 'tid');
    $count = $query
      ->countQuery()
      ->execute()
      ->fetchField();
  }
  return $count;
}