public function IndexTidDepth::query

Set up the query for this argument.

The argument sent may be found at $this->argument.

Overrides ArgumentPluginBase::query

File

drupal/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/views/argument/IndexTidDepth.php, line 98
Definition of Drupal\taxonomy\Plugin\views\argument\IndexTidDepth.

Class

IndexTidDepth
Argument handler for taxonomy terms with depth.

Namespace

Drupal\taxonomy\Plugin\views\argument

Code

public function query($group_by = FALSE) {
  $this
    ->ensureMyTable();
  if (!empty($this->options['break_phrase'])) {
    $tids = new \stdClass();
    $tids->value = $this->argument;
    $tids = $this
      ->breakPhrase($this->argument, $tids);
    if ($tids->value == array(
      -1,
    )) {
      return FALSE;
    }
    if (count($tids->value) > 1) {
      $operator = 'IN';
    }
    else {
      $operator = '=';
    }
    $tids = $tids->value;
  }
  else {
    $operator = "=";
    $tids = $this->argument;
  }

  // Now build the subqueries.
  $subquery = db_select('taxonomy_index', 'tn');
  $subquery
    ->addField('tn', 'nid');
  $where = db_or()
    ->condition('tn.tid', $tids, $operator);
  $last = "tn";
  if ($this->options['depth'] > 0) {
    $subquery
      ->leftJoin('taxonomy_term_hierarchy', 'th', "th.tid = tn.tid");
    $last = "th";
    foreach (range(1, abs($this->options['depth'])) as $count) {
      $subquery
        ->leftJoin('taxonomy_term_hierarchy', "th{$count}", "{$last}.parent = th{$count}.tid");
      $where
        ->condition("th{$count}.tid", $tids, $operator);
      $last = "th{$count}";
    }
  }
  elseif ($this->options['depth'] < 0) {
    foreach (range(1, abs($this->options['depth'])) as $count) {
      $subquery
        ->leftJoin('taxonomy_term_hierarchy', "th{$count}", "{$last}.tid = th{$count}.parent");
      $where
        ->condition("th{$count}.tid", $tids, $operator);
      $last = "th{$count}";
    }
  }
  $subquery
    ->condition($where);
  $this->query
    ->addWhere(0, "{$this->tableAlias}.{$this->realField}", $subquery, 'IN');
}