Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / views / src / Plugin / views / argument / NumericArgument.php
1 <?php
2
3 namespace Drupal\views\Plugin\views\argument;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\Core\Plugin\Context\ContextDefinition;
7
8 /**
9  * Basic argument handler for arguments that are numeric. Incorporates
10  * break_phrase.
11  *
12  * @ingroup views_argument_handlers
13  *
14  * @ViewsArgument("numeric")
15  */
16 class NumericArgument extends ArgumentPluginBase {
17
18   /**
19    * The operator used for the query: or|and.
20    * @var string
21    */
22   public $operator;
23
24   /**
25    * The actual value which is used for querying.
26    * @var array
27    */
28   public $value;
29
30   protected function defineOptions() {
31     $options = parent::defineOptions();
32
33     $options['break_phrase'] = ['default' => FALSE];
34     $options['not'] = ['default' => FALSE];
35
36     return $options;
37   }
38
39   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
40     parent::buildOptionsForm($form, $form_state);
41
42     // allow + for or, , for and
43     $form['break_phrase'] = [
44       '#type' => 'checkbox',
45       '#title' => $this->t('Allow multiple values'),
46       '#description' => $this->t('If selected, users can enter multiple values in the form of 1+2+3 (for OR) or 1,2,3 (for AND).'),
47       '#default_value' => !empty($this->options['break_phrase']),
48       '#group' => 'options][more',
49     ];
50
51     $form['not'] = [
52       '#type' => 'checkbox',
53       '#title' => $this->t('Exclude'),
54       '#description' => $this->t('If selected, the numbers entered for the filter will be excluded rather than limiting the view.'),
55       '#default_value' => !empty($this->options['not']),
56       '#group' => 'options][more',
57     ];
58   }
59
60   public function title() {
61     if (!$this->argument) {
62       return !empty($this->definition['empty field name']) ? $this->definition['empty field name'] : $this->t('Uncategorized');
63     }
64
65     if (!empty($this->options['break_phrase'])) {
66       $break = static::breakString($this->argument, FALSE);
67       $this->value = $break->value;
68       $this->operator = $break->operator;
69     }
70     else {
71       $this->value = [$this->argument];
72       $this->operator = 'or';
73     }
74
75     if (empty($this->value)) {
76       return !empty($this->definition['empty field name']) ? $this->definition['empty field name'] : $this->t('Uncategorized');
77     }
78
79     if ($this->value === [-1]) {
80       return !empty($this->definition['invalid input']) ? $this->definition['invalid input'] : $this->t('Invalid input');
81     }
82
83     return implode($this->operator == 'or' ? ' + ' : ', ', $this->titleQuery());
84   }
85
86   /**
87    * Override for specific title lookups.
88    * @return array
89    *   Returns all titles, if it's just one title it's an array with one entry.
90    */
91   public function titleQuery() {
92     return $this->value;
93   }
94
95   public function query($group_by = FALSE) {
96     $this->ensureMyTable();
97
98     if (!empty($this->options['break_phrase'])) {
99       $break = static::breakString($this->argument, FALSE);
100       $this->value = $break->value;
101       $this->operator = $break->operator;
102     }
103     else {
104       $this->value = [$this->argument];
105     }
106
107     $placeholder = $this->placeholder();
108     $null_check = empty($this->options['not']) ? '' : " OR $this->tableAlias.$this->realField IS NULL";
109
110     if (count($this->value) > 1) {
111       $operator = empty($this->options['not']) ? 'IN' : 'NOT IN';
112       $placeholder .= '[]';
113       $this->query->addWhereExpression(0, "$this->tableAlias.$this->realField $operator($placeholder)" . $null_check, [$placeholder => $this->value]);
114     }
115     else {
116       $operator = empty($this->options['not']) ? '=' : '!=';
117       $this->query->addWhereExpression(0, "$this->tableAlias.$this->realField $operator $placeholder" . $null_check, [$placeholder => $this->argument]);
118     }
119   }
120
121   /**
122    * {@inheritdoc}
123    */
124   public function getSortName() {
125     return $this->t('Numerical', [], ['context' => 'Sort order']);
126   }
127
128   /**
129    * {@inheritdoc}
130    */
131   public function getContextDefinition() {
132     if ($context_definition = parent::getContextDefinition()) {
133       return $context_definition;
134     }
135
136     // If the parent does not provide a context definition through the
137     // validation plugin, fall back to the integer type.
138     return new ContextDefinition('integer', $this->adminLabel(), FALSE);
139   }
140
141 }