You are here

protected function Combine::opContainsWord in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/views/src/Plugin/views/filter/Combine.php \Drupal\views\Plugin\views\filter\Combine::opContainsWord()
  2. 9 core/modules/views/src/Plugin/views/filter/Combine.php \Drupal\views\Plugin\views\filter\Combine::opContainsWord()

Filters by one or more words.

By default opContainsWord uses add_where, that doesn't support complex expressions.

Parameters

string $expression: The expression to add to the query.

Overrides StringFilter::opContainsWord

File

core/modules/views/src/Plugin/views/filter/Combine.php, line 155

Class

Combine
Filter handler which allows to search on multiple fields.

Namespace

Drupal\views\Plugin\views\filter

Code

protected function opContainsWord($expression) {
  $placeholder = $this
    ->placeholder();

  // Don't filter on empty strings.
  if (empty($this->value)) {
    return;
  }

  // Match all words separated by spaces or sentences encapsulated by double
  // quotes.
  preg_match_all(static::WORDS_PATTERN, ' ' . $this->value, $matches, PREG_SET_ORDER);

  // Switch between the 'word' and 'allwords' operator.
  $type = $this->operator == 'word' ? 'OR' : 'AND';
  $group = $this->query
    ->setWhereGroup($type);
  $operator = $this
    ->getConditionOperator('LIKE');
  foreach ($matches as $match_key => $match) {
    $temp_placeholder = $placeholder . '_' . $match_key;

    // Clean up the user input and remove the sentence delimiters.
    $word = trim($match[2], ',?!();:-"');
    $this->query
      ->addWhereExpression($group, "{$expression} {$operator} {$temp_placeholder}", [
      $temp_placeholder => '%' . $this->connection
        ->escapeLike($word) . '%',
    ]);
  }
}