You are here

public function TaxonomyEntityIndexDepth::query in Taxonomy Entity Index 8

Set up the query for this argument.

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

Overrides IndexTidDepth::query

File

src/Plugin/views/argument/TaxonomyEntityIndexDepth.php, line 74

Class

TaxonomyEntityIndexDepth
Abstract argument handler for entity taxonomy terms with depth.

Namespace

Drupal\taxonomy_entity_index\Plugin\views\argument

Code

public function query($group_by = FALSE) {
  $this
    ->ensureMyTable();
  if (!empty($this->options['break_phrase']) && static::breakString($this->argument)->value === [
    -1,
  ]) {
    return FALSE;
  }
  list($operator, $ids) = $this
    ->getOperatorAndIds();
  if (!($tids = $this
    ->convertIdsToEntityIds($ids))) {
    $tids = [
      -1,
    ];
  }

  // Now build the subqueries.
  $subquery = $this->database
    ->select('taxonomy_entity_index', 'tn');
  $base_field = $this->baseTableInfo['taxonomy_entity_index_entity_tid']['relationship']['base field'];
  $real_field = $this->baseTableInfo['taxonomy_entity_index_entity_tid']['relationship']['real field'];
  $subquery
    ->addField('tn', $base_field);
  if (isset($this->baseTableInfo['table']['entity type'])) {
    $subquery
      ->condition('entity_type', $this->baseTableInfo['table']['entity type']);
  }
  $or = new Condition('OR');
  $where = $or
    ->condition('tn.tid', $tids, $operator);
  $last = "tn";
  if ($this->options['depth'] > 0) {
    $subquery
      ->leftJoin('taxonomy_term__parent', 'th', "th.entity_id = tn.tid");
    $last = "th";
    foreach (range(1, abs($this->options['depth'])) as $count) {
      $subquery
        ->leftJoin('taxonomy_term__parent', "th{$count}", "{$last}.parent_target_id = th{$count}.entity_id");
      $where
        ->condition("th{$count}.entity_id", $tids, $operator);
      $last = "th{$count}";
    }
  }
  elseif ($this->options['depth'] < 0) {
    foreach (range(1, abs($this->options['depth'])) as $count) {
      $subquery
        ->leftJoin('taxonomy_term__parent', "th{$count}", "{$last}.entity_id = th{$count}.parent_target_id");
      $where
        ->condition("th{$count}.entity_id", $tids, $operator);
      $last = "th{$count}";
    }
  }
  $subquery
    ->condition($where);
  $this->query
    ->addWhere(0, "{$this->tableAlias}.{$real_field}", $subquery, 'IN');
}