This hooks allow configurable objects to be altered and have additions default values added to the configuration by another module.

This is used by feeds delete to allow altering of the default values to add new parameters to the configurable.

Example of implementation see http://drupal.org/sandbox/gordon/1939256

Comments

twistor’s picture

Component: Documentation » Code
Status: Needs review » Needs work

This is a nice idea. I have tried your approach before, creating a module that works with all plugins, it's not easy.

+++ b/includes/FeedsConfigurable.incundefined
@@ -166,7 +166,10 @@ abstract class FeedsConfigurable {
-    return array();
+    $hook = 'feeds_' . get_class($this) . '_defaults';

This should use the plugin id, not the class. I know they are usually the same, but not always.

gordon’s picture

I have fixed up this, It was not easy since once the plugin is loaded, the name was being lost.

twistor’s picture

+++ b/feeds.moduleundefined
@@ -928,10 +928,12 @@ function feeds_source($importer_id, $feed_nid = 0) {
-function feeds_plugin($plugin, $id) {
+function feeds_plugin($plugin_key, $id) {
   ctools_include('plugins');
-  if ($class = ctools_plugin_load_class('feeds', 'plugins', $plugin, 'handler')) {
-    return FeedsConfigurable::instance($class, $id);
+  if ($class = ctools_plugin_load_class('feeds', 'plugins', $plugin_key, 'handler')) {
+    $plugin = FeedsConfigurable::instance($class, $id);
+    $plugin->plugin_key = $plugin_key;
+    return $plugin;

Errr... No.

+++ b/includes/FeedsConfigurable.incundefined
@@ -166,7 +166,14 @@ abstract class FeedsConfigurable {
-    return array();
+    $defaults = array();
+    if (isset($this->plugin_key)) {
+      $hook = 'feeds_' . $this->plugin_key . '_defaults';
+      $defaults = module_invoke_all($hook, $this);
+    }
+    $defaults = array_merge($defaults, module_invoke_all('feeds_defaults', $this));
+
+    return $defaults;

This needs to be moved to FeedsPlugin. FeedsConfigurables are not necessarily plugins.

So, you can get the plugin_key in a rather convoluted way without touching feeds_plugin(). Either way, that's the wrong place to do it. I will open up a followup issue about adding a pluginKey() method, which I've been meaning to do already. But I want to do a bit more.

Until then, from FeedsPlugin::configDefaults():
feeds_importer($this->id)->config[$this->pluginType()]['plugin_key']

gordon’s picture

This needs to be moved to FeedsPlugin. FeedsConfigurables are not necessarily plugins

This is why the hook_feeds_PLUGINNAME_defaults() now only gets called for plugins and not for the primary classes.

Until then, from FeedsPlugin::configDefaults():
feeds_importer($this->id)->config[$this->pluginType()]['plugin_key']

this only happens in the feed processing, and not in the admin screens. Which is why I changed feeds_plugin() to record the plugin_key.

Any ideas most welcome.

twistor’s picture

I don't understand:

this only happens in the feed processing, and not in the admin screens. Which is why I changed feeds_plugin() to record the plugin_key.

The changes in feeds_plugin() are unnecessary and wrong.

Wouldn't it make more sense to have the hook name be hook_feeds_PLUGIN_TYPE_defaults()?

gordon’s picture

I like the option of implement the hook as plugin type, but I can't find a method of getting the current plugin type for the current plugin.

If you can give me some clues on how to do this.

Thanks

gordon’s picture

Status: Needs work » Needs review
StatusFileSize
new3.4 KB

Worked it out.

twistor’s picture

Issue summary: View changes
Status: Needs review » Needs work
Issue tags: +Needs tests
  1. +++ b/feeds.api.php
    @@ -363,5 +363,24 @@ function my_module_form_callback($mapping, $target, $form, $form_state) {
    +function hook_feeds_defaults(FeedsConfigurable $configurable) {
    +  if (is_a($configurable, 'FeedsProcessor')) {
    +    return array(
    +      'purge_unseen_items' => 0,
    +      'purge_after_period' => NULL,
    +      'purge_delete' => 0,
    

    The hook name is wrong.
    Use instanceof rather than is_a().
    The array being returned should be something more generic.

  2. +++ b/includes/FeedsConfigurable.inc
    @@ -166,7 +166,14 @@ abstract class FeedsConfigurable {
    +    $defaults = array();
    +    if (method_exists($this, 'pluginType')) {
    +      $hook = 'feeds_' . $this->pluginType() . '_defaults';
    +      $defaults = module_invoke_all($hook, $this);
    +    }
    +    $defaults = array_merge($defaults, module_invoke_all('feeds_defaults', $this));
    

    This should be moved to FeedsPlugin.

  3. +++ b/plugins/FeedsCSVParser.inc
    @@ -174,7 +174,7 @@ class FeedsCSVParser extends FeedsParser {
    +    )+parent::configDefaults();
    

    Needs spaces.

This also needs a test to ensure it works.

megachriz’s picture

Title: Add hook hook_feeds_defaults() » Add hook hook_feeds_config_defaults() to allow other modules to add extra configuration
Status: Needs work » Needs review
StatusFileSize
new14.72 KB
new15.45 KB

This is also useful for the Feeds Import Preview module to bundle additional configuration along with the importer. See #2735141-3: Extending to Preview All . .

So I took a step at this and did the following:

  1. Changed the names of the two hooks to hook_feeds_config_defaults() and hook_feeds_PLUGIN_TYPE_config_defaults()
  2. Documented the hooks in feeds.api.php.
  3. Moved the plugin specific hook to FeedsPlugin::configDefaults().
  4. Added automated tests for both hooks.

Still need to check if this implementation helps Feeds Import Preview enough.

mattjones86’s picture

megachriz’s picture

Thanks for the feedback, I make this a target for 7.x-2.0-beta4.

megachriz’s picture

StatusFileSize
new14.73 KB

Reroll.

megachriz’s picture

I'm testing the patch now with Feeds Import Preview module. It appears that the only thing to add is that the hook hook_feeds_config_defaults() may live in modulename.feeds.inc. For the hook hook_feeds_PLUGIN_TYPE_config_defaults() I don't think this is possible, as it is a dynamic hook.

megachriz’s picture

Turns out that hook_feeds_PLUGIN_TYPE_config_defaults() *can* be detected as PLUGIN_TYPE is either 'fetcher', 'parser' or 'processor', so I added these three variations to feeds_hook_info(). Also moved hook implementations for the tests to feeds_tests.feeds.inc.

  • MegaChriz committed 446981e on 7.x-2.x
    Issue #1962006 by MegaChriz, gordon, twistor: Added hook...
megachriz’s picture

Status: Needs review » Fixed
Issue tags: -Needs tests

Committed #14.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.