function Combine::opContainsWord
Same name in other branches
- 8.9.x core/modules/views/src/Plugin/views/filter/Combine.php \Drupal\views\Plugin\views\filter\Combine::opContainsWord()
- 10 core/modules/views/src/Plugin/views/filter/Combine.php \Drupal\views\Plugin\views\filter\Combine::opContainsWord()
- 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: The expression to add to the query.
Overrides StringFilter::opContainsWord
File
-
core/
modules/ views/ src/ Plugin/ views/ filter/ Combine.php, line 156
Class
- Combine
- Filter handler which allows to search on multiple fields.
Namespace
Drupal\views\Plugin\views\filterCode
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) . '%',
]);
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.