2

I've just defined this event with the two arguments: number and message. I want to use them to verify the message and send a response back to the number. I have an action for that but I don't know how to pass the arguments.

function sms_rules_rules_event_info() {
  return array(
      'sms_rules_sms_received' => array(
         'label' => t('SMS received'),
         'module' => 'sms_rules',
         'arguments' => array(
            'number' => array('type' => 'text', 'label' => t('Number from where the message was received.')),
            'message' => array('type' => 'long text', 'label' => t('Contents of message.')),
         ),
      ),
  );
}

1 Answer 1

2

In your action you might have something like

function mymodule_rules_action_info() {
  return array(
    'my_action' => array(
      'label' => t('Label'),
      'module' => t('My module'),
      'arguments' => array(
        'number' => array('type' => 'text', 'label' => t('Number from where the message was received.')),
        'message' => array('type' => 'long text', 'label' => t('Contents of message.')),
      ),
      'base' => 'mymodule_my_action',
    ),
  );
}

The 'base' property indicates the callback for the action. You can also use the key of your action as the callback if you choose not to set a 'base'. In this case it would be function my_action(). The arguments defined in the action will automatically be passed to that function. Now, whether the action is available is dependent upon which arguments your event makes available and which arguments your action requires. So, if I had an event that passes a 'type' => 'user' and 'type' => 'node' then only actions that have arguments 'user' and 'node' would be available. This is how Rules 1 works. Alternatively, by using a simple 'text' data type the actions will always be available because the data type is not unique. You can specify your own data types by implementing hook_rules_data_type_info().

Anyways, I got off track. So, with your base being mymodule_my_action:

function mymodule_my_action($number, $message) {
  // Do stuff here.
  return $number;
}

Another addition you can make if necessary is adding a settings form to your action.

function mymodule_my_action_form($settings, &$form) {
  $form['settings']['my_options'] = array(
    '#type' => 'select',
    '#title' => t('Select an option'),
    '#options' => array(
      'this' => t('this'),
      'that' => t('that'),
      'also' => t('also'),
    ),
    '#default_value' => $settings['my_options'],
    '#required' => TRUE,
  );
}

In this case, you would just add the settings array to your action callback as the last argument:

function mymodule_my_action($number, $message, $settings) {
  if ($settings['my_options'] == 'this') {
    // Do stuff.
  }
}

Hmm... I don't even know if I answered your question, but I tried to provide as much info as possible. If I completely missed it please give me some guidance. I maintain three Rules modules, so I've been around that module for a while and I'm glad to help.

2
  • Looks very good. Unfortunately I won't be needing it for a few weeks so I can't give you feedback on the answer, but I'll vote it and come back when I do. Aug 28, 2011 at 22:08
  • Oh, I remembered what my initial problem was. I needed to know how to write an event that gives me variables so I can use them for an action. Aug 28, 2011 at 22:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.