You are here

function taxonomy_edge_select_nodes in Taxonomy Edge 8

Same name and namespace in other branches
  1. 6 taxonomy_edge.module \taxonomy_edge_select_nodes()
  2. 7.2 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 819
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 = NULL) {
  if (!variable_get('taxonomy_maintain_index_table', TRUE)) {
    return array();
  }

  // If no depth, just use core (faster query due to no joins)
  if ($depth == 0) {
    $args = $order ? array(
      $tid,
      $pager,
      $limit,
      $order,
    ) : array(
      $tid,
      $pager,
      $limit,
    );
    return call_user_func_array('taxonomy_select_nodes', $args);
  }

  // Set default order if not specified
  if (!isset($order)) {
    $order = array(
      'n.sticky' => 'DESC',
      'n.created' => 'DESC',
      'n.nid' => 'DESC',
    );
  }

  // Lookup vid for better query on edge table
  $vid = db_query("SELECT vid FROM {taxonomy_term_data} WHERE tid = :tid", array(
    ':tid' => $tid,
  ))
    ->fetchField();

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

  // Find nodes
  $query = db_select('taxonomy_index', 't');
  $query
    ->join('node', 'n', 'n.nid = t.nid');
  $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', 'e', 'e.tid = t.tid');
    $count_query
      ->condition('e.parent', $tid);
    $count_query
      ->condition('e.vid', $vid);
    $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('n', '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();
}