3 namespace Drupal\taxonomy\Plugin\views\filter;
5 use Drupal\Core\Form\FormStateInterface;
8 * Filter handler for taxonomy terms with depth.
10 * This handler is actually part of the node table and has some restrictions,
11 * because it uses a subquery to find nodes with.
13 * @ingroup views_filter_handlers
15 * @ViewsFilter("taxonomy_index_tid_depth")
17 class TaxonomyIndexTidDepth extends TaxonomyIndexTid {
19 public function operatorOptions($which = 'title') {
21 'or' => $this->t('Is one of'),
25 protected function defineOptions() {
26 $options = parent::defineOptions();
28 $options['depth'] = ['default' => 0];
33 public function buildExtraOptionsForm(&$form, FormStateInterface $form_state) {
34 parent::buildExtraOptionsForm($form, $form_state);
38 '#title' => $this->t('Depth'),
39 '#default_value' => $this->options['depth'],
40 '#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).'),
44 public function query() {
45 // If no filter values are present, then do nothing.
46 if (count($this->value) == 0) {
49 elseif (count($this->value) == 1) {
50 // Sometimes $this->value is an array with a single element so convert it.
51 if (is_array($this->value)) {
52 $this->value = current($this->value);
57 $operator = 'IN';# " IN (" . implode(', ', array_fill(0, sizeof($this->value), '%d')) . ")";
60 // The normal use of ensureMyTable() here breaks Views.
61 // So instead we trick the filter into using the alias of the base table.
62 // See https://www.drupal.org/node/271833.
63 // If a relationship is set, we must use the alias it provides.
64 if (!empty($this->relationship)) {
65 $this->tableAlias = $this->relationship;
67 // If no relationship, then use the alias of the base table.
69 $this->tableAlias = $this->query->ensureTable($this->view->storage->get('base_table'));
72 // Now build the subqueries.
73 $subquery = db_select('taxonomy_index', 'tn');
74 $subquery->addField('tn', 'nid');
75 $where = db_or()->condition('tn.tid', $this->value, $operator);
78 if ($this->options['depth'] > 0) {
79 $subquery->leftJoin('taxonomy_term_hierarchy', 'th', "th.tid = tn.tid");
81 foreach (range(1, abs($this->options['depth'])) as $count) {
82 $subquery->leftJoin('taxonomy_term_hierarchy', "th$count", "$last.parent = th$count.tid");
83 $where->condition("th$count.tid", $this->value, $operator);
87 elseif ($this->options['depth'] < 0) {
88 foreach (range(1, abs($this->options['depth'])) as $count) {
89 $subquery->leftJoin('taxonomy_term_hierarchy', "th$count", "$last.tid = th$count.parent");
90 $where->condition("th$count.tid", $this->value, $operator);
95 $subquery->condition($where);
96 $this->query->addWhere($this->options['group'], "$this->tableAlias.$this->realField", $subquery, 'IN');