You are here

public function TaxonomyEntityIndexTidDepth::query in Taxonomy Entity Index 8

Add this filter to the query.

Due to the nature of fapi, the value and the operator have an unintended level of indirection. You will find them in $this->operator and $this->value respectively.

Overrides TaxonomyIndexTidDepth::query

File

src/Plugin/views/filter/TaxonomyEntityIndexTidDepth.php, line 94

Class

TaxonomyEntityIndexTidDepth
Filter handler for taxonomy terms with depth.

Namespace

Drupal\taxonomy_entity_index\Plugin\views\filter

Code

public function query() {

  // If no filter values are present, then do nothing.
  if (count($this->value) == 0) {
    return;
  }
  elseif (count($this->value) == 1) {

    // Sometimes $this->value is an array with a single element so convert it.
    if (is_array($this->value)) {
      $this->value = current($this->value);
    }
    $operator = '=';
  }
  else {
    $operator = 'IN';
  }

  // The normal use of ensure_my_table() here breaks Views.
  // So instead we trick the filter into using the alias of the base table.
  // See http://drupal.org/node/271833
  // If a relationship is set, we must use the alias it provides.
  if (!empty($this->relationship)) {
    $this->tableAlias = $this->relationship;
  }
  else {
    $this->tableAlias = $this->query
      ->ensureTable($this->view->storage
      ->get('base_table'));
  }

  // Now build the subqueries.
  $subquery = $this->database
    ->select('taxonomy_entity_index', 'tei');
  $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('tei', $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('tei.tid', $this->value, $operator);
  $last = "tei";
  if ($this->options['depth'] > 0) {
    $subquery
      ->leftJoin('taxonomy_term__parent', 'th', "th.entity_id = tei.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", $this->value, $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", $this->value, $operator);
      $last = "th{$count}";
    }
  }
  $subquery
    ->condition($where);
  $this->query
    ->addWhere($this->options['group'], "{$this->tableAlias}.{$real_field}", $subquery, 'IN');
}