Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / taxonomy / src / Plugin / views / filter / TaxonomyIndexTidDepth.php
1 <?php
2
3 namespace Drupal\taxonomy\Plugin\views\filter;
4
5 use Drupal\Core\Database\Query\Condition;
6 use Drupal\Core\Form\FormStateInterface;
7
8 /**
9  * Filter handler for taxonomy terms with depth.
10  *
11  * This handler is actually part of the node table and has some restrictions,
12  * because it uses a subquery to find nodes with.
13  *
14  * @ingroup views_filter_handlers
15  *
16  * @ViewsFilter("taxonomy_index_tid_depth")
17  */
18 class TaxonomyIndexTidDepth extends TaxonomyIndexTid {
19
20   public function operatorOptions($which = 'title') {
21     return [
22       'or' => $this->t('Is one of'),
23     ];
24   }
25
26   protected function defineOptions() {
27     $options = parent::defineOptions();
28
29     $options['depth'] = ['default' => 0];
30
31     return $options;
32   }
33
34   public function buildExtraOptionsForm(&$form, FormStateInterface $form_state) {
35     parent::buildExtraOptionsForm($form, $form_state);
36
37     $form['depth'] = [
38       '#type' => 'weight',
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).'),
42     ];
43   }
44
45   public function query() {
46     // If no filter values are present, then do nothing.
47     if (count($this->value) == 0) {
48       return;
49     }
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);
54       }
55       $operator = '=';
56     }
57     else {
58       $operator = 'IN';
59     }
60
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;
67     }
68     // If no relationship, then use the alias of the base table.
69     else {
70       $this->tableAlias = $this->query->ensureTable($this->view->storage->get('base_table'));
71     }
72
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);
77     $last = "tn";
78
79     if ($this->options['depth'] > 0) {
80       $subquery->leftJoin('taxonomy_term__parent', 'th', "th.entity_id = tn.tid");
81       $last = "th";
82       foreach (range(1, abs($this->options['depth'])) as $count) {
83         $subquery->leftJoin('taxonomy_term__parent', "th$count", "$last.parent_target_id = th$count.entity_id");
84         $where->condition("th$count.entity_id", $this->value, $operator);
85         $last = "th$count";
86       }
87     }
88     elseif ($this->options['depth'] < 0) {
89       foreach (range(1, abs($this->options['depth'])) as $count) {
90         $field = $count == 1 ? 'tid' : 'entity_id';
91         $subquery->leftJoin('taxonomy_term__parent', "th$count", "$last.$field = th$count.parent_target_id");
92         $where->condition("th$count.entity_id", $this->value, $operator);
93         $last = "th$count";
94       }
95     }
96
97     $subquery->condition($where);
98     $this->query->addWhere($this->options['group'], "$this->tableAlias.$this->realField", $subquery, 'IN');
99   }
100
101 }