Creating new actions not working properly D7

Events happening in the community are now at Drupal community events on www.drupal.org.
Iulian Arcus's picture

Hello everyone,

I'm trying to create a new action to generate a password. Seems easy, but I can't get it to work.

The action_info

$return['generate_random_password'] = array(
    'group' => t('MyGroup'),
    'label' => t('Generate password'),
    'base' => 'rules_action_generate_password',
   
    'provides' => array(
       'generated_password' => array(
        'type' => 'text',
        'label' => t("Generated Password"),
       ),
    ),
  ); 

and the base function
function rules_action_generate_password() {
  $return = array();
  $return['generated_password']=user_password(10);
  return $return;

}

After a whole struggle I got it to show in my actions list, but after I select it I get an empty form with a continue button that goes round and round.

Comments

Associated array

itangalo's picture

To have your action returning values to Rules, you need to send back an associated array with all the new parameters – not just the single value (which would be problematic if you were to create two different parameters).

In your case, you need to return this:

return array(
  'generated_password' => $return,
);

Two more notes:

  • You should prefix all your actions with the name of your module, to avoid name conflicts. It is also a good habit to use the words action/condition/event to separate different types of Rules plugins. Assuming your module has the name passwordgenerator, your action could have the name passwordgenerator_action_generate_random_password.
  • Your callback function, too, should be prefixed with the name of your module to avoid name conflicts (unless you're reusing functions from other modules, that is). For most actions, the action name and the name of the callback function are the same – and if this is the case you don't need to declare the base parameter.

There are some tutorials on writing Rules plugins available at http://nodeone.se/node/895 (and of course at http://drupal.org/node/878720).

Good luck!
//Johan Falk
**
Check out NodeOne's Drupal Learning Library! 150+ screencasts and exercises, for Drupal introduction, advanced configuration, and coding.

Rules

Group organizers

Group categories

Categories

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: