diff --git a/core/modules/views/src/Plugin/views/filter/Combine.php b/core/modules/views/src/Plugin/views/filter/Combine.php index c3c12c4..6d68f86 100644 --- a/core/modules/views/src/Plugin/views/filter/Combine.php +++ b/core/modules/views/src/Plugin/views/filter/Combine.php @@ -3,7 +3,6 @@ namespace Drupal\views\Plugin\views\filter; use Drupal\Core\Form\FormStateInterface; -use Drupal\Core\Database\Database; /** * Filter handler which allows to search on multiple fields. @@ -137,7 +136,7 @@ function opEqual($expression) { protected function opContains($expression) { $placeholder = $this->placeholder(); $operator = $this->getConditionOperator('LIKE'); - $this->query->addWhereExpression($this->options['group'], "$expression $operator $placeholder", array($placeholder => '%' . Database::getConnection()->escapeLike($this->value) . '%')); + $this->query->addWhereExpression($this->options['group'], "$expression $operator $placeholder", array($placeholder => '%' . $this->connection->escapeLike($this->value) . '%')); } /** @@ -163,45 +162,44 @@ protected function opContainsWord($expression) { // Switch between the 'word' and 'allwords' operator. $type = $this->operator == 'word' ? 'OR' : 'AND'; $group = $this->query->setWhereGroup($type); - $operator = Database::getConnection()->mapConditionOperator('LIKE'); - $operator = isset($operator['operator']) ? $operator['operator'] : 'LIKE'; + $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", array($temp_placeholder => '%' . Database::getConnection()->escapeLike($word) . '%')); + $this->query->addWhereExpression($group, "$expression $operator $temp_placeholder", array($temp_placeholder => '%' . $this->connection->escapeLike($word) . '%')); } } protected function opStartsWith($expression) { $placeholder = $this->placeholder(); $operator = $this->getConditionOperator('LIKE'); - $this->query->addWhereExpression($this->options['group'], "$expression $operator $placeholder", array($placeholder => Database::getConnection()->escapeLike($this->value) . '%')); + $this->query->addWhereExpression($this->options['group'], "$expression $operator $placeholder", array($placeholder => $this->connection->escapeLike($this->value) . '%')); } protected function opNotStartsWith($expression) { $placeholder = $this->placeholder(); $operator = $this->getConditionOperator('NOT LIKE'); - $this->query->addWhereExpression($this->options['group'], "$expression $operator $placeholder", array($placeholder => Database::getConnection()->escapeLike($this->value) . '%')); + $this->query->addWhereExpression($this->options['group'], "$expression $operator $placeholder", array($placeholder => $this->connection->escapeLike($this->value) . '%')); } protected function opEndsWith($expression) { $placeholder = $this->placeholder(); $operator = $this->getConditionOperator('LIKE'); - $this->query->addWhereExpression($this->options['group'], "$expression $operator $placeholder", array($placeholder => '%' . Database::getConnection()->escapeLike($this->value))); + $this->query->addWhereExpression($this->options['group'], "$expression $operator $placeholder", array($placeholder => '%' . $this->connection->escapeLike($this->value))); } protected function opNotEndsWith($expression) { $placeholder = $this->placeholder(); $operator = $this->getConditionOperator('NOT LIKE'); - $this->query->addWhereExpression($this->options['group'], "$expression $operator $placeholder", array($placeholder => '%' . Database::getConnection()->escapeLike($this->value))); + $this->query->addWhereExpression($this->options['group'], "$expression $operator $placeholder", array($placeholder => '%' . $this->connection->escapeLike($this->value))); } protected function opNotLike($expression) { $placeholder = $this->placeholder(); $operator = $this->getConditionOperator('NOT LIKE'); - $this->query->addWhereExpression($this->options['group'], "$expression $operator $placeholder", array($placeholder => '%' . Database::getConnection()->escapeLike($this->value) . '%')); + $this->query->addWhereExpression($this->options['group'], "$expression $operator $placeholder", array($placeholder => '%' . $this->connection->escapeLike($this->value) . '%')); } protected function opRegex($expression) { diff --git a/core/modules/views/src/Plugin/views/filter/StringFilter.php b/core/modules/views/src/Plugin/views/filter/StringFilter.php index 490cd55..99ca0e6 100644 --- a/core/modules/views/src/Plugin/views/filter/StringFilter.php +++ b/core/modules/views/src/Plugin/views/filter/StringFilter.php @@ -3,7 +3,8 @@ namespace Drupal\views\Plugin\views\filter; use Drupal\Core\Form\FormStateInterface; -use Drupal\Core\Database\Database; +use Drupal\Core\Database\Connection; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Basic textfield filter to handle string filtering commands @@ -23,6 +24,43 @@ class StringFilter extends FilterPluginBase { // exposed filter options protected $alwaysMultiple = TRUE; + /** + * The database connection. + * + * @var \Drupal\Core\Database\Connection + */ + protected $connection; + + /** + * Constructs a new StringFilter object. + * + * @param array $configuration + * A configuration array containing information about the plugin instance. + * @param string $plugin_id + * The plugin_id for the plugin instance. + * @param mixed $plugin_definition + * The plugin implementation definition. + * @param \Drupal\Core\Database\Connection $connection + * The database connection. + */ + public function __construct(array $configuration, $plugin_id, $plugin_definition, Connection $connection) { + parent::__construct($configuration, $plugin_id, $plugin_definition); + + $this->connection = $connection; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('database') + ); + } + protected function defineOptions() { $options = parent::defineOptions(); @@ -249,7 +287,7 @@ function operator() { * Specified condition operator mapping value. */ public function getConditionOperator($operator) { - $mapping = Database::getConnection()->mapConditionOperator($operator); + $mapping = $this->connection->mapConditionOperator($operator); return isset($mapping['operator']) ? $mapping['operator'] : $operator; } @@ -275,7 +313,7 @@ public function opEqual($field) { } protected function opContains($field) { - $this->query->addWhere($this->options['group'], $field, '%' . Database::getConnection()->escapeLike($this->value) . '%', 'LIKE'); + $this->query->addWhere($this->options['group'], $field, '%' . $this->connection->escapeLike($this->value) . '%', 'LIKE'); } protected function opContainsWord($field) { @@ -297,7 +335,7 @@ protected function opContainsWord($field) { $words = trim($match[2], ',?!();:-'); $words = $phrase ? array($words) : preg_split('/ /', $words, -1, PREG_SPLIT_NO_EMPTY); foreach ($words as $word) { - $where->condition($field, '%' . Database::getConnection()->escapeLike(trim($word, " ,!?")) . '%', 'LIKE'); + $where->condition($field, '%' . $this->connection->escapeLike(trim($word, " ,!?")) . '%', 'LIKE'); } } @@ -311,23 +349,23 @@ protected function opContainsWord($field) { } protected function opStartsWith($field) { - $this->query->addWhere($this->options['group'], $field, Database::getConnection()->escapeLike($this->value) . '%', 'LIKE'); + $this->query->addWhere($this->options['group'], $field, $this->connection->escapeLike($this->value) . '%', 'LIKE'); } protected function opNotStartsWith($field) { - $this->query->addWhere($this->options['group'], $field, Database::getConnection()->escapeLike($this->value) . '%', 'NOT LIKE'); + $this->query->addWhere($this->options['group'], $field, $this->connection->escapeLike($this->value) . '%', 'NOT LIKE'); } protected function opEndsWith($field) { - $this->query->addWhere($this->options['group'], $field, '%' . Database::getConnection()->escapeLike($this->value), 'LIKE'); + $this->query->addWhere($this->options['group'], $field, '%' . $this->connection->escapeLike($this->value), 'LIKE'); } protected function opNotEndsWith($field) { - $this->query->addWhere($this->options['group'], $field, '%' . Database::getConnection()->escapeLike($this->value), 'NOT LIKE'); + $this->query->addWhere($this->options['group'], $field, '%' . $this->connection->escapeLike($this->value), 'NOT LIKE'); } protected function opNotLike($field) { - $this->query->addWhere($this->options['group'], $field, '%' . Database::getConnection()->escapeLike($this->value) . '%', 'NOT LIKE'); + $this->query->addWhere($this->options['group'], $field, '%' . $this->connection->escapeLike($this->value) . '%', 'NOT LIKE'); } protected function opShorterThan($field) {