You are here

class NumericArgument in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/views/src/Plugin/views/argument/NumericArgument.php \Drupal\views\Plugin\views\argument\NumericArgument
  2. 9 core/modules/views/src/Plugin/views/argument/NumericArgument.php \Drupal\views\Plugin\views\argument\NumericArgument

Basic argument handler for arguments that are numeric. Incorporates break_phrase.

Plugin annotation

@ViewsArgument("numeric");

Hierarchy

  • class \Drupal\views\Plugin\views\argument\NumericArgument extends \Drupal\views\Plugin\views\argument\ArgumentPluginBase

Expanded class hierarchy of NumericArgument

7 files declare their use of NumericArgument
Fid.php in core/modules/file/src/Plugin/views/argument/Fid.php
Nid.php in core/modules/node/src/Plugin/views/argument/Nid.php
NumberListField.php in core/modules/options/src/Plugin/views/argument/NumberListField.php
Taxonomy.php in core/modules/taxonomy/src/Plugin/views/argument/Taxonomy.php
Uid.php in core/modules/user/src/Plugin/views/argument/Uid.php

... See full list

File

core/modules/views/src/Plugin/views/argument/NumericArgument.php, line 16

Namespace

Drupal\views\Plugin\views\argument
View source
class NumericArgument extends ArgumentPluginBase {

  /**
   * The actual value which is used for querying.
   * @var array
   */
  public $value;
  protected function defineOptions() {
    $options = parent::defineOptions();
    $options['break_phrase'] = [
      'default' => FALSE,
    ];
    $options['not'] = [
      'default' => FALSE,
    ];
    return $options;
  }
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
    parent::buildOptionsForm($form, $form_state);

    // allow + for or, , for and
    $form['break_phrase'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Allow multiple values'),
      '#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).'),
      '#default_value' => !empty($this->options['break_phrase']),
      '#group' => 'options][more',
    ];
    $form['not'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Exclude'),
      '#description' => $this
        ->t('If selected, the numbers entered for the filter will be excluded rather than limiting the view.'),
      '#default_value' => !empty($this->options['not']),
      '#group' => 'options][more',
    ];
  }
  public function title() {
    if (!$this->argument) {
      return !empty($this->definition['empty field name']) ? $this->definition['empty field name'] : $this
        ->t('Uncategorized');
    }
    if (!empty($this->options['break_phrase'])) {
      $break = static::breakString($this->argument, FALSE);
      $this->value = $break->value;
      $this->operator = $break->operator;
    }
    else {
      $this->value = [
        $this->argument,
      ];
      $this->operator = 'or';
    }
    if (empty($this->value)) {
      return !empty($this->definition['empty field name']) ? $this->definition['empty field name'] : $this
        ->t('Uncategorized');
    }
    if ($this->value === [
      -1,
    ]) {
      return !empty($this->definition['invalid input']) ? $this->definition['invalid input'] : $this
        ->t('Invalid input');
    }
    return implode($this->operator == 'or' ? ' + ' : ', ', $this
      ->titleQuery());
  }

  /**
   * Override for specific title lookups.
   * @return array
   *   Returns all titles, if it's just one title it's an array with one entry.
   */
  public function titleQuery() {
    return $this->value;
  }
  public function query($group_by = FALSE) {
    $this
      ->ensureMyTable();
    if (!empty($this->options['break_phrase'])) {
      $break = static::breakString($this->argument, FALSE);
      $this->value = $break->value;
      $this->operator = $break->operator;
    }
    else {
      $this->value = [
        $this->argument,
      ];
    }
    $placeholder = $this
      ->placeholder();
    $null_check = empty($this->options['not']) ? '' : " OR {$this->tableAlias}.{$this->realField} IS NULL";
    if (count($this->value) > 1) {
      $operator = empty($this->options['not']) ? 'IN' : 'NOT IN';
      $placeholder .= '[]';
      $this->query
        ->addWhereExpression(0, "{$this->tableAlias}.{$this->realField} {$operator}({$placeholder})" . $null_check, [
        $placeholder => $this->value,
      ]);
    }
    else {
      $operator = empty($this->options['not']) ? '=' : '!=';
      $this->query
        ->addWhereExpression(0, "{$this->tableAlias}.{$this->realField} {$operator} {$placeholder}" . $null_check, [
        $placeholder => $this->argument,
      ]);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getSortName() {
    return $this
      ->t('Numerical', [], [
      'context' => 'Sort order',
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function getContextDefinition() {
    if ($context_definition = parent::getContextDefinition()) {
      return $context_definition;
    }

    // If the parent does not provide a context definition through the
    // validation plugin, fall back to the integer type.
    return new ContextDefinition('integer', $this
      ->adminLabel(), FALSE);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
NumericArgument::$value public property The actual value which is used for querying.
NumericArgument::buildOptionsForm public function 1
NumericArgument::defineOptions protected function 1
NumericArgument::getContextDefinition public function
NumericArgument::getSortName public function
NumericArgument::query public function 1
NumericArgument::title public function 2
NumericArgument::titleQuery public function Override for specific title lookups. 4