6933e745610afc7965cb74264eebf94c7b969662
[yaffs-website] / web / core / modules / taxonomy / src / Plugin / views / argument / IndexTidDepth.php
1 <?php
2
3 namespace Drupal\taxonomy\Plugin\views\argument;
4
5 use Drupal\Core\Entity\EntityStorageInterface;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
8 use Drupal\views\Plugin\views\argument\ArgumentPluginBase;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10
11 /**
12  * Argument handler for taxonomy terms with depth.
13  *
14  * This handler is actually part of the node table and has some restrictions,
15  * because it uses a subquery to find nodes with.
16  *
17  * @ingroup views_argument_handlers
18  *
19  * @ViewsArgument("taxonomy_index_tid_depth")
20  */
21 class IndexTidDepth extends ArgumentPluginBase implements ContainerFactoryPluginInterface {
22
23   /**
24    * @var EntityStorageInterface
25    */
26   protected $termStorage;
27
28   /**
29    * {@inheritdoc}
30    */
31   public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityStorageInterface $termStorage) {
32     parent::__construct($configuration, $plugin_id, $plugin_definition);
33
34     $this->termStorage = $termStorage;
35   }
36
37   /**
38    * {@inheritdoc}
39    */
40   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
41     return new static($configuration, $plugin_id, $plugin_definition, $container->get('entity.manager')->getStorage('taxonomy_term'));
42   }
43
44   protected function defineOptions() {
45     $options = parent::defineOptions();
46
47     $options['depth'] = ['default' => 0];
48     $options['break_phrase'] = ['default' => FALSE];
49     $options['use_taxonomy_term_path'] = ['default' => FALSE];
50
51     return $options;
52   }
53
54   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
55     $form['depth'] = [
56       '#type' => 'weight',
57       '#title' => $this->t('Depth'),
58       '#default_value' => $this->options['depth'],
59       '#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).'),
60     ];
61
62     $form['break_phrase'] = [
63       '#type' => 'checkbox',
64       '#title' => $this->t('Allow multiple values'),
65       '#description' => $this->t('If selected, users can enter multiple values in the form of 1+2+3. Due to the number of JOINs it would require, AND will be treated as OR with this filter.'),
66       '#default_value' => !empty($this->options['break_phrase']),
67     ];
68
69     parent::buildOptionsForm($form, $form_state);
70   }
71
72   /**
73    * Override defaultActions() to remove summary actions.
74    */
75   protected function defaultActions($which = NULL) {
76     if ($which) {
77       if (in_array($which, ['ignore', 'not found', 'empty', 'default'])) {
78         return parent::defaultActions($which);
79       }
80       return;
81     }
82     $actions = parent::defaultActions();
83     unset($actions['summary asc']);
84     unset($actions['summary desc']);
85     unset($actions['summary asc by count']);
86     unset($actions['summary desc by count']);
87     return $actions;
88   }
89
90   public function query($group_by = FALSE) {
91     $this->ensureMyTable();
92
93     if (!empty($this->options['break_phrase'])) {
94       $break = static::breakString($this->argument);
95       if ($break->value === [-1]) {
96         return FALSE;
97       }
98
99       $operator = (count($break->value) > 1) ? 'IN' : '=';
100       $tids = $break->value;
101     }
102     else {
103       $operator = "=";
104       $tids = $this->argument;
105     }
106     // Now build the subqueries.
107     $subquery = db_select('taxonomy_index', 'tn');
108     $subquery->addField('tn', 'nid');
109     $where = db_or()->condition('tn.tid', $tids, $operator);
110     $last = "tn";
111
112     if ($this->options['depth'] > 0) {
113       $subquery->leftJoin('taxonomy_term_hierarchy', 'th', "th.tid = tn.tid");
114       $last = "th";
115       foreach (range(1, abs($this->options['depth'])) as $count) {
116         $subquery->leftJoin('taxonomy_term_hierarchy', "th$count", "$last.parent = th$count.tid");
117         $where->condition("th$count.tid", $tids, $operator);
118         $last = "th$count";
119       }
120     }
121     elseif ($this->options['depth'] < 0) {
122       foreach (range(1, abs($this->options['depth'])) as $count) {
123         $subquery->leftJoin('taxonomy_term_hierarchy', "th$count", "$last.tid = th$count.parent");
124         $where->condition("th$count.tid", $tids, $operator);
125         $last = "th$count";
126       }
127     }
128
129     $subquery->condition($where);
130     $this->query->addWhere(0, "$this->tableAlias.$this->realField", $subquery, 'IN');
131   }
132
133   public function title() {
134     $term = $this->termStorage->load($this->argument);
135     if (!empty($term)) {
136       return $term->getName();
137     }
138     // TODO review text
139     return $this->t('No name');
140   }
141
142 }