From 541dd42e0399be250e0dbfdb1fdf571a06d88029 Mon Sep 17 00:00:00 2001
From: "tim.plunkett" <tim.plunkett@241634.no-reply.drupal.org>
Date: Thu, 13 Sep 2012 00:24:14 +0200
Subject: [PATCH] Issue #1727266 by tim.plunkett, aspilicious, damiankloip:
 Lazy load plugin managers with dependency injection.

---
 lib/Drupal/views/Plugin/views/PluginBase.php  | 30 +++++++++++++++++++
 lib/Drupal/views/Tests/PluginInstanceTest.php |  4 +--
 lib/Drupal/views/ViewsBundle.php              | 29 ++++++++++++++++++
 views.module                                  | 16 +++++-----
 4 files changed, 68 insertions(+), 11 deletions(-)
 create mode 100644 lib/Drupal/views/ViewsBundle.php

diff --git a/lib/Drupal/views/Plugin/views/PluginBase.php b/lib/Drupal/views/Plugin/views/PluginBase.php
index ba5640976852..f236b497f2a2 100644
--- a/lib/Drupal/views/Plugin/views/PluginBase.php
+++ b/lib/Drupal/views/Plugin/views/PluginBase.php
@@ -51,6 +51,36 @@ public function __construct(array $configuration, $plugin_id, DiscoveryInterface
     $this->construct();
   }
 
+  /**
+   * Returns the valid types of plugins that can be used.
+   *
+   * @return array
+   *   An array of plugin type strings.
+   */
+  public static function getTypes() {
+    return array(
+      'access',
+      'area',
+      'argument',
+      'argument_default',
+      'argument_validator',
+      'cache',
+      'display_extender',
+      'display',
+      'exposed_form',
+      'field',
+      'filter',
+      'join',
+      'pager',
+      'query',
+      'relationship',
+      'row',
+      'sort',
+      'style',
+      'wizard',
+    );
+  }
+
   /**
    * Information about options for all kinds of purposes will be held here.
    * @code
diff --git a/lib/Drupal/views/Tests/PluginInstanceTest.php b/lib/Drupal/views/Tests/PluginInstanceTest.php
index 7ee50d24c642..1afd0faf6410 100644
--- a/lib/Drupal/views/Tests/PluginInstanceTest.php
+++ b/lib/Drupal/views/Tests/PluginInstanceTest.php
@@ -8,7 +8,6 @@
 namespace Drupal\views\Tests;
 
 use ReflectionClass;
-use Drupal\views\Plugin\Type\ViewsPluginManager;
 
 /**
  * Checks general plugin data and instances for all plugin types.
@@ -88,9 +87,10 @@ public function testPluginData() {
    * This will iterate through all plugins from _views_fetch_plugin_data().
    */
   public function testPluginInstances() {
+    $container = drupal_container();
     foreach ($this->definitions as $type => $plugins) {
       // Get a plugin manager for this type.
-      $manager = new ViewsPluginManager($type);
+      $manager = $container->get("plugin.manager.views.$type");
       foreach ($plugins as $definition) {
         // Get a reflection class for this plugin.
         // We only want to test true plugins, i.e. They extend PluginBase.
diff --git a/lib/Drupal/views/ViewsBundle.php b/lib/Drupal/views/ViewsBundle.php
new file mode 100644
index 000000000000..6342662fcf23
--- /dev/null
+++ b/lib/Drupal/views/ViewsBundle.php
@@ -0,0 +1,29 @@
+<?php
+
+/**
+ * @file
+ * Definition of Drupal\views\ViewsBundle.
+ */
+
+namespace Drupal\views;
+
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+use Symfony\Component\HttpKernel\Bundle\Bundle;
+use Drupal\views\Plugin\views\PluginBase;
+
+/**
+ * Views dependency injection container.
+ */
+class ViewsBundle extends Bundle {
+
+  /**
+   * Overrides Symfony\Component\HttpKernel\Bundle\Bundle::build().
+   */
+  public function build(ContainerBuilder $container) {
+    foreach (PluginBase::getTypes() as $type) {
+      $container->register("plugin.manager.views.$type", 'Drupal\views\Plugin\Type\ViewsPluginManager')
+        ->addArgument($type);
+    }
+  }
+
+}
diff --git a/views.module b/views.module
index d5c69cd8f23b..ace1cf1de836 100644
--- a/views.module
+++ b/views.module
@@ -12,7 +12,7 @@
 use Drupal\Core\Database\Query\AlterableInterface;
 use Drupal\views\TempStore\UserTempStore;
 use Drupal\views\View;
-use Drupal\views\Plugin\Type\ViewsPluginManager;
+use Drupal\views\Plugin\views\PluginBase;
 use Drupal\Component\Plugin\Exception\PluginException;
 
 /**
@@ -1324,7 +1324,7 @@ function views_get_handler($table, $field, $type, $override = NULL) {
     }
 
     // @todo This is crazy. Find a way to remove the override functionality.
-    $manager = new ViewsPluginManager($type);
+    $manager = drupal_container()->get("plugin.manager.views.$type");
     $plugin_id = !empty($definition['override handler']) ? $definition['override handler'] : $definition['id'];
     // Try to use the overridden handler.
     try {
@@ -1412,8 +1412,7 @@ function views_fetch_plugin_names($type, $key = NULL, $base = array()) {
  *   The created plugin instance.
  */
 function views_get_plugin($type, $plugin_id) {
-  $manager = new ViewsPluginManager($type);
-  return $manager->createInstance($plugin_id);
+  return drupal_container()->get("plugin.manager.views.$type")->createInstance($plugin_id);
 }
 
 /**
@@ -1428,10 +1427,10 @@ function views_get_plugin($type, $plugin_id) {
  */
 function views_get_plugin_definitions($type = FALSE) {
   $plugins = array();
-  $plugin_types = $type ? array($type) : array('access', 'area', 'argument', 'argument_default', 'argument_validator', 'cache', 'display_extender', 'display', 'exposed_form', 'field', 'filter', 'join', 'pager', 'query', 'relationship', 'row', 'sort', 'style', 'wizard');
+  $plugin_types = $type ? array($type) : PluginBase::getTypes();
+  $container = drupal_container();
   foreach ($plugin_types as $plugin_type) {
-    $manager = new ViewsPluginManager($plugin_type);
-    $plugins[$plugin_type] = $manager->getDefinitions();
+    $plugins[$plugin_type] = $container->get("plugin.manager.views.$plugin_type")->getDefinitions();
   }
   if ($type) {
     return $plugins[$type];
@@ -1451,8 +1450,7 @@ function views_get_plugin_definitions($type = FALSE) {
  *   A plugin definition.
  */
 function views_get_plugin_definition($type, $plugin_id) {
-  $manager = new ViewsPluginManager($type);
-  return $manager->getDefinition($plugin_id);
+  return drupal_container()->get("plugin.manager.views.$type")->getDefinition($plugin_id);
 }
 
 /**
-- 
GitLab