diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/query/QueryPluginBase.php b/core/modules/views/lib/Drupal/views/Plugin/views/query/QueryPluginBase.php index 60ed13f..a26434f 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/views/query/QueryPluginBase.php +++ b/core/modules/views/lib/Drupal/views/Plugin/views/query/QueryPluginBase.php @@ -48,6 +48,13 @@ public function query($get_count = FALSE) { } function alter(ViewExecutable $view) { } /** + * Run before the view is built. + */ + public function preQuery() { + + } + + /** * Builds the necessary info to execute the query. * * @param view $view diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/query/Sql.php b/core/modules/views/lib/Drupal/views/Plugin/views/query/Sql.php index 2ba6eb6..27febac 100644 --- a/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 @@ protected function compileFields($query) { * 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 @@ public function query($get_count = FALSE) { $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); @@ -1338,6 +1319,35 @@ function alter(ViewExecutable $view) { } /** + * 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) { diff --git a/core/modules/views/lib/Drupal/views/ViewExecutable.php b/core/modules/views/lib/Drupal/views/ViewExecutable.php index 1e59e22..aaf9f55 100644 --- a/core/modules/views/lib/Drupal/views/ViewExecutable.php +++ b/core/modules/views/lib/Drupal/views/ViewExecutable.php @@ -773,7 +773,7 @@ public function getBaseTables() { } /** - * Run the preQuery() on all active handlers. + * Run the preQuery() on all active handlers and the query plugin. */ protected function _preQuery() { foreach ($this::viewsHandlerTypes() as $key => $info) { @@ -785,6 +785,8 @@ protected function _preQuery() { $position++; } } + + $this->query->preQuery(); } /** @@ -995,7 +997,7 @@ public function build($display_id = NULL) { // Run through our handlers and ensure they have necessary information. $this->initHandlers(); - // Let the handlers interact with each other if they really want. + // Let the handlers and query interact with each other if they really want. $this->_preQuery(); if ($this->display_handler->usesExposed()) {