Adding a new plugin is easy.

Simply add the following to your module file where plugins is the directory you want to store your plugins in.

<?php
/**
 * Implements hook_ctools_plugin_directory().
 */
function MODULE_ctools_plugin_directory($owner, $plugin_type){
  if ($owner == 'feeds_tamper' && $plugin_type == 'plugins') {
    return 'plugins';
  }
}
?>

Then, add a file to the plugins directory named my_plugin.inc.

<?php

/**
 * @file
 * Do super awesome thing.
 */

$plugin = array(
  'form' => 'MODULE_MY_PLUGIN_form',
  // Optional validation callback.
  'validate' => 'MODULE_MY_PLUGIN_validate',
  'callback' => 'MODULE_MY_PLUGIN_callback',
  'name' => 'My plugin',
  'multi' => 'loop',
  'category' => 'Other',
);

function MODULE_MY_PLUGIN_form($importer, $element_key, $settings) {
  $form = array();
  $form['help']['#value'] = t('My plugin can do awesome things.');
  //
  // Other formy stuff here.
  //
  return $form;
}

function MODULE_MY_PLUGIN_validate(&$settings) {
  // Validate $settings.
}

function MODULE_MY_PLUGIN_callback($feeds_parser_result, $item_key, $element_key, &$field, $settings, $feeds_source) {
  $field = crazy_modification($field);
}
?>

$plugin keys

  • 'multi' => can be either 'direct' or 'loop'. This specifies how to handle multiple valued fields. 'direct' will pass the whole array into the callback, whereas 'loop' will loop over the data, passing each value individually.
  • 'single' => 'skip' can be set if your plugin only wants to handle multiple-valued fields directly.

Validate

The validate callback is optional and can be used to set form errors on invalid input. There is, however, a more important task which the validate callback can be used for. It can be used to pre-compute values for the callback so that the callback has to do as little work as possible. See keyword_filter.inc for an example of this.

Callback

The full Feeds item can be accessed through $feeds_parser_result->items[$item_key]. With $feeds_parser_result->items[$item_key][$element_key] being the value of $field. This is provided to allow modifying/ removing the Feeds item as a whole. See keyword_filter.inc for an example.

The Feeds source ($feeds_source) can be used to access useful information, such as mapping settings, the raw feed, importer properties, etc.

Comments

inversed’s picture

For this to work for me, I had to change the variable used by ctools. I took a look at the ctools module and instead of $module, it is using $owner. Try this:

/**
 * Implements hook_ctools_plugin_directory().
 */
function CUSTOM_MODULE_ctools_plugin_directory($owner, $plugin) {
  if ($owner == 'feeds_tamper') {
    return 'plugins';
  }
}

As for the actual plugin include, I just copied one of the existing files and modified it as needed.

ykyuen’s picture

Hi guys,

i have read the api doc about the hook_ctools_plugin_directory().

I still didn't get the idea about the $owner.

For example, i want to create new custom layouts which would be stored in my custom module my_module/plugins/layouts folder. so in that in that case i just need to tell the ctools system to check the my_module/plugins/layouts to find my new custom layouts. why do i need to check the $owner whether it is 'ctools' or 'feeds_tamper' or 'panels'?

Thanks in advance.

Kit

ykyuen’s picture

Finally figure it out. All ctools plugins could be used by other module. in this example, the ctools plugins would be available for feeds_temper.

mmaranao’s picture

What I'm trying to achieve is if the target field value has been changed, I don't want it to be overwritten by the source field value. Is that possible?

sarbazx’s picture

Hi, How to this in Drupal 8/9? is there any documentation?

zapaaron’s picture

These examples did not load for me on Drupal 9 for some reason. Wondering if it was a pathing issue as we have modules/contrib where feeds_tamper resides and modules/custom where this example module is. While there is no errors, it does not get picked up in the dropdown as a tamper plugin to add. Do you think it needs to be added to modules/contrib/Tamper as that's where all the other plugins are being shown from?

pub497’s picture

If anyone stumbles upon this, to do this in d9+ you simply need to make your own Tamper plugin, check out contrib module tamper>src>Plugin>Tamper for examples.