diff -u b/core/modules/views/lib/Drupal/views/Plugin/views/query/Sql.php b/core/modules/views/lib/Drupal/views/Plugin/views/query/Sql.php --- b/core/modules/views/lib/Drupal/views/Plugin/views/query/Sql.php +++ b/core/modules/views/lib/Drupal/views/Plugin/views/query/Sql.php @@ -1172,6 +1172,7 @@ * Provide a countquery if this is true, otherwise provide a normal query. */ public function query($get_count = FALSE) { + $query = $get_count ? $this->view->build_info['count_query'] : $this->view->build_info['query']; // Check query distinct value. if (empty($this->no_distinct) && $this->distinct && !empty($this->fields)) { $base_field_alias = $this->addField($this->view->storage->get('base_table'), $this->view->storage->get('base_field')); @@ -1198,26 +1199,6 @@ $this->get_count_optimized = TRUE; } - $options = array(); - $target = 'default'; - $key = 'default'; - // Detect an external database and set the - if (isset($this->view->base_database)) { - $key = $this->view->base_database; - } - - // Set the slave target if the slave option is set - if (!empty($this->options['slave'])) { - $target = 'slave'; - } - - // Go ahead and build the query. - // db_select doesn't support to specify the key, so use getConnection directly. - $query = Database::getConnection($target, $key) - ->select($this->view->storage->get('base_table'), $this->view->storage->get('base_table'), $options) - ->addTag('views') - ->addTag('views_' . $this->view->storage->id()); - // Add the tags added to the view itself. foreach ($this->tags as $tag) { $query->addTag($tag); @@ -1239,7 +1220,6 @@ * Provide a countquery if this is true, otherwise provide a normal query. */ public function query($get_count = FALSE) { - $query = $get_count ? $this->view->build_info['count_query'] : $this->view->build_info['query']; // Check query distinct value. if (empty($this->no_distinct) && $this->distinct && !empty($this->fields)) { $base_field_alias = $this->add_field($this->view->storage->get('base_table'), $this->view->storage->get('base_field')); @@ -1266,6 +1246,26 @@ $this->get_count_optimized = TRUE; } + $options = array(); + $target = 'default'; + $key = 'default'; + // Detect an external database and set the + if (isset($this->view->base_database)) { + $key = $this->view->base_database; + } + + // Set the slave target if the slave option is set + if (!empty($this->options['slave'])) { + $target = 'slave'; + } + + // Go ahead and build the query. + // db_select doesn't support to specify the key, so use getConnection directly. + $query = Database::getConnection($target, $key) + ->select($this->view->storage->get('base_table'), $this->view->storage->get('base_table'), $options) + ->addTag('views') + ->addTag('views_' . $this->view->storage->id()); + // Add the tags added to the view itself. foreach ($this->tags as $tag) { $query->addTag($tag); @@ -1319,6 +1319,35 @@ } /** + * Implements \Drupal\views\Plugin\views\query\QueryPluginBase::preQuery(). + */ + public function preQuery() { + // Setup the query object, with the right database connection. + $options = array(); + $target = 'default'; + $key = 'default'; + // Detect an external database and set the + if (isset($this->view->base_database)) { + $key = $this->view->base_database; + } + + // Set the slave target if the slave option is set + if (!empty($this->options['slave'])) { + $target = 'slave'; + } + + // Go ahead and build the query. + // db_select doesn't support to specify the key, so use getConnection directly. + $query = Database::getConnection($target, $key) + ->select($this->view->storage->get('base_table'), $this->view->storage->get('base_table'), $options) + ->addTag('views') + ->addTag('views_' . $this->view->storage->id()); + + $this->view->build_info['query'] = $query; + $this->view->build_info['count_query'] = clone $query; + } + + /** * Builds the necessary info to execute the query. */ function build(ViewExecutable $view) { @@ -1389,35 +1418,6 @@ } /** - * Implements \Drupal\views\Plugin\views\query\QueryPluginBase::preQuery(). - */ - public function preQuery() { - // Setup the query object, with the right database connection. - $options = array(); - $target = 'default'; - $key = 'default'; - // Detect an external database and set the - if (isset($this->view->base_database)) { - $key = $this->view->base_database; - } - - // Set the slave target if the slave option is set - if (!empty($this->options['slave'])) { - $target = 'slave'; - } - - // Go ahead and build the query. - // db_select doesn't support to specify the key, so use getConnection directly. - $query = Database::getConnection($target, $key) - ->select($this->view->storage->get('base_table'), $this->view->storage->get('base_table'), $options) - ->addTag('views') - ->addTag('views_' . $this->view->storage->id()); - - $this->view->build_info['query'] = $query; - $this->view->build_info['count_query'] = clone $query; - } - - /** * Builds the necessary info to execute the query. */ function build(ViewExecutable $view) {