From ede61cb82d1f9ac7b85ad2785d4bd0e8b201b715 Mon Sep 17 00:00:00 2001
From: twistor <twistor@473738.no-reply.drupal.org>
Date: Sat, 30 May 2015 00:34:07 -0700
Subject: [PATCH] Issue #2497507 by twistor: Pass plugin definition to
 FeedsPlugin objects

---
 feeds.module            | 17 ++++++++--------
 plugins/FeedsPlugin.inc | 45 ++++++++++++++++++++++++++++++++++++++---
 2 files changed, 51 insertions(+), 11 deletions(-)

diff --git a/feeds.module b/feeds.module
index 9d9f6ed6..e74582e1 100644
--- a/feeds.module
+++ b/feeds.module
@@ -1011,22 +1011,21 @@ function feeds_source($importer_id, $feed_nid = 0) {
 /**
  * Gets an instance of a class for a given plugin and id.
  *
- * @param $plugin
+ * @param string $plugin
  *   A string that is the key of the plugin to load.
- * @param $id
+ * @param string $id
  *   A string that is the id of the object.
  *
- * @return
+ * @return FeedsPlugin
  *   A FeedsPlugin object.
- *
- * @throws Exception
- *   If plugin can't be instantiated.
  */
 function feeds_plugin($plugin, $id) {
   ctools_include('plugins');
+
   if ($class = ctools_plugin_load_class('feeds', 'plugins', $plugin, 'handler')) {
-    return FeedsConfigurable::instance($class, $id);
+    return FeedsPlugin::instance($class, $id, ctools_get_plugins('feeds', 'plugins', $plugin));
   }
+
   $args = array('%plugin' => $plugin, '@id' => $id);
   if (user_access('administer feeds')) {
     $args['@link'] = url('admin/structure/feeds/' . $id);
@@ -1035,8 +1034,10 @@ function feeds_plugin($plugin, $id) {
   else {
     drupal_set_message(t('Missing Feeds plugin %plugin. Please contact your site administrator.', $args), 'warning', FALSE);
   }
+
   $class = ctools_plugin_load_class('feeds', 'plugins', 'FeedsMissingPlugin', 'handler');
-  return FeedsConfigurable::instance($class, $id);
+
+  return FeedsPlugin::instance($class, $id);
 }
 
 /**
diff --git a/plugins/FeedsPlugin.inc b/plugins/FeedsPlugin.inc
index cfe3ecfd..ec489a89 100644
--- a/plugins/FeedsPlugin.inc
+++ b/plugins/FeedsPlugin.inc
@@ -19,6 +19,47 @@ class FeedsResult {}
  */
 abstract class FeedsPlugin extends FeedsConfigurable implements FeedsSourceInterface {
 
+  /**
+   * The plugin definition.
+   *
+   * @var array
+   */
+  protected $pluginDefinition;
+
+  /**
+   * Constructs a FeedsPlugin object.
+   *
+   * @param string $id
+   *   The plugin id.
+   * @param array $plugin_definition
+   *   The plugin definition.
+   */
+  protected function __construct($id, array $plugin_definition) {
+    $this->pluginDefinition = $plugin_definition;
+    parent::__construct($id);
+  }
+
+  /**
+   * Instantiates a FeedsPlugin object.
+   *
+   * Don't use directly, use feeds_plugin() instead.
+   *
+   * @see feeds_plugin()
+   */
+  public static function instance($class, $id, array $plugin_definition = array()) {
+    if (!strlen($id)) {
+      throw new InvalidArgumentException(t('Empty configuration identifier.'));
+    }
+
+    static $instances = array();
+
+    if (!isset($instances[$class][$id])) {
+      $instances[$class][$id] = new $class($id, $plugin_definition);
+    }
+
+    return $instances[$class][$id];
+  }
+
   /**
    * Returns the type of plugin.
    *
@@ -36,9 +77,7 @@ abstract class FeedsPlugin extends FeedsConfigurable implements FeedsSourceInter
    * @see ctools_get_plugins()
    */
   public function pluginDefinition() {
-    $importer = feeds_importer($this->id);
-    $plugin_key = $importer->config[$this->pluginType()]['plugin_key'];
-    return ctools_get_plugins('feeds', 'plugins', $plugin_key);
+    return $this->pluginDefinition;
   }
 
   /**
-- 
GitLab