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;
class TaxonomyEntityIndexTidDepth extends TaxonomyIndexTidDepth implements ContainerFactoryPluginInterface {
protected $vocabularyStorage;
protected $termStorage;
private $baseTableInfo = [];
protected $database;
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;
}
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'));
}
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);
}
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).');
}
public function query() {
if (count($this->value) == 0) {
return;
}
elseif (count($this->value) == 1) {
if (is_array($this->value)) {
$this->value = current($this->value);
}
$operator = '=';
}
else {
$operator = 'IN';
}
if (!empty($this->relationship)) {
$this->tableAlias = $this->relationship;
}
else {
$this->tableAlias = $this->query
->ensureTable($this->view->storage
->get('base_table'));
}
$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');
}
}