From 6a9ce89a6577ac08f833fc616b0ad8d7a4738da6 Mon Sep 17 00:00:00 2001
From: Daniel Wehner <daniel.wehner@erdfisch.de>
Date: Sun, 19 Aug 2012 15:19:00 +0200
Subject: [PATCH] Issue #1706258 by dawehner, damiankloip: Convert join class
 to plugins.

---
 includes/handlers.inc                         |  9 ++++---
 .../views/join/JoinPluginBase.php}            |  6 ++---
 .../views/Plugin/views/join/Standard.php      | 21 +++++++++++++++
 .../views/join/Subquery.php}                  | 13 ++++++----
 lib/Drupal/views/Plugin/views/query/Sql.php   | 20 +++++++-------
 .../views/relationship/GroupwiseMax.php       |  8 +++---
 .../relationship/RelationshipPluginBase.php   |  7 ++---
 .../views/Plugin/views/sort/MenuHierarchy.php |  3 +--
 .../Plugin/views/field/NcsLastCommentName.php |  3 +--
 .../Plugin/views/sort/NcsLastCommentName.php  |  2 +-
 .../views/relationship/EntityReverse.php      | 16 +++++++-----
 .../search/Plugin/views/argument/Search.php   |  3 +--
 .../search/Plugin/views/filter/Search.php     |  3 +--
 .../views/relationship/NodeTermData.php       |  3 +--
 .../Plugin/views/relationship/Translation.php |  8 +++---
 modules/views.views.inc                       | 26 +++++++++----------
 views.module                                  | 13 ++++++++++
 17 files changed, 100 insertions(+), 64 deletions(-)
 rename lib/Drupal/views/{Join.php => Plugin/views/join/JoinPluginBase.php} (98%)
 create mode 100644 lib/Drupal/views/Plugin/views/join/Standard.php
 rename lib/Drupal/views/{JoinSubquery.php => Plugin/views/join/Subquery.php} (93%)

diff --git a/includes/handlers.inc b/includes/handlers.inc
index 300fa9c33c45..21ff1c142082 100644
--- a/includes/handlers.inc
+++ b/includes/handlers.inc
@@ -7,7 +7,7 @@
 
 use Drupal\Core\Database\Database;
 use Drupal\views\View;
-use Drupal\views\Join;
+use Drupal\views\Plugin\views\join\JoinPluginBase;
 use Drupal\views\Plugin\Type\ViewsPluginManager;
 use Drupal\Component\Plugin\Exception\PluginException;
 
