You are here

TaxonomyEntityIndexTidDepth.php in Taxonomy Entity Index 8

File

src/Plugin/views/filter/TaxonomyEntityIndexTidDepth.php
View source
<?php

namespace Drupal\taxonomy_entity_index\Plugin\views\filter;

use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\taxonomy\Plugin\views\filter\TaxonomyIndexTidDepth;
use Drupal\taxonomy\TermStorageInterface;
use Drupal\taxonomy\VocabularyStorageInterface;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\ViewExecutable;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Filter handler for taxonomy terms with depth.
 *
 * @ingroup views_filter_handlers
 *
 * @ViewsFilter("taxonomy_entity_index_tid_depth")
 */
class TaxonomyEntityIndexTidDepth extends TaxonomyIndexTidDepth implements ContainerFactoryPluginInterface {

  /**
   * The vocabulary storage.
   *
   * @var \Drupal\taxonomy\VocabularyStorageInterface
   */
  protected $vocabularyStorage;

  /**
   * The term storage.
   *
   * @var \Drupal\taxonomy\TermStorageInterface
   */
  protected $termStorage;

  /**
   * Stores the base table information.
   *
   * @var array
   */
  private $baseTableInfo = [];

  /**
   * The database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $database;

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, Connection $database, VocabularyStorageInterface $vocabulary_storage, TermStorageInterface $term_storage) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $vocabulary_storage, $term_storage);
    $this->database = $database;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('database'), $container
      ->get('entity_type.manager')
      ->getStorage('taxonomy_vocabulary'), $container
      ->get('entity_type.manager')
      ->getStorage('taxonomy_term'));
  }

  /**
   * {@inheritdoc}
   */
  public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
    parent::init($view, $display, $options);
    $this->baseTableInfo = \Drupal::service('views.views_data')
      ->get($this->table);
  }

  /**
   * {@inheritdoc}
   */
  public function buildExtraOptionsForm(&$form, FormStateInterface $form_state) {
    parent::buildExtraOptionsForm($form, $form_state);
    $form['depth']['#description'] = $this
      ->t('The depth will match entities tagged with terms in the hierarchy. For example, if you have the term "fruit" and a child term "apple", with a depth of 1 (or higher) then filtering for the term "fruit" will get entities that are tagged with "apple" as well as "fruit". If negative, the reverse is true; searching for "apple" will also pick up nodes tagged with "fruit" if depth is -1 (or lower).');
  }

  /**
   * {@inheritdoc}
   */
  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');
  }

}

Classes

Namesort descending Description
TaxonomyEntityIndexTidDepth Filter handler for taxonomy terms with depth.