You are here

function taxonomy_edge_select_nodes in Taxonomy Edge 7.2

Same name and namespace in other branches
  1. 8 taxonomy_edge.module \taxonomy_edge_select_nodes()
  2. 6 taxonomy_edge.module \taxonomy_edge_select_nodes()
  3. 7 taxonomy_edge.module \taxonomy_edge_select_nodes()

Reimplementation of taxonomy_select_nodes() re-allowing depth modifier.

2 calls to taxonomy_edge_select_nodes()
taxonomy_edge_term_feed in ./taxonomy_edge.pages.inc
Generate the content feed for a taxonomy term.
taxonomy_edge_term_page in ./taxonomy_edge.pages.inc
Menu callback; displays all nodes associated with a term.

File

./taxonomy_edge.module, line 892
Selecting all children of a given taxonomy term can be a pain. This module makes it easier to do this, by maintaining a complete list of edges for each term using the adjecency matrix graph theory.

Code

function taxonomy_edge_select_nodes($tid, $pager = TRUE, $limit = FALSE, $depth = 0, $order = array(
  't.sticky' => 'DESC',
  't.created' => 'DESC',
)) {
  if (!variable_get('taxonomy_maintain_index_table', TRUE)) {
    return array();
  }

  // Locate tids to search in
  $subquery = db_select('taxonomy_term_edge', 'e');
  $subquery
    ->join('taxonomy_term_edge_path', 'p', 'p.pid = e.pid');
  $subquery
    ->join('taxonomy_term_edge_path', 'p2', 'p2.pid = e.parent');
  $subquery
    ->condition('p2.tid', $tid);
  $subquery
    ->condition('e.distance', $depth, '<=');
  $subquery
    ->fields('p', array(
    'tid',
  ));

  // Find nodes
  $query = db_select('taxonomy_index', 't');
  $query
    ->addTag('node_access');
  $query
    ->condition('t.tid', $subquery, 'IN');
  if ($pager) {
    $count_query = db_select('taxonomy_index', 't');
    $count_query
      ->join('taxonomy_term_edge_path', 'p', 'p.tid = t.tid');
    $count_query
      ->join('taxonomy_term_edge', 'e', 'e.pid = p.pid');
    $count_query
      ->join('taxonomy_term_edge_path', 'p2', 'p2.pid = e.parent');
    $count_query
      ->condition('p2.tid', $tid);
    $count_query
      ->condition('e.distance', $depth, '<=');
    $count_query
      ->addExpression('COUNT(1)');
    $query = $query
      ->extend('PagerDefault');
    if ($limit !== FALSE) {
      $query = $query
        ->limit($limit);
    }
    $query
      ->setCountQuery($count_query);
  }
  else {
    if ($limit !== FALSE) {
      $query
        ->range(0, $limit);
    }
  }
  $query
    ->addField('t', 'nid');
  $query
    ->addField('t', 'tid');
  foreach ($order as $field => $direction) {
    $query
      ->orderBy($field, $direction);

    // ORDER BY fields need to be loaded too, assume they are in the form
    // table_alias.name
    list($table_alias, $name) = explode('.', $field);
    $query
      ->addField($table_alias, $name);
  }
  return $query
    ->execute()
    ->fetchCol();
}