How to pass value from rules_invoke_event() to the rules_action_info() 'base' handler?

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

Hi,

I'm fairly new to Rules in Drupal 7 and I'm trying to figure out how to pass a string value from the rules_invoke_event() call so that I can access it in the handler defined by _rules_action_info()...

I invoke the event...

rules_invoke_event('my event', 'this is a test');

I define the handler here...

function my_module_rules_action_info() {
  $defaults = array(
    'group' => 'My Group'
  );

  return array(
    'my_action_name' => array(
      'label' => t('Do Something'),
      'base' => 'my_method'
    )
  );
}

and I want to access the string (value: "this is a test") here...

function my_method() {
...
}

Any ideas? I'm stuck!

Thanks,

Steve

Comments

Events and actions

itangalo's picture

When using rules_invoke_event(), you should call an event – but it seems you are declaring an action.

The events are used to (potentially) trigger reaction rules, and all the parameters from the invocation will then be available inside the reaction rule.

This module gives some useful examples of how to code for Rules: http://drupal.org/project/rules_example
These screencasts may also be of use: http://dev.nodeone.se/node/895

Good luck!
//Johan Falk, Sweden

Thanks Johan, It appears the

steve.neill's picture

Thanks Johan,

It appears the dev.nodeone.se site is down -- can't reach it from here.

I've managed to invoke and event and do the action, the problem is passing stuff from the invoke() and seeing it appear in the action handler. I see the parameters defined by the event, but nothing else.

Steve

Stored in parameters

itangalo's picture

Strange that dev.nodeone.se doesn't seem to work – it seems fine from here. I hope it was just a glitch, and that it works properly again.

The parameter value you send off in rules_invoke_event() should be available as the value of the parameter – not any new parameters. Thus, if you try just printing out the parameter ("show a message on the site"), you should see the value. (If you have the Devel module installed, there is also the very hande "debyg" action available to inspect variables.)

If this doesn't work – what does your hook_rules_event_info look like?

Good luck,
//Johan Falk

Thanks again Johan... I

steve.neill's picture

Thanks again Johan... I checked out the Rules Example code and that helped a little... but I still can't get it to work. I've attached some code that shows how I'm trying to make this work.

Thanks,

Steve

function mymodule_rules_event_info() {
  $defaults = array(
    'group' => t('My Module')
  );

  return array(
    'mymodule_search' => $defaults + array(
      'label' => t('User has performed a search'),
      'variables' => array(
        'eventname' => array(
          'type' => 'text',
          'label' => t('Points Event Name')
        )
      )
    )
  );
}

function mymodule_rules_action_info() {
  $defaults = array(
    'group' => t('My Module')
  );

  return array(
    'special_add_points' => $defaults + array(
      'label' => t('Award points'),
      'parameter' => array(
        'points' => array(
          'type' => 'text',
          'label' => t('Points'),
          'description' => t('Award points to the user'),
          'options list' => 'mymodule_rules_get_special_list',
        ),
      ),
    )
  );
}

function mymodule_rules_get_special_list() {
  return array(
    1 => 1,
    2 => 2,
    3 => 3
  );
}

function special_add_points($points, $foo) {
  print_r($points);
  print_r($foo); // <--- I'm expecting to see eventname in $foo
}

rules_invoke_event('mymodule_search', array('eventname' => 'testing!'));

PS. I also

steve.neill's picture

PS. I also tried

rules_invoke_event('mymodule_search', 'testing!');

with the same result. I'm also clearing the Drupal cache after each change.

Steve

Where do you put rules_invoke_event?

itangalo's picture

The only weird thing I can see about the code above is that you seem to call rules_invoke_event() directly from the module – not by placing the statement inside a hook or something. Usually you want these events to trigger when certain things happen on the site – for example a node being saved, a user makes a search, or whatev.

I would try this:
1) Place rules_invoke_event() inside some hook function, say hook_node_view().
2) Skip your custom action for now, and just make sure that the parameter value gets passed into Rules correctly. (Use the "debug" action from the Devel module instead.)

BTW: If you write rules_invoke_event('mymodule_search', array('eventname' => 'testing!'));, I think you should use rules_invoke_event_by_args() instead. I'm not 100% sure, but rules.api.php should give you a clear answer.

Good luck!

sorry... i just listed the

steve.neill's picture

sorry... i just listed the code snippets... it's not the actual code as laid out in the various files.

I actually got it to work (finally) by changing the action def as follows:

function mymodule_rules_action_info() {
  $defaults = array(
    'group' => t('My Module')
  );

  return array(
    'special_add_points' => $defaults + array(
      'label' => t('Award points'),
      'parameter' => array(
        'points' => array(
          'type' => 'text',
          'label' => t('Points'),
          'description' => t('Award points to the user'),
          'options list' => 'mymodule_rules_get_special_list',
        ),
        'eventname' => array(
          'type' => 'text',
          'label' => t('The event name')
        ),
      ),
    )
  );
}

It appears that you need to define any parameter that gets passed to the 'special_add_points' method.

Hopefully my understanding is correct, if not what more is there? :)

Steve

Pass multiple value into Rule

tamnv's picture

How can I pass variable as multiple value?. For example I need to pass variable for "List of node items" parameter

Rules

Group organizers

Group categories

Categories

Group notifications

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