diff --git a/core/modules/views/src/Plugin/views/query/Sql.php b/core/modules/views/src/Plugin/views/query/Sql.php index 7503e1d8b1..5f166d496a 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') * ); @@ -1111,13 +1110,26 @@ 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'); + $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'; + } + + $connection = Database::getConnection($target, $key); + $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;