function Combine::opContainsWord

Same name and namespace in other branches
  1. 9 core/modules/views/src/Plugin/views/filter/Combine.php \Drupal\views\Plugin\views\filter\Combine::opContainsWord()
  2. 10 core/modules/views/src/Plugin/views/filter/Combine.php \Drupal\views\Plugin\views\filter\Combine::opContainsWord()
  3. 11.x 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:

Overrides StringFilter::opContainsWord

File

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

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->connection
        ->mapConditionOperator('LIKE');
    $operator = isset($operator['operator']) ? $operator['operator'] : '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) . '%',
        ]);
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.