diff --git a/core/modules/views/src/Plugin/views/display/EntityReference.php b/core/modules/views/src/Plugin/views/display/EntityReference.php index c8ab57c499..80bcb6d775 100644 --- a/core/modules/views/src/Plugin/views/display/EntityReference.php +++ b/core/modules/views/src/Plugin/views/display/EntityReference.php @@ -3,7 +3,6 @@ namespace Drupal\views\Plugin\views\display; use Drupal\Core\Database\Connection; -use Drupal\Core\Database\Query\Condition; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -170,7 +169,7 @@ public function query() { } // Multiple search fields are OR'd together. - $conditions = new Condition('OR'); + $conditions = $this->connection->condition('OR'); // Build the condition using the selected search fields. foreach ($style_options['options']['search_fields'] as $field_id) { diff --git a/core/modules/views/src/Plugin/views/filter/BooleanOperator.php b/core/modules/views/src/Plugin/views/filter/BooleanOperator.php index 81a31880c3..e8446f2d44 100644 --- a/core/modules/views/src/Plugin/views/filter/BooleanOperator.php +++ b/core/modules/views/src/Plugin/views/filter/BooleanOperator.php @@ -2,10 +2,11 @@ namespace Drupal\views\Plugin\views\filter; -use Drupal\Core\Database\Query\Condition; +use Drupal\Core\Database\Connection; use Drupal\Core\Form\FormStateInterface; use Drupal\views\Plugin\views\display\DisplayPluginBase; use Drupal\views\ViewExecutable; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Simple filter to handle matching of boolean values @@ -48,6 +49,26 @@ class BooleanOperator extends FilterPluginBase { // Whether to accept NULL as a false value or not public $accept_null = FALSE; + /** + * The database connection. + * + * @var \Drupal\Core\Database\Connection + */ + protected $connection; + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + $instance = new static( + $configuration, + $plugin_id, + $plugin_definition, + ); + $instance->connection = $container->get('database'); + return $instance; + } + /** * {@inheritdoc} */ @@ -236,12 +257,12 @@ protected function queryOpBoolean($field, $query_operator = self::EQUAL) { if (empty($this->value)) { if ($this->accept_null) { if ($query_operator === self::EQUAL) { - $condition = (new Condition('OR')) + $condition = $this->connection->condition('OR') ->condition($field, 0, $query_operator) ->isNull($field); } else { - $condition = (new Condition('AND')) + $condition = $this->connection->condition('AND') ->condition($field, 0, $query_operator) ->isNotNull($field); } diff --git a/core/modules/views/src/Plugin/views/filter/StringFilter.php b/core/modules/views/src/Plugin/views/filter/StringFilter.php index 229eb90733..77798e07a9 100644 --- a/core/modules/views/src/Plugin/views/filter/StringFilter.php +++ b/core/modules/views/src/Plugin/views/filter/StringFilter.php @@ -3,7 +3,6 @@ namespace Drupal\views\Plugin\views\filter; use Drupal\Core\Database\Connection; -use Drupal\Core\Database\Query\Condition; use Drupal\Core\Form\FormStateInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -330,7 +329,7 @@ protected function opContains($field) { } protected function opContainsWord($field) { - $where = $this->operator == 'word' ? new Condition('OR') : new Condition('AND'); + $where = $this->operator == 'word' ? $this->connection->condition('OR') : $this->connection->condition('AND'); // Don't filter on empty strings. if (empty($this->value)) { diff --git a/core/modules/views/src/Plugin/views/query/Sql.php b/core/modules/views/src/Plugin/views/query/Sql.php index 59b9eb7e83..6cf84afda7 100644 --- a/core/modules/views/src/Plugin/views/query/Sql.php +++ b/core/modules/views/src/Plugin/views/query/Sql.php @@ -5,7 +5,6 @@ use Drupal\Component\Utility\NestedArray; use Drupal\Core\Cache\Cache; use Drupal\Core\Database\Database; -use Drupal\Core\Database\Query\Condition; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Messenger\MessengerInterface; @@ -885,7 +884,7 @@ public function clearFields() { * @code * $this->query->addWhere( * $this->options['group'], - * (new Condition('OR')) + * ($connection->condition('OR')) * ->condition($field, $value, 'NOT IN') * ->condition($field, $value, 'IS NULL') * ); @@ -1095,6 +1094,22 @@ public function placeholder($base = 'views') { } } + public function getConnection() { + $target = 'default'; + $key = 'default'; + // Detect an external database and set the + if (isset($this->view->base_database)) { + $key = $this->view->base_database; + } + + // Set the replica target if the replica option is set + if (!empty($this->options['replica'])) { + $target = 'replica'; + } + + return Database::getConnection($target, $key); + } + /** * Construct the "WHERE" or "HAVING" part of the query. * @@ -1111,13 +1126,14 @@ protected function buildCondition($where = 'where') { $has_arguments = FALSE; $has_filter = FALSE; - $main_group = new Condition('AND'); - $filter_group = $this->groupOperator == 'OR' ? new Condition('OR') : new Condition('AND'); + $connection = $this->getConnection(); + $main_group = $connection->condition('AND'); + $filter_group = $this->groupOperator == 'OR' ? $connection->condition('OR') : $connection->condition('AND'); foreach ($this->$where as $group => $info) { if (!empty($info['conditions'])) { - $sub_group = $info['type'] == 'OR' ? new Condition('OR') : new Condition('AND'); + $sub_group = $info['type'] == 'OR' ? $connection->condition('OR') : $connection->condition('AND'); foreach ($info['conditions'] as $clause) { if ($clause['operator'] == 'formula') { $has_condition = TRUE; @@ -1283,20 +1299,8 @@ public function query($get_count = FALSE) { } $options = []; - $target = 'default'; - $key = 'default'; - // Detect an external database and set the - if (isset($this->view->base_database)) { - $key = $this->view->base_database; - } - - // Set the replica target if the replica option is set - if (!empty($this->options['replica'])) { - $target = 'replica'; - } - // Go ahead and build the query. - $query = Database::getConnection($target, $key) + $query = $this->getConnection() ->select($this->view->storage->get('base_table'), $this->view->storage->get('base_table'), $options) ->addTag('views') ->addTag('views_' . $this->view->storage->id());