From be8fab2fe42cbce4ef81cb2bf9419efcc2e5acd7 Mon Sep 17 00:00:00 2001
From: Daniel Wehner <daniel.wehner@erdfisch.de>
Date: Thu, 26 Jul 2012 16:34:20 -0500
Subject: [PATCH] try to make basic plugins possible

---
 includes/admin.inc                            | 11 +++--
 includes/handlers.inc                         | 18 +++++++++
 .../Plugins/Discovery/ViewsDiscovery.php      |  6 ++-
 .../views/Plugins/Type/WizardManager.php      |  5 +--
 .../views/Plugins/views/wizard/WizardBase.php |  2 +-
 views.module                                  | 40 ++++++++++++++++++-
 views_ui.module                               | 26 ++++--------
 7 files changed, 79 insertions(+), 29 deletions(-)

diff --git a/includes/admin.inc b/includes/admin.inc
index 88d79ac227c4..38bb8efe0888 100644
--- a/includes/admin.inc
+++ b/includes/admin.inc
@@ -379,8 +379,11 @@ function views_ui_add_form($form, &$form_state) {
 
   // Build the rest of the form based on the currently selected wizard plugin.
   $wizard_key = $show_form['wizard_key']['#default_value'];
-  $get_instance = $wizard_plugins[$wizard_key]['get_instance'];
-  $wizard_instance = $get_instance($wizard_plugins[$wizard_key]);
+
+  views_include_handlers();
+  $manager = new WizardManager();
+  $info = $manager->getDefinition($wizard_key);
+  $wizard_instance = $manager->createInstance($wizard_key, $info);
   $form = $wizard_instance->build_form($form, $form_state);
 
   $form['save'] = array(
@@ -702,9 +705,9 @@ function views_ui_nojs_submit($form, &$form_state) {
  */
 function views_ui_wizard_form_validate($form, &$form_state) {
   $wizard = views_ui_get_wizard($form_state['values']['show']['wizard_key']);
+  $manager = new WizardManager();
   $form_state['wizard'] = $wizard;
-  $get_instance = $wizard['get_instance'];
-  $form_state['wizard_instance'] = $get_instance($wizard);
+  $form_state['wizard_instance'] = $manager->createInstance($wizard['name']);
   $errors = $form_state['wizard_instance']->validate($form, $form_state);
   foreach ($errors as $name => $message) {
     form_set_error($name, $message);
diff --git a/includes/handlers.inc b/includes/handlers.inc
index 254cf56f5f86..129b83abb5d3 100644
--- a/includes/handlers.inc
+++ b/includes/handlers.inc
@@ -10,6 +10,24 @@
 use Drupal\views\Join;
 use Drupal\views\ViewsObject;
 
+/**
+ * Instantiate and construct a new plugin.
+ *
+ * @todo
+ * Figure out what to keep from _views_create_handler.
+ */
+function _views_create_plugin($type, $plugin_id, $definition) {
+  $manager = views_get_plugin_manager($type);
+  $instance = $manager->createInstance($plugin_id);
+
+  $instance->set_definition($definition);
+
+  // Let the handler have something like a constructor.
+  $instance->construct();
+
+  return $instance;
+}
+
 /**
  * Instantiate and construct a new handler
  */
diff --git a/lib/Drupal/views/Plugins/Discovery/ViewsDiscovery.php b/lib/Drupal/views/Plugins/Discovery/ViewsDiscovery.php
index bee04e955a93..214b41a0bf8d 100644
--- a/lib/Drupal/views/Plugins/Discovery/ViewsDiscovery.php
+++ b/lib/Drupal/views/Plugins/Discovery/ViewsDiscovery.php
@@ -37,7 +37,11 @@ function __construct($hook, $plugin_type) {
    * Implements Drupal\Component\Plugin\Discovery\DicoveryInterface::getDefinitions().
    */
   public function getDefinitions() {
-    $definitions = parent::getDefinitions();
+    views_include('plugins');
+    views_include_handlers();
+
+    $definitions = module_invoke_all($this->hook);
+    drupal_alter($this->hook, $definitions);
     return $definitions[$this->viewsPluginType];
   }
 }
diff --git a/lib/Drupal/views/Plugins/Type/WizardManager.php b/lib/Drupal/views/Plugins/Type/WizardManager.php
index 64aacb2883db..15bed32d2479 100644
--- a/lib/Drupal/views/Plugins/Type/WizardManager.php
+++ b/lib/Drupal/views/Plugins/Type/WizardManager.php
@@ -9,12 +9,11 @@
 
 use Drupal\Component\Plugin\PluginManagerBase;
 use Drupal\Component\Plugin\Factory\DefaultFactory;
-use Drupal\Core\Plugin\Discovery\HookDiscovery;
-use Drupal\Core\Plugin\MapClassLoader;
+use Drupal\views\Plugins\Discovery\WizardDiscovery;
 
 class WizardManager extends PluginManagerBase {
   public function __construct() {
-    $this->discovery = new HookDiscovery('views_wizard');
+    $this->discovery = new WizardDiscovery('views_wizard');
     $this->factory = new DefaultFactory($this->discovery);
   }
 }
diff --git a/lib/Drupal/views/Plugins/views/wizard/WizardBase.php b/lib/Drupal/views/Plugins/views/wizard/WizardBase.php
index 52f5e294a2fa..c7a7eaf86991 100644
--- a/lib/Drupal/views/Plugins/views/wizard/WizardBase.php
+++ b/lib/Drupal/views/Plugins/views/wizard/WizardBase.php
@@ -273,7 +273,7 @@ protected function build_form_style(&$form, &$form_state, $type) {
     $style_form =& $form['displays'][$type]['options']['style'];
     $style = $style_form['style_plugin']['#default_value'];
     // @fixme
-    return;
+
     $style_plugin = views_get_plugin('style', $style);
     if (isset($style_plugin) && $style_plugin->uses_row_plugin()) {
       $options = $this->row_style_options($type);
diff --git a/views.module b/views.module
index f3cd92a9f627..a43d7463dbef 100644
--- a/views.module
+++ b/views.module
@@ -15,6 +15,16 @@
 
 // @todo: Should views_get_plugin_manager be moved to a ceperate file?
 use Drupal\views\Plugins\Type\StylePluginManager;
+use Drupal\views\Plugins\Type\DisplayPluginManager;
+use Drupal\views\Plugins\Type\AccessPluginManager;
+use Drupal\views\Plugins\Type\ArgumentDefaultPluginManager;
+use Drupal\views\Plugins\Type\ArgumentValidatorPluginManager;
+use Drupal\views\Plugins\Type\CachePluginManager;
+use Drupal\views\Plugins\Type\LocalizationPluginManager;
+use Drupal\views\Plugins\Type\PagerPluginManager;
+use Drupal\views\Plugins\Type\QueryPluginManager;
+use Drupal\views\Plugins\Type\RowPluginManager;
+use Drupal\views\Plugins\Type\ExposedFormPluginManager;
 
 /**
  * Advertise the current views api version
@@ -1337,12 +1347,40 @@ function views_fetch_plugin_names($type, $key = NULL, $base = array()) {
  */
 function views_get_plugin($type, $plugin, $reset = FALSE) {
   views_include('handlers');
+
   $definition = views_fetch_plugin_data($type, $plugin, $reset);
   if (!empty($definition)) {
-    return _views_create_handler($definition, $type);
+    return _views_create_plugin($type, $plugin, $definition);
   }
 }
 
+/**
+ * Returns the plugin manager used for a certain plugin type.
+ *
+ * @param string $type
+ *   The plugin type to use.
+ *
+ * @param PluginManagerInterface
+ */
+function views_get_plugin_manager($type) {
+  switch ($type) {
+    case 'style':
+      $manager = new StylePluginManager();
+      break;
+    case 'display':
+      $manager = new DisplayPluginManager();
+      break;
+    case 'localization':
+      $manager = new LocalizationPluginManager();
+      break;
+    case 'row':
+      $manager = new RowPluginManager();
+      break;
+  }
+
+  return $manager;
+}
+
 /**
  * Load the current enabled localization plugin.
  *
diff --git a/views_ui.module b/views_ui.module
index 620a6c79019a..7d8bb45ffc0d 100644
--- a/views_ui.module
+++ b/views_ui.module
@@ -6,6 +6,7 @@
  */
 
 use Drupal\views\View;
+use Drupal\views\Plugins\Type\WizardManager;
 
 /**
  * Implements hook_menu().
@@ -550,7 +551,8 @@ function views_ui_ctools_plugin_directory($module, $plugin) {
  */
 function views_ui_get_wizard($wizard_type) {
   ctools_include('plugins');
-  $wizard = ctools_get_plugins('views_ui', 'views_wizard', $wizard_type);
+  $manager = new WizardManager();
+  $wizard = $manager->getDefinition($wizard_type);
   // @todo - handle this via an alter hook instead.
   if (!$wizard) {
     // Must be a base table using the default wizard plugin.
@@ -576,7 +578,7 @@ function views_ui_get_wizard($wizard_type) {
  */
 function views_ui_get_wizards() {
   ctools_include('plugins');
-  $wizard_plugins = ctools_get_plugins('views_ui', 'views_wizard');
+  $wizard_plugins = module_invoke_all('views_wizard');
   $wizard_tables = array();
   foreach ($wizard_plugins as $name => $info) {
     $wizard_tables[$info['base_table']] = TRUE;
@@ -611,13 +613,6 @@ function views_ui_views_wizard_defaults() {
     // so they are documented.
     'title' => '',
     'base_table' => NULL,
-    // This is a callback that takes the wizard as argument and returns
-    // an instantiazed Views UI form wizard object.
-    'get_instance' => 'views_ui_get_form_wizard_instance',
-    'form_wizard_class' => array(
-      'file' => 'views_ui_base_views_wizard',
-      'class' => 'ViewsUiBaseViewsWizard',
-    ),
   );
 }
 
@@ -628,22 +623,15 @@ function views_ui_ctools_plugin_type() {
   return array(
     'views_wizard' => array(
       'child plugins' => TRUE,
-      'classes' => array(
-        'form_wizard_class',
-      ),
       'defaults' => views_ui_views_wizard_defaults(),
     ),
   );
 }
 
 function views_ui_get_form_wizard_instance($wizard) {
-  if (isset($wizard['form_wizard_class']['class'])) {
-    $class = $wizard['form_wizard_class']['class'];
-    return new $class($wizard);
-  }
-  else {
-    return new ViewsUiBaseViewsWizard($wizard);
-  }
+  $manager = new WizardManager();
+  $instance = $manager->createInstance($wizard['name']);
+  return $instance;
 }
 
 /**
-- 
GitLab