From 11cfa543be239a5384a6abd307896d1c785fd9bd Mon Sep 17 00:00:00 2001
From: catch <catch@35733.no-reply.drupal.org>
Date: Sun, 4 Nov 2012 21:10:57 +0000
Subject: [PATCH] Issue #1778356 by dawehner: Unify and standardize the init()
 method for the query plugin.

---
 .../search/Plugin/views/argument/Search.php   |  2 +-
 .../search/Plugin/views/filter/Search.php     |  2 +-
 .../views/filter/TaxonomyIndexTidDepth.php    |  4 +-
 .../lib/Drupal/views/ManyToOneHelper.php      |  2 +-
 .../Drupal/views/Plugin/views/HandlerBase.php |  2 +-
 .../views/argument/ArgumentPluginBase.php     |  2 +-
 .../views/display/DisplayPluginBase.php       | 11 +---
 .../Plugin/views/query/QueryPluginBase.php    | 10 ++--
 .../Drupal/views/Plugin/views/query/Sql.php   | 55 ++++++++++---------
 .../views/lib/Drupal/views/ViewExecutable.php | 13 +----
 10 files changed, 45 insertions(+), 58 deletions(-)

diff --git a/core/modules/search/lib/Drupal/search/Plugin/views/argument/Search.php b/core/modules/search/lib/Drupal/search/Plugin/views/argument/Search.php
index 8409349caad8..756b8defa65b 100644
--- a/core/modules/search/lib/Drupal/search/Plugin/views/argument/Search.php
+++ b/core/modules/search/lib/Drupal/search/Plugin/views/argument/Search.php
@@ -74,7 +74,7 @@ public function query($group_by = FALSE) {
       $this->search_score = $this->query->add_field('', "SUM($search_index.score * $search_total.count)", 'score', array('aggregate' => TRUE));
 
       if (empty($this->query->relationships[$this->relationship])) {
-        $base_table = $this->query->base_table;
+        $base_table = $this->view->storage->get('base_table');
       }
       else {
         $base_table = $this->query->relationships[$this->relationship]['base'];
diff --git a/core/modules/search/lib/Drupal/search/Plugin/views/filter/Search.php b/core/modules/search/lib/Drupal/search/Plugin/views/filter/Search.php
index 6fc2f8caf24d..8953c91cce02 100644
--- a/core/modules/search/lib/Drupal/search/Plugin/views/filter/Search.php
+++ b/core/modules/search/lib/Drupal/search/Plugin/views/filter/Search.php
@@ -155,7 +155,7 @@ public function query() {
       $this->search_score = $this->query->add_field('', "SUM($search_index.score * $search_total.count)", 'score', array('aggregate' => TRUE));
 
       if (empty($this->query->relationships[$this->relationship])) {
-        $base_table = $this->query->base_table;
+        $base_table = $this->view->storage->get('base_table');
       }
       else {
         $base_table = $this->query->relationships[$this->relationship]['base'];
diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/views/filter/TaxonomyIndexTidDepth.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/views/filter/TaxonomyIndexTidDepth.php
index ba96a52aa574..e8cee6aab73b 100644
--- a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/views/filter/TaxonomyIndexTidDepth.php
+++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/views/filter/TaxonomyIndexTidDepth.php
@@ -73,8 +73,8 @@ public function query() {
       $this->tableAlias = $this->relationship;
     }
     // If no relationship, then use the alias of the base table.
-    elseif (isset($this->query->table_queue[$this->query->base_table]['alias'])) {
-      $this->tableAlias = $this->query->table_queue[$this->query->base_table]['alias'];
+    elseif (isset($this->query->table_queue[$this->view->storage->get('base_table')]['alias'])) {
+      $this->tableAlias = $this->query->table_queue[$this->view->storage->get('base_table')]['alias'];
     }
     // This should never happen, but if it does, we fail quietly.
     else {
diff --git a/core/modules/views/lib/Drupal/views/ManyToOneHelper.php b/core/modules/views/lib/Drupal/views/ManyToOneHelper.php
index 18320eba02ea..1209901ea117 100644
--- a/core/modules/views/lib/Drupal/views/ManyToOneHelper.php
+++ b/core/modules/views/lib/Drupal/views/ManyToOneHelper.php
@@ -79,7 +79,7 @@ function add_table($join = NULL, $alias = NULL) {
 
     // Determine the primary table to seek
     if (empty($this->handler->query->relationships[$relationship])) {
-      $base_table = $this->handler->query->base_table;
+      $base_table = $this->handler->view->storage->get('base_table');
     }
     else {
       $base_table = $this->handler->query->relationships[$relationship]['base'];
diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/HandlerBase.php b/core/modules/views/lib/Drupal/views/Plugin/views/HandlerBase.php
index e67614e057fa..0b897ea3f9b4 100644
--- a/core/modules/views/lib/Drupal/views/Plugin/views/HandlerBase.php
+++ b/core/modules/views/lib/Drupal/views/Plugin/views/HandlerBase.php
@@ -591,7 +591,7 @@ public function getJoin() {
     // get the join from this table that links back to the base table.
     // Determine the primary table to seek
     if (empty($this->query->relationships[$this->relationship])) {
-      $base_table = $this->query->base_table;
+      $base_table = $this->view->storage->get('base_table');
     }
     else {
       $base_table = $this->query->relationships[$this->relationship]['base'];
diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/argument/ArgumentPluginBase.php b/core/modules/views/lib/Drupal/views/Plugin/views/argument/ArgumentPluginBase.php
index ef30140fe8cd..5ec22eac82ac 100644
--- a/core/modules/views/lib/Drupal/views/Plugin/views/argument/ArgumentPluginBase.php
+++ b/core/modules/views/lib/Drupal/views/Plugin/views/argument/ArgumentPluginBase.php
@@ -851,7 +851,7 @@ function summary_basics($count_field = TRUE) {
     // Add the number of nodes counter
     $distinct = ($this->view->display_handler->getOption('distinct') && empty($this->query->no_distinct));
 
-    $count_alias = $this->query->add_field($this->query->base_table, $this->query->base_field, 'num_records', array('count' => TRUE, 'distinct' => $distinct));
+    $count_alias = $this->query->add_field($this->view->storage->get('base_table'), $this->view->storage->get('base_field'), 'num_records', array('count' => TRUE, 'distinct' => $distinct));
     $this->query->add_groupby($this->name_alias);
 
     if ($count_field) {
diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/display/DisplayPluginBase.php b/core/modules/views/lib/Drupal/views/Plugin/views/display/DisplayPluginBase.php
index 46e79e1f415c..c2721807656f 100644
--- a/core/modules/views/lib/Drupal/views/Plugin/views/display/DisplayPluginBase.php
+++ b/core/modules/views/lib/Drupal/views/Plugin/views/display/DisplayPluginBase.php
@@ -776,20 +776,15 @@ public function getPlugin($type) {
     // Query plugins allow specifying a specific query class per base table.
     if ($type == 'query') {
       $views_data = views_fetch_data($this->view->storage->get('base_table'));
-      $name = !empty($views_data['table']['base']['query class']) ? $views_data['table']['base']['query class'] : 'views_query';
+      $name = isset($views_data['table']['base']['query_id']) ? $views_data['table']['base']['query_id'] : 'views_query';
     }
 
     // Plugin instances are stored on the display for re-use.
     if (!isset($this->plugins[$type][$name])) {
       $plugin = drupal_container()->get("plugin.manager.views.$type")->createInstance($name);
 
-      // Initialize the plugin. Query has a unique method signature.
-      if ($type == 'query') {
-        $plugin->init($this->view->storage->get('base_table'), $this->view->storage->get('base_field'), $options['options']);
-      }
-      else {
-        $plugin->init($this->view, $this, $options['options']);
-      }
+      // Initialize the plugin.
+      $plugin->init($this->view, $this, $options['options']);
 
       $this->plugins[$type][$name] = $plugin;
     }
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 0cc44c35c077..10731b7fe451 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
@@ -8,6 +8,7 @@
 namespace Drupal\views\Plugin\views\query;
 
 use Drupal\views\Plugin\views\PluginBase;
+use Drupal\views\Plugin\views\display\DisplayPluginBase;
 use Drupal\views\ViewExecutable;
 
 /**
@@ -23,12 +24,13 @@ abstract class QueryPluginBase extends PluginBase implements QueryInterface {
   var $pager = NULL;
 
   /**
-   * Constructor; Create the basic query object and fill with default values.
+   * Overrides Drupal\views\Plugin\views\PluginBase::init().
    */
-  public function init($base_table, $base_field, $options) {
+  public function init(ViewExecutable $view, DisplayPluginBase $display, array $options = array()) {
+    $this->view = $view;
+    $this->displayHandler = $display;
+
     $this->setOptionDefaults($this->options, $this->defineOptions());
-    $this->base_table = $base_table;
-    $this->base_field = $base_field;
     $this->unpackOptions($this->options, $options);
   }
 
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 43e52cade84f..103ea43dc4df 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
@@ -8,6 +8,7 @@
 namespace Drupal\views\Plugin\views\query;
 
 use Drupal\Core\Database\Database;
+use Drupal\views\Plugin\views\display\DisplayPluginBase;
 use Drupal\Core\Database\DatabaseExceptionWrapper;
 use Drupal\views\Plugin\views\join\JoinPluginBase;
 use Drupal\views\Plugin\views\HandlerBase;
@@ -113,12 +114,12 @@ class Sql extends QueryPluginBase {
   var $no_distinct;
 
   /**
-   * Constructor; Create the basic query object and fill with default values.
+   * Overrides Drupal\views\Plugin\views\query\QueryPluginBase::init().
    */
-  public function init($base_table = 'node', $base_field = 'nid', $options) {
-    parent::init($base_table, $base_field, $options);
-    $this->base_table = $base_table;  // Predefine these above, for clarity.
-    $this->base_field = $base_field;
+  public function init(ViewExecutable $view, DisplayPluginBase $display, array $options = array()) {
+    parent::init($view, $display, $options);
+    $base_table = $this->view->storage->get('base_table');
+    $base_field = $this->view->storage->get('base_field');
     $this->relationships[$base_table] = array(
       'link' => NULL,
       'table' => $base_table,
@@ -288,7 +289,7 @@ public function submitOptionsForm(&$form, &$form_state) {
    */
   function add_relationship($alias, JoinPluginBase $join, $base, $link_point = NULL) {
     if (empty($link_point)) {
-      $link_point = $this->base_table;
+      $link_point = $this->view->storage->get('base_table');
     }
     elseif (!array_key_exists($link_point, $this->relationships)) {
       return FALSE;
@@ -322,7 +323,7 @@ function add_relationship($alias, JoinPluginBase $join, $base, $link_point = NUL
       'base' => $base,
     );
 
-    $this->tables[$this->base_table][$alias] = array(
+    $this->tables[$this->view->storage->get('base_table')][$alias] = array(
       'count' => 1,
       'alias' => $alias,
     );
@@ -406,7 +407,7 @@ function queue_table($table, $relationship = NULL, JoinPluginBase $join = NULL,
     }
 
     if (empty($relationship)) {
-      $relationship = $this->base_table;
+      $relationship = $this->view->storage->get('base_table');
     }
 
     if (!array_key_exists($relationship, $this->relationships)) {
@@ -414,7 +415,7 @@ function queue_table($table, $relationship = NULL, JoinPluginBase $join = NULL,
     }
 
     if (!$alias && $join && $relationship && !empty($join->adjusted) && $table != $join->table) {
-      if ($relationship == $this->base_table) {
+      if ($relationship == $this->view->storage->get('base_table')) {
         $alias = $table;
       }
       else {
@@ -438,7 +439,7 @@ function queue_table($table, $relationship = NULL, JoinPluginBase $join = NULL,
     // If this is a relationship based table, add a marker with
     // the relationship as a primary table for the alias.
     if ($table != $alias) {
-      $this->mark_table($alias, $this->base_table, $alias);
+      $this->mark_table($alias, $this->view->storage->get('base_table'), $alias);
     }
 
     // If no join is specified, pull it from the table data.
@@ -467,7 +468,7 @@ function mark_table($table, $relationship, $alias) {
     if (empty($this->tables[$relationship][$table])) {
       if (!isset($alias)) {
         $alias = '';
-        if ($relationship != $this->base_table) {
+        if ($relationship != $this->view->storage->get('base_table')) {
           // double underscore will help prevent accidental name
           // space collisions.
           $alias = $relationship . '__';
@@ -507,14 +508,14 @@ function mark_table($table, $relationship, $alias) {
   function ensure_table($table, $relationship = NULL, JoinPluginBase $join = NULL) {
     // ensure a relationship
     if (empty($relationship)) {
-      $relationship = $this->base_table;
+      $relationship = $this->view->storage->get('base_table');
     }
 
     // If the relationship is the primary table, this actually be a relationship
     // link back from an alias. We store all aliases along with the primary table
     // to detect this state, because eventually it'll hit a table we already
     // have and that's when we want to stop.
-    if ($relationship == $this->base_table && !empty($this->tables[$relationship][$table])) {
+    if ($relationship == $this->view->storage->get('base_table') && !empty($this->tables[$relationship][$table])) {
       return $this->tables[$relationship][$table]['alias'];
     }
 
@@ -585,7 +586,7 @@ function ensure_table($table, $relationship = NULL, JoinPluginBase $join = NULL)
    */
   function ensure_path($table, $relationship = NULL, $join = NULL, $traced = array(), $add = array()) {
     if (!isset($relationship)) {
-      $relationship = $this->base_table;
+      $relationship = $this->view->storage->get('base_table');
     }
 
     if (!array_key_exists($relationship, $this->relationships)) {
@@ -645,7 +646,7 @@ function adjust_join($join, $relationship) {
     }
 
     // Adjusts the left table for our relationship.
-    if ($relationship != $this->base_table) {
+    if ($relationship != $this->view->storage->get('base_table')) {
       // If we're linking to the primary table, the relationship to use will
       // be the prior relationship. Unless it's a direct link.
 
@@ -709,8 +710,8 @@ function get_table_info($table) {
     }
 
     // In rare cases we might *only* have aliased versions of the table.
-    if (!empty($this->tables[$this->base_table][$table])) {
-      $alias = $this->tables[$this->base_table][$table]['alias'];
+    if (!empty($this->tables[$this->view->storage->get('base_table')][$table])) {
+      $alias = $this->tables[$this->view->storage->get('base_table')][$table]['alias'];
       if (!empty($this->table_queue[$alias])) {
         return $this->table_queue[$alias];
       }
@@ -741,8 +742,8 @@ function get_table_info($table) {
    */
   function add_field($table, $field, $alias = '', $params = array()) {
     // We check for this specifically because it gets a special alias.
-    if ($table == $this->base_table && $field == $this->base_field && empty($alias)) {
-      $alias = $this->base_field;
+    if ($table == $this->view->storage->get('base_table') && $field == $this->view->storage->get('base_field') && empty($alias)) {
+      $alias = $this->view->storage->get('base_field');
     }
 
     if ($table && empty($this->table_queue[$table])) {
@@ -1215,10 +1216,10 @@ function compile_fields($query) {
         $query->addExpression($string, $fieldname, $placeholders);
       }
       elseif ($this->distinct && !in_array($fieldname, $this->groupby)) {
-        $query->addField(!empty($field['table']) ? $field['table'] : $this->base_table, $field['field'], $fieldname);
+        $query->addField(!empty($field['table']) ? $field['table'] : $this->view->storage->get('base_table'), $field['field'], $fieldname);
       }
       elseif (empty($field['aggregate'])) {
-        $query->addField(!empty($field['table']) ? $field['table'] : $this->base_table, $field['field'], $fieldname);
+        $query->addField(!empty($field['table']) ? $field['table'] : $this->view->storage->get('base_table'), $field['field'], $fieldname);
       }
 
       if ($this->get_count_optimized) {
@@ -1238,7 +1239,7 @@ function compile_fields($query) {
   public function query($get_count = FALSE) {
     // Check query distinct value.
     if (empty($this->no_distinct) && $this->distinct && !empty($this->fields)) {
-      $base_field_alias = $this->add_field($this->base_table, $this->base_field);
+      $base_field_alias = $this->add_field($this->view->storage->get('base_table'), $this->view->storage->get('base_field'));
       $this->add_groupby($base_field_alias);
       $distinct = TRUE;
     }
@@ -1278,7 +1279,7 @@ public function query($get_count = FALSE) {
     // 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->base_table, $this->base_table, $options)
+      ->select($this->view->storage->get('base_table'), $this->view->storage->get('base_table'), $options)
       ->addTag('views')
       ->addTag('views_' . $this->view->storage->get('name'));
 
@@ -1441,7 +1442,7 @@ function execute(ViewExecutable $view) {
     $count_query->addMetaData('view', $view);
 
     if (empty($this->options['disable_sql_rewrite'])) {
-      $base_table_data = views_fetch_data($this->base_table);
+      $base_table_data = views_fetch_data($this->view->storage->get('base_table'));
       if (isset($base_table_data['table']['base']['access query tag'])) {
         $access_tag = $base_table_data['table']['base']['access query tag'];
         $query->addTag($access_tag);
@@ -1539,10 +1540,10 @@ function execute(ViewExecutable $view) {
   function get_entity_tables() {
     // Start with the base table.
     $entity_tables = array();
-    $base_table_data = views_fetch_data($this->base_table);
+    $base_table_data = views_fetch_data($this->view->storage->get('base_table'));
     if (isset($base_table_data['table']['entity type'])) {
-      $entity_tables[$this->base_table] = array(
-        'base' => $this->base_table,
+      $entity_tables[$this->view->storage->get('base_table')] = array(
+        'base' => $this->view->storage->get('base_table'),
         'relationship_id' => 'none',
         'entity_type' => $base_table_data['table']['entity type'],
         'revision' => FALSE,
diff --git a/core/modules/views/lib/Drupal/views/ViewExecutable.php b/core/modules/views/lib/Drupal/views/ViewExecutable.php
index 4e59f7ffefa6..4ad1633f8bdf 100644
--- a/core/modules/views/lib/Drupal/views/ViewExecutable.php
+++ b/core/modules/views/lib/Drupal/views/ViewExecutable.php
@@ -945,18 +945,7 @@ public function initQuery() {
       $this->base_database = $views_data['table']['base']['database'];
     }
 
-    // Load the options.
-    $query_options = $this->display_handler->getOption('query');
-
-    // Create and initialize the query object.
-    $plugin = !empty($views_data['table']['base']['query_id']) ? $views_data['table']['base']['query_id'] : 'views_query';
-    $this->query = views_get_plugin('query', $plugin);
-
-    if (empty($this->query)) {
-      return FALSE;
-    }
-
-    $this->query->init($this->storage->get('base_table'), $this->storage->get('base_field'), $query_options['options']);
+    $this->query = $this->display_handler->getPlugin('query');
     return TRUE;
   }
 
-- 
GitLab