3 namespace Drupal\taxonomy\Plugin\views\filter;
5 use Drupal\Core\Database\Query\Condition;
6 use Drupal\Core\Form\FormStateInterface;
9 * Filter handler for taxonomy terms with depth.
11 * This handler is actually part of the node table and has some restrictions,
12 * because it uses a subquery to find nodes with.
14 * @ingroup views_filter_handlers
16 * @ViewsFilter("taxonomy_index_tid_depth")
18 class TaxonomyIndexTidDepth extends TaxonomyIndexTid {
20 public function operatorOptions($which = 'title') {
22 'or' => $this->t('Is one of'),
26 protected function defineOptions() {
27 $options = parent::defineOptions();
29 $options['depth'] = ['default' => 0];
34 public function buildExtraOptionsForm(&$form, FormStateInterface $form_state) {
35 parent::buildExtraOptionsForm($form, $form_state);
39 '#title' => $this->t('Depth'),
40 '#default_value' => $this->options['depth'],
41 '#description' => $this->t('The depth will match nodes 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 nodes 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).'),
45 public function query() {
46 // If no filter values are present, then do nothing.
47 if (count($this->value) == 0) {
50 elseif (count($this->value) == 1) {
51 // Sometimes $this->value is an array with a single element so convert it.
52 if (is_array($this->value)) {
53 $this->value = current($this->value);
61 // The normal use of ensureMyTable() here breaks Views.
62 // So instead we trick the filter into using the alias of the base table.
63 // See https://www.drupal.org/node/271833.
64 // If a relationship is set, we must use the alias it provides.
65 if (!empty($this->relationship)) {
66 $this->tableAlias = $this->relationship;
68 // If no relationship, then use the alias of the base table.
70 $this->tableAlias = $this->query->ensureTable($this->view->storage->get('base_table'));
73 // Now build the subqueries.
74 $subquery = db_select('taxonomy_index', 'tn');
75 $subquery->addField('tn', 'nid');
76 $where = (new Condition('OR'))->condition('tn.tid', $this->value, $operator);
79 if ($this->options['depth'] > 0) {
80 $subquery->leftJoin('taxonomy_term_hierarchy', 'th', "th.tid = tn.tid");
82 foreach (range(1, abs($this->options['depth'])) as $count) {
83 $subquery->leftJoin('taxonomy_term_hierarchy', "th$count", "$last.parent = th$count.tid");
84 $where->condition("th$count.tid", $this->value, $operator);
88 elseif ($this->options['depth'] < 0) {
89 foreach (range(1, abs($this->options['depth'])) as $count) {
90 $subquery->leftJoin('taxonomy_term_hierarchy', "th$count", "$last.tid = th$count.parent");
91 $where->condition("th$count.tid", $this->value, $operator);
96 $subquery->condition($where);
97 $this->query->addWhere($this->options['group'], "$this->tableAlias.$this->realField", $subquery, 'IN');