You are here

public function StringArgument::query in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/views/src/Plugin/views/argument/StringArgument.php \Drupal\views\Plugin\views\argument\StringArgument::query()

Build the query based upon the formula.

Overrides ArgumentPluginBase::query

File

core/modules/views/src/Plugin/views/argument/StringArgument.php, line 198

Class

StringArgument
Basic argument handler to implement string arguments that may have length limits.

Namespace

Drupal\views\Plugin\views\argument

Code

public function query($group_by = FALSE) {
  $argument = $this->argument;
  if (!empty($this->options['transform_dash'])) {
    $argument = strtr($argument, '-', ' ');
  }
  if (!empty($this->options['break_phrase'])) {
    $this
      ->unpackArgumentValue();
  }
  else {
    $this->value = [
      $argument,
    ];
    $this->operator = 'or';
  }

  // Support case-insensitive substring comparisons for PostgreSQL by
  // converting the arguments to lowercase.
  if ($this->options['case'] != 'none' && Database::getConnection()
    ->databaseType() == 'pgsql') {
    foreach ($this->value as $key => $value) {
      $this->value[$key] = mb_strtolower($value);
    }
  }
  if (!empty($this->definition['many to one'])) {
    if (!empty($this->options['glossary'])) {
      $this->helper->formula = TRUE;
    }
    $this->helper
      ->ensureMyTable();
    $this->helper
      ->addFilter();
    return;
  }
  $this
    ->ensureMyTable();
  $formula = FALSE;
  if (empty($this->options['glossary'])) {
    $field = "{$this->tableAlias}.{$this->realField}";
  }
  else {
    $formula = TRUE;
    $field = $this
      ->getFormula();
  }
  if (count($this->value) > 1) {
    $operator = 'IN';
    $argument = $this->value;
  }
  else {
    $operator = '=';
  }
  if ($formula) {
    $placeholder = $this
      ->placeholder();
    if ($operator == 'IN') {
      $field .= " IN({$placeholder})";
    }
    else {
      $field .= ' = ' . $placeholder;
    }
    $placeholders = [
      $placeholder => $argument,
    ];
    $this->query
      ->addWhereExpression(0, $field, $placeholders);
  }
  else {
    $this->query
      ->addWhere(0, $field, $argument, $operator);
  }
}