@@ -80,12 +80,13 @@ function views_get_table_join($table, $base_table) {
   $data = views_fetch_data($table);
   if (isset($data['table']['join'][$base_table])) {
     $h = $data['table']['join'][$base_table];
-    if (!empty($h['handler']) && class_exists($h['handler'])) {
-      $handler = new $h['handler'];
+    if (!empty($h['join_id']) && class_exists($h['handler'])) {
+      $id = $h['join_id'];
     }
     else {
-      $handler = new Join();
+      $id = 'standard';
     }
+    $handler = views_get_join($id);
 
     // Fill in some easy defaults
     $handler->definition = $h;
diff --git a/lib/Drupal/views/Join.php b/lib/Drupal/views/Plugin/views/join/JoinPluginBase.php
similarity index 98%
rename from lib/Drupal/views/Join.php
rename to lib/Drupal/views/Plugin/views/join/JoinPluginBase.php
index 4adb0210a09e..8e9fe6dbf394 100644
--- a/lib/Drupal/views/Join.php
+++ b/lib/Drupal/views/Plugin/views/join/JoinPluginBase.php
@@ -2,10 +2,10 @@
 
 /**
  * @file
- * Definition of Drupal\views\Join
+ * Definition of Drupal\views\Plugin\views\join\JoinPluginBase.
  */
 
-namespace Drupal\views;
+namespace Drupal\views\Plugin\views\join;
 
 /**
  * @defgroup views_join_handlers Views join handlers
@@ -59,7 +59,7 @@
  *   - - numeric: If true, the value will not be surrounded in quotes.
  *   - - extra type: How all the extras will be combined. Either AND or OR. Defaults to AND.
  */
-class Join {
+class JoinPluginBase {
 
   var $table = NULL;
 
diff --git a/lib/Drupal/views/Plugin/views/join/Standard.php b/lib/Drupal/views/Plugin/views/join/Standard.php
new file mode 100644
index 000000000000..ba54cc445794
--- /dev/null
+++ b/lib/Drupal/views/Plugin/views/join/Standard.php
@@ -0,0 +1,21 @@
+<?php
+
+/**
+ * @file
+ * Definition of Drupal\views\Plugin\views\join\Standard.
+ */
+
+namespace Drupal\views\Plugin\views\join;
+
+use Drupal\Core\Annotation\Plugin;
+
+/**
+ * Default implementation of the join plugin.
+ *
+ * @Plugin(
+ *   id = "standard"
+ * )
+ */
+class Standard extends JoinPluginBase {
+
+}
diff --git a/lib/Drupal/views/JoinSubquery.php b/lib/Drupal/views/Plugin/views/join/Subquery.php
similarity index 93%
rename from lib/Drupal/views/JoinSubquery.php
rename to lib/Drupal/views/Plugin/views/join/Subquery.php
index dcc4b90b3c35..b2867e72e62a 100644
--- a/lib/Drupal/views/JoinSubquery.php
+++ b/lib/Drupal/views/Plugin/views/join/Subquery.php
@@ -2,12 +2,11 @@
 
 /**
  * @file
- * Definition of Drupal\views\JoinSubquery.
+ * Definition of Drupal\views\Plugin\views\join\Subquery.
  */
 
-namespace Drupal\views;
-
-use Drupal\views\Join;
+namespace Drupal\views\Plugin\views\join;
+use Drupal\Core\Annotation\Plugin;
 
 /**
  * Join handler for relationships that join with a subquery as the left field.
@@ -17,8 +16,12 @@
  * join definition
  *   same as Join class above, except:
  *   - left_query: The subquery to use in the left side of the join clause.
+ *
+ * @Plugin(
+ *   id = "subquery"
+ * )
  */
-class JoinSubquery extends Join {
+class Subquery extends JoinPluginBase {
 
   function construct($table = NULL, $left_table = NULL, $left_field = NULL, $field = NULL, $extra = array(), $type = 'LEFT') {
     parent::construct($table, $left_table, $left_field, $field, $extra, $type);
diff --git a/lib/Drupal/views/Plugin/views/query/Sql.php b/lib/Drupal/views/Plugin/views/query/Sql.php
index 1a5e3cc9271a..6470abe38bf4 100644
--- a/lib/Drupal/views/Plugin/views/query/Sql.php
+++ b/lib/Drupal/views/Plugin/views/query/Sql.php
@@ -8,7 +8,7 @@
 namespace Drupal\views\Plugin\views\query;
 
 use Drupal\Core\Database\Database;
-use Drupal\views\Join;
+use Drupal\views\Plugin\views\join\JoinPluginBase;
 use Exception;
 use Drupal\Core\Annotation\Plugin;
 use Drupal\Core\Annotation\Translation;
@@ -326,7 +326,7 @@ function options_submit(&$form, &$form_state) {
    * @param $alias
    *   What this relationship will be called, and is also the alias
    *   for the table.
-   * @param Drupal\views\Join $join
+   * @param Drupal\views\Plugin\views\join\JoinPluginBase $join
    *   A Join object (or derived object) to join the alias in.
    * @param $base
    *   The name of the 'base' table this relationship represents; this
@@ -338,7 +338,7 @@ function options_submit(&$form, &$form_state) {
    *   might have a relationship to an 'album' node, which might
    *   have a relationship to an 'artist' node.
    */
-  function add_relationship($alias, Join $join, $base, $link_point = NULL) {
+  function add_relationship($alias, JoinPluginBase $join, $base, $link_point = NULL) {
     if (empty($link_point)) {
       $link_point = $this->base_table;
     }
@@ -398,7 +398,7 @@ function add_relationship($alias, Join $join, $base, $link_point = NULL) {
    *   tables exist and are properly aliased. If set to NULL the path to
    *   the primary table will be ensured. If the path cannot be made, the
    *   table will NOT be added.
-   * @param Drupal\views\Join $join
+   * @param Drupal\views\Plugin\views\join\JoinPluginBase $join
    *   In some join configurations this table may actually join back through
    *   a different method; this is most likely to be used when tracing
    *   a hierarchy path. (node->parent->parent2->parent3). This parameter
@@ -412,7 +412,7 @@ function add_relationship($alias, Join $join, $base, $link_point = NULL) {
    *   adding parts to the query. Or FALSE if the table was not able to be
    *   added.
    */
-  function add_table($table, $relationship = NULL, Join $join = NULL, $alias = NULL) {
+  function add_table($table, $relationship = NULL, JoinPluginBase $join = NULL, $alias = NULL) {
     if (!$this->ensure_path($table, $relationship, $join)) {
       return FALSE;
     }
@@ -437,7 +437,7 @@ function add_table($table, $relationship = NULL, Join $join = NULL, $alias = NUL
    * @param $relationship
    *   The primary table alias this table is related to. If not set, the
    *   primary table will be used.
-   * @param Drupal\views\Join $join
+   * @param Drupal\views\Plugin\views\join\JoinPluginBase $join
    *   In some join configurations this table may actually join back through
    *   a different method; this is most likely to be used when tracing
    *   a hierarchy path. (node->parent->parent2->parent3). This parameter
@@ -451,7 +451,7 @@ function add_table($table, $relationship = NULL, Join $join = NULL, $alias = NUL
    *   adding parts to the query. Or FALSE if the table was not able to be
    *   added.
    */
-  function queue_table($table, $relationship = NULL, Join $join = NULL, $alias = NULL) {
+  function queue_table($table, $relationship = NULL, JoinPluginBase $join = NULL, $alias = NULL) {
     // If the alias is set, make sure it doesn't already exist.
     if (isset($this->table_queue[$alias])) {
       return $alias;
@@ -549,14 +549,14 @@ function mark_table($table, $relationship, $alias) {
    *   The relationship to ensure the table links to. Each relationship will
    *   get a unique instance of the table being added. If not specified,
    *   will be the primary table.
-   * @param Drupal\views\Join $join
+   * @param Drupal\views\Plugin\views\join\JoinPluginBase $join
    *   A Join object (or derived object) to join the alias in.
    *
    * @return
    *   The alias used to refer to this specific table, or NULL if the table
    *   cannot be ensured.
    */
-  function ensure_table($table, $relationship = NULL, Join $join = NULL) {
+  function ensure_table($table, $relationship = NULL, JoinPluginBase $join = NULL) {
     // ensure a relationship
     if (empty($relationship)) {
       $relationship = $this->base_table;
@@ -737,7 +737,7 @@ function adjust_join($join, $relationship) {
    * @param $base_table
    *   The path we're following to get this join.
    *
-   * @return Drupal\views\Join
+   * @return Drupal\views\Plugin\views\join\JoinPluginBase
    *   A Join object or child object, if one exists.
    */
   function get_join_data($table, $base_table) {
diff --git a/lib/Drupal/views/Plugin/views/relationship/GroupwiseMax.php b/lib/Drupal/views/Plugin/views/relationship/GroupwiseMax.php
index 78ba5e0ece1a..47901a01f287 100644
--- a/lib/Drupal/views/Plugin/views/relationship/GroupwiseMax.php
+++ b/lib/Drupal/views/Plugin/views/relationship/GroupwiseMax.php
@@ -9,7 +9,6 @@
 
 use Drupal\Core\Database\Query\AlterableInterface;
 use Drupal\views\View;
-use Drupal\views\JoinSubquery;
 use Drupal\Core\Annotation\Plugin;
 
 /**
@@ -373,12 +372,13 @@ function query() {
       }
     }
 
-    if (!empty($def['join_handler']) && class_exists($def['join_handler'])) {
-      $join = new $def['join_handler'];
+    if (!empty($def['join_id'])) {
+      $id = $def['join_id'];
     }
     else {
-      $join = new JoinSubquery();
+      $id = 'subquery';
     }
+    $join = views_get_join($id);
 
     $join->definition = $def;
     $join->construct();
diff --git a/lib/Drupal/views/Plugin/views/relationship/RelationshipPluginBase.php b/lib/Drupal/views/Plugin/views/relationship/RelationshipPluginBase.php
index deef3f434a11..6753be3dd681 100644
--- a/lib/Drupal/views/Plugin/views/relationship/RelationshipPluginBase.php
+++ b/lib/Drupal/views/Plugin/views/relationship/RelationshipPluginBase.php
@@ -132,12 +132,13 @@ function query() {
       $def['extra'] = $this->definition['extra'];
     }
 
-    if (!empty($def['join_handler']) && class_exists($def['join_handler'])) {
-      $join = new $def['join_handler'];
+    if (!empty($def['join_id'])) {
+      $id = $def['join_id'];
     }
     else {
-      $join = new Join();
+      $id = 'standard';
     }
+    $join = views_get_join($id);
 
     $join->definition = $def;
     $join->options = $this->options;
diff --git a/lib/Drupal/views/Plugin/views/sort/MenuHierarchy.php b/lib/Drupal/views/Plugin/views/sort/MenuHierarchy.php
index 45922081f14d..9bd46b55e183 100644
--- a/lib/Drupal/views/Plugin/views/sort/MenuHierarchy.php
+++ b/lib/Drupal/views/Plugin/views/sort/MenuHierarchy.php
@@ -7,7 +7,6 @@
 
 namespace Drupal\views\Plugin\views\sort;
 
-use Drupal\views\Join;
 use Drupal\Core\Annotation\Plugin;
 
 
@@ -47,7 +46,7 @@ function query() {
     $max_depth = isset($this->definition['max depth']) ? $this->definition['max depth'] : MENU_MAX_DEPTH;
     for ($i = 1; $i <= $max_depth; ++$i) {
       if ($this->options['sort_within_level']) {
-        $join = new Join();
+        $join = views_get_join();
         $join->construct('menu_links', $this->table_alias, $this->field . $i, 'mlid');
         $menu_links = $this->query->add_table('menu_links', NULL, $join);
         $this->query->add_orderby($menu_links, 'weight', $this->options['order']);
diff --git a/lib/Views/comment/Plugin/views/field/NcsLastCommentName.php b/lib/Views/comment/Plugin/views/field/NcsLastCommentName.php
index e58ddb529890..461bd2b719a7 100644
--- a/lib/Views/comment/Plugin/views/field/NcsLastCommentName.php
+++ b/lib/Views/comment/Plugin/views/field/NcsLastCommentName.php
@@ -7,7 +7,6 @@
 
 namespace Views\comment\Plugin\views\field;
 
-use Drupal\views\Join;
 use Drupal\views\Plugin\views\field\FieldPluginBase;
 use Drupal\Core\Annotation\Plugin;
 
@@ -28,7 +27,7 @@ function query() {
     // have to join in a specially related user table.
     $this->ensure_my_table();
     // join 'users' to this table via vid
-    $join = new Join();
+    $join = views_get_join();
     $join->construct('users', $this->table_alias, 'last_comment_uid', 'uid');
     $join->extra = array(array('field' => 'uid', 'operator' => '!=', 'value' => '0'));
 
diff --git a/lib/Views/comment/Plugin/views/sort/NcsLastCommentName.php b/lib/Views/comment/Plugin/views/sort/NcsLastCommentName.php
index 2cc5a4049154..3ca454ffb8a8 100644
--- a/lib/Views/comment/Plugin/views/sort/NcsLastCommentName.php
+++ b/lib/Views/comment/Plugin/views/sort/NcsLastCommentName.php
@@ -7,7 +7,6 @@
 
 namespace Views\comment\Plugin\views\sort;
 
-use Drupal\views\Join;
 use Drupal\views\Plugin\views\sort\SortPluginBase;
 use Drupal\Core\Annotation\Plugin;
 
@@ -27,6 +26,7 @@ class NcsLastCommentName extends SortPluginBase {
   function query() {
     $this->ensure_my_table();
     $join = new Join();
+    $join = views_get_join();
     $join->construct('users', $this->table_alias, 'last_comment_uid', 'uid');
 
     // @todo this might be safer if we had an ensure_relationship rather than guessing
diff --git a/lib/Views/field/Plugin/views/relationship/EntityReverse.php b/lib/Views/field/Plugin/views/relationship/EntityReverse.php
index 7de431bc4719..a39027cdc065 100644
--- a/lib/Views/field/Plugin/views/relationship/EntityReverse.php
+++ b/lib/Views/field/Plugin/views/relationship/EntityReverse.php
@@ -7,7 +7,6 @@
 
 namespace Views\field\Plugin\views\relationship;
 
-use Drupal\views\Join;
 use Drupal\views\Plugin\views\relationship\RelationshipPluginBase;
 use Drupal\Core\Annotation\Plugin;
 
@@ -53,12 +52,14 @@ function query() {
       $first['extra'] = $this->definition['join_extra'];
     }
 
-    if (!empty($this->definition['join_handler']) && class_exists($this->definition['join_handler'])) {
-      $first_join = new $this->definition['join_handler'];
+    if (!empty($def['join_id'])) {
+      $id = $def['join_id'];
     }
     else {
-      $first_join = new Join();
+      $id = 'standard';
     }
+    $first_join = views_get_join($id);
+
     $first_join->definition = $first;
     $first_join->construct();
     $first_join->adjusted = TRUE;
@@ -78,12 +79,13 @@ function query() {
       $second['type'] = 'INNER';
     }
 
-    if (!empty($this->definition['join_handler']) && class_exists($this->definition['join_handler'])) {
-      $second_join = new $this->definition['join_handler'];
+    if (!empty($def['join_id'])) {
+      $id = $def['join_id'];
     }
     else {
-      $second_join = new Join();
+      $id = 'standard';
     }
+    $second_join = views_get_join($id);
     $second_join->definition = $second;
     $second_join->construct();
     $second_join->adjusted = TRUE;
diff --git a/lib/Views/search/Plugin/views/argument/Search.php b/lib/Views/search/Plugin/views/argument/Search.php
index 5991b5c81eea..d17d72924263 100644
--- a/lib/Views/search/Plugin/views/argument/Search.php
+++ b/lib/Views/search/Plugin/views/argument/Search.php
@@ -7,7 +7,6 @@
 
 namespace Views\search\Plugin\views\argument;
 
-use Drupal\views\Join;
 use Drupal\views\Plugin\views\argument\ArgumentPluginBase;
 use Drupal\Core\Annotation\Plugin;
 
@@ -63,7 +62,7 @@ function query($group_by = FALSE) {
       $search_condition = db_and();
 
       // Create a new join to relate the 'search_total' table to our current 'search_index' table.
-      $join = new Join();
+      $join = views_get_join();
       $join->construct('search_total', $search_index, 'word', 'word');
       $search_total = $this->query->add_relationship('search_total', $join, $search_index);
 
diff --git a/lib/Views/search/Plugin/views/filter/Search.php b/lib/Views/search/Plugin/views/filter/Search.php
index a1ee24ae5afb..f381babcab8d 100644
--- a/lib/Views/search/Plugin/views/filter/Search.php
+++ b/lib/Views/search/Plugin/views/filter/Search.php
@@ -7,7 +7,6 @@
 
 namespace Views\search\Plugin\views\filter;
 
-use Drupal\views\Join;
 use Drupal\search\SearchQuery;
 use Drupal\views\Plugin\views\filter\FilterPluginBase;
 use Drupal\Core\Annotation\Plugin;
@@ -140,7 +139,7 @@ function query() {
       $search_condition = db_and();
 
       // Create a new join to relate the 'serach_total' table to our current 'search_index' table.
-      $join = new Join();
+      $join = views_get_join();
       $join->construct('search_total', $search_index, 'word', 'word');
       $search_total = $this->query->add_relationship('search_total', $join, $search_index);
 
diff --git a/lib/Views/taxonomy/Plugin/views/relationship/NodeTermData.php b/lib/Views/taxonomy/Plugin/views/relationship/NodeTermData.php
index c33d32fdc2ef..76690543c2d1 100644
--- a/lib/Views/taxonomy/Plugin/views/relationship/NodeTermData.php
+++ b/lib/Views/taxonomy/Plugin/views/relationship/NodeTermData.php
@@ -7,7 +7,6 @@
 
 namespace Views\taxonomy\Plugin\views\relationship;
 
-use Drupal\views\Join;
 use Drupal\views\Plugin\views\relationship\RelationshipPluginBase;
 use Drupal\Core\Annotation\Plugin;
 
@@ -93,7 +92,7 @@ function query() {
       $def['table formula'] = $query;
     }
 
-    $join = new Join();
+    $join = views_get_join('standard');
 
     $join->definition = $def;
     $join->construct();
diff --git a/lib/Views/translation/Plugin/views/relationship/Translation.php b/lib/Views/translation/Plugin/views/relationship/Translation.php
index 1af139b6d70a..49311628e9b4 100644
--- a/lib/Views/translation/Plugin/views/relationship/Translation.php
+++ b/lib/Views/translation/Plugin/views/relationship/Translation.php
@@ -7,7 +7,6 @@
 
 namespace Views\translation\Plugin\views\relationship;
 
-use Drupal\views\Join;
 use Drupal\views\Plugin\views\relationship\RelationshipPluginBase;
 use Drupal\Core\Annotation\Plugin;
 
@@ -96,12 +95,13 @@ function query() {
       }
     }
 
-    if (!empty($def['join_handler']) && class_exists($def['join_handler'])) {
-      $join = new $def['join_handler'];
+    if (!empty($def['join_id'])) {
+      $id = $def['join_id'];
     }
     else {
-      $join = new Join();
+      $id = 'standard';
     }
+    $join = views_get_join($id);
 
     $join->definition = $def;
     $join->construct();
diff --git a/modules/views.views.inc b/modules/views.views.inc
index c8f94c23ad4b..5718cb1c7035 100644
--- a/modules/views.views.inc
+++ b/modules/views.views.inc
@@ -10,20 +10,20 @@
 /**
  * Implements hook_views_data().
  */
-    function views_views_data() {
-    $data['views']['table']['group'] = t('Global');
-    $data['views']['table']['join'] = array(
-      // #global is a special flag which let's a table appear all the time.
-    '#global' => array(),
-    );
+function views_views_data() {
+  $data['views']['table']['group'] = t('Global');
+  $data['views']['table']['join'] = array(
+    // #global is a special flag which let's a table appear all the time.
+  '#global' => array(),
+  );
 
-    $data['views']['random'] = array(
-    'title' => t('Random'),
-    'help' => t('Randomize the display order.'),
-    'sort' => array(
-      'id' => 'random',
-      ),
-    );
+  $data['views']['random'] = array(
+  'title' => t('Random'),
+  'help' => t('Randomize the display order.'),
+  'sort' => array(
+    'id' => 'random',
+    ),
+  );
 
   $data['views']['null'] = array(
     'title' => t('Null'),
diff --git a/views.module b/views.module
index e4ef31ce7ab5..6dcc9856f081 100644
--- a/views.module
+++ b/views.module
@@ -1382,6 +1382,19 @@ function views_get_plugin($type, $id, $reset = FALSE) {
   }
 }
 
+/**
+ * Get a join plugin instance.
+ *
+ * @param string $plugin_id
+ *   The name of the join for example standard.
+ *
+ * @return Drupal\views\Plugin\views\join\JoinPluginBase
+ */
+function views_get_join($plugin_id = 'standard') {
+  $manager = new ViewsPluginManager('join');
+  return $manager->createInstance($plugin_id);
+}
+
 /**
  * Load the current enabled localization plugin.
  *
-- 
GitLab