You are here

public function NodeOrderManager::selectNodes in Node Order 8

Finds all nodes that match selected taxonomy conditions.

NOTE: This is nearly a direct copy of taxonomy_select_nodes() -- see http://drupal.org/node/25801 if you find this sort of copy and paste upsetting...

Parameters

array $tids: An array of term IDs to match.

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

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

bool $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).

string $order: The order clause for the query that retrieve the nodes.

int $count: If $pager is TRUE, the number of nodes per page, or -1 to use the backward-compatible 'default_nodes_main' variable setting. If $pager is FALSE, the total number of nodes to select; or -1 to use the backward-compatible 'feed_default_items' variable setting; or 0 to select all nodes.

Return value

\Drupal\Core\Database\StatementInterface A resource identifier pointing to the query results.

Overrides NodeOrderManagerInterface::selectNodes

File

src/NodeOrderManager.php, line 156

Class

NodeOrderManager
Defines a service that creates & manages node ordering within taxonomy terms.

Namespace

Drupal\nodeorder

Code

public function selectNodes($tids = [], $operator = 'or', $depth = 0, $pager = TRUE, $order = 'n.sticky DESC, n.created DESC', $count = -1) {
  if (count($tids) > 0) {

    // For each term ID, generate an array of descendant term IDs
    // to the right depth.
    $descendant_tids = [];
    if ($depth === 'all') {
      $depth = NULL;
    }
    foreach ($tids as $index => $tid) {
      $term = $this->termStorage
        ->load($tid);
      $tree = $this->termStorage
        ->loadTree($term
        ->getVocabularyId(), $tid, $depth);
      $descendant_tids[] = array_merge([
        $tid,
      ], array_map(function ($value) {
        return $value
          ->id();
      }, $tree));
    }
    if ($operator == 'or') {
      $args = call_user_func_array('array_merge', $descendant_tids);
      $placeholders = db_placeholders($args, 'int');
      $sql = 'SELECT DISTINCT(n.nid), nd.sticky, nd.title, nd.created, tn.weight FROM {node} n LEFT JOIN {node_field_data} nd INNER JOIN {taxonomy_index} tn ON n.vid = tn.vid WHERE tn.tid IN (' . $placeholders . ') AND n.status = 1 ORDER BY ' . $order;
      $sql_count = 'SELECT COUNT(DISTINCT(n.nid)) FROM {node} n INNER JOIN {taxonomy_index} tn ON n.vid = tn.vid WHERE tn.tid IN (' . $placeholders . ') AND n.status = 1';
    }
    else {
      $args = [];
      $query = \Drupal::database()
        ->select('node', 'n');
      $query
        ->join('node_field_data', 'nd', 'nd.nid = n.nid');
      $query
        ->condition('nd.status', 1);
      foreach ($descendant_tids as $index => $tids) {
        $query
          ->join('taxonomy_index', "tn{$index}", "n.nid = tn{$index}.nid");
        $query
          ->condition("tn{$index}.tid", $tids, 'IN');
      }
      $query
        ->fields('nd', [
        'nid',
        'sticky',
        'title',
        'created',
      ]);

      // @todo: distinct?
      $query
        ->fields('tn0', [
        'weight',
      ]);

      // @todo: ORDER BY ' . $order;
      // $sql_count = 'SELECT COUNT(DISTINCT(n.nid))
      // FROM {node} n ' . $joins . ' WHERE n.status = 1 ' . $wheres;.
    }
    if ($pager) {
      if ($count == -1) {
        $count = $this->configFactory
          ->get('nodeorder.settings')
          ->get('default_nodes_main');
      }
      $result = pager_query($sql, $count, 0, $sql_count, $args);
    }
    else {
      if ($count == -1) {
        $count = $this->configFactory
          ->get('nodeorder.settings')
          ->get('feed_default_items');
      }
      if ($count == 0) {

        // TODO Please convert this statement to the D7 database API syntax.
        $result = $query
          ->execute();
      }
      else {

        // TODO Please convert this statement to the D7 database API syntax.
        $result = db_query_range($sql, $args);
      }
    }
  }
  return $result;
}