3 namespace Drupal\views\Plugin\views\argument;
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\Core\Plugin\Context\ContextDefinition;
9 * Basic argument handler for arguments that are numeric. Incorporates
12 * @ingroup views_argument_handlers
14 * @ViewsArgument("numeric")
16 class NumericArgument extends ArgumentPluginBase {
19 * The operator used for the query: or|and.
25 * The actual value which is used for querying.
30 protected function defineOptions() {
31 $options = parent::defineOptions();
33 $options['break_phrase'] = ['default' => FALSE];
34 $options['not'] = ['default' => FALSE];
39 public function buildOptionsForm(&$form, FormStateInterface $form_state) {
40 parent::buildOptionsForm($form, $form_state);
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',
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',
60 public function title() {
61 if (!$this->argument) {
62 return !empty($this->definition['empty field name']) ? $this->definition['empty field name'] : $this->t('Uncategorized');
65 if (!empty($this->options['break_phrase'])) {
66 $break = static::breakString($this->argument, FALSE);
67 $this->value = $break->value;
68 $this->operator = $break->operator;
71 $this->value = [$this->argument];
72 $this->operator = 'or';
75 if (empty($this->value)) {
76 return !empty($this->definition['empty field name']) ? $this->definition['empty field name'] : $this->t('Uncategorized');
79 if ($this->value === [-1]) {
80 return !empty($this->definition['invalid input']) ? $this->definition['invalid input'] : $this->t('Invalid input');
83 return implode($this->operator == 'or' ? ' + ' : ', ', $this->titleQuery());
87 * Override for specific title lookups.
89 * Returns all titles, if it's just one title it's an array with one entry.
91 public function titleQuery() {
95 public function query($group_by = FALSE) {
96 $this->ensureMyTable();
98 if (!empty($this->options['break_phrase'])) {
99 $break = static::breakString($this->argument, FALSE);
100 $this->value = $break->value;
101 $this->operator = $break->operator;
104 $this->value = [$this->argument];
107 $placeholder = $this->placeholder();
108 $null_check = empty($this->options['not']) ? '' : " OR $this->tableAlias.$this->realField IS NULL";
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]);
116 $operator = empty($this->options['not']) ? '=' : '!=';
117 $this->query->addWhereExpression(0, "$this->tableAlias.$this->realField $operator $placeholder" . $null_check, [$placeholder => $this->argument]);
124 public function getSortName() {
125 return $this->t('Numerical', [], ['context' => 'Sort order']);
131 public function getContextDefinition() {
132 if ($context_definition = parent::getContextDefinition()) {
133 return $context_definition;
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);