You are here

function taxonomy_list_select_nodes in Taxonomy List 7

Same name and namespace in other branches
  1. 5.2 taxonomy_list.module \taxonomy_list_select_nodes()
  2. 5 taxonomy_list.module \taxonomy_list_select_nodes()
  3. 6.2 taxonomy_list.module \taxonomy_list_select_nodes()
  4. 6 taxonomy_list.module \taxonomy_list_select_nodes()

Finds all nodes that match selected taxonomy conditions. Copied from taxonomy.module.

Parameters

$tids: An array of term IDs to match.

$operator: How to interpret multiple IDs in the array. Can be "or" or "and".

$depth: How many levels deep to traverse the taxonomy tree. Can be a nonnegative integer or "all".

$pager: Whether the nodes are to be used with a pager (the case on most Drupal pages) or not (in an XML feed, for example).

Return value

A resource identifier pointing to the query results.

3 calls to taxonomy_list_select_nodes()
taxonomy_list_nodes_render in ./taxonomy_list.module
Select and render the nodes in the chosen vocabularies.
theme_taxonomy_list_directory in ./taxonomy_list.module
Theme a directory list.
_taxonomy_list_directory_children in ./taxonomy_list.module
Theme child terms for the directory list.

File

./taxonomy_list.module, line 735
List all terms in a vocabulary.

Code

function taxonomy_list_select_nodes($tids = array(), $operator = 'or', $depth = 0, $pager = TRUE) {
  $result = FALSE;
  if (count($tids) > 0) {

    // For each term ID, generate an array of descendant term IDs to the right depth.
    $descendant_tids = array();
    if ($depth === 'all') {
      $depth = NULL;
    }
    foreach ($tids as $index => $tid) {
      $term = taxonomy_term_load($tid);
      $tree = taxonomy_get_tree($term->vid, $tid, -1, $depth);
      if ($tree) {
        $descendant_tids[] = array_merge(array(
          $tid,
        ), array_map('_taxonomy_get_tid_from_term', $tree));
      }
      else {
        $descendant_tids[] = $tid;
      }
    }
    $query = db_select('node', 'n')
      ->distinct()
      ->fields('n', array(
      'nid',
      'sticky',
      'title',
      'created',
      'status',
    ))
      ->condition('n.status', 1);
    $query
      ->innerJoin('taxonomy_index', 'tn', 'n.nid = tn.nid');
    $query
      ->orderBy('n.sticky', 'DESC')
      ->orderBy('n.created', 'DESC');
    $query
      ->addTag('node_access');
    if ($operator == 'or') {
      $query
        ->condition('tn.tid', $descendant_tids, 'IN');

      // <<<--- Okay for "or";
    }
    else {

      // @TODO: how could we get here?
      foreach ($descendant_tids as $index => $tids) {
        $query
          ->innerJoin('taxonomy_index', 'tn' . $index, 'n.nid = tn' . $index . '.nid');
        $query
          ->condition('tn' . $index . '.tid', $tids, 'IN');
      }
    }

    //    dpq($query);
    $query
      ->range(0, 10);

    // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    $result = $query
      ->execute()
      ->fetchAll();
  }
  else {
    drupal_set_message(t("You didn't tell me what to look for."));
  }
  return $result;
}