1


Hello,
I have the Rules module. I tried to programmatically create or modify a rule but it doesn't work. What I tried so far : I created the MY_MODULE.rules_defaults.inc file and wrote this code :

function MY_MODULE_default_rules_configuration() {
$configs = array();
$rule = { "rules_name" : {
  "LABEL" : "rules ",
  "PLUGIN" : "reaction rule",
  "OWNER" : "rules",
  "REQUIRES" : [ "rules", "mimemail", "uc_cart" ],
  "ON" : { "uc_checkout_complete" : [] },
  "DO" : [
    { "component_rules_commerce_order_status_pending" : { "order" : [ "site:current-cart-order" ] } },
    { "mimemail" : {
        "key" : "[site:name]",
        "to" : "mail@mail.com",
        "subject" : "[site:name]",
        "body" : "Random text",
        "language" : [ "" ]
      }
    }
  ]
}
}
 $configs['rules_name'] = rules_import($rule);
  return $configs;
}

Yet it doesn't work. I also tried with MY_MODULE_default_rules_configuration_alter() but to no avail.

6

You have to create new Rule via the provided APIs and HOOKs from the Rules module.

From creating rules through the UI you have seen their configuration attributes, such as

  1. Rules Event
  2. Rules Condition
  3. Rules Action etc.

Now you have all the useful HOOKs to create new ones. If you want to add a new Rules event to your System then go for hook_rules_event_info()

/**
* Implementation of hook_rules_event_info().
* @ingroup rules
*/
function your_module_rules_event_info() {
  return array(
    'your_module_custom_event' => array(
      'label' => t('A custom event happened in your application'),
      'module' => 'your_module',
      'arguments' => array(
        'param1' => array('type' => 'user', 'label' => t('The user object')),
      ),
    ),
  );
}

You can invoke this event also from custom code in your module as

rules_invoke_event('your_module_custom_event', $param1);

If you want to add some custom conditions to a pre defined rule then use hook_rules_condition_info()

/**
 * Implements hook_rules_condition_info().
 * Checks if the given user is male or not.
 */
function your_module_rules_condition_info() {
  return array(
    'user_is_a_male' => array(
      'group' => t('test'),
      'label' => t('If user is male'),
      'parameter' => array(
        'user' => array(
          'type' => 'user',
          'label' => t('Validate registered users if male'),
        )
      ),
    ),
  );
}
function user_is_a_male($user) {
  // your custom condition to return boolean TRUE or FALSE
}

To alter pre-defined Rules you might use hook_rules_component_alter or hook_default_rules_configuration_alter but again depending on needs these will vary. Go through via the link above and you will find all.

FYI, you can export Rules via Features module with your custom HOOKs as well. And these custom HOOK's should be placed on a file as "your_module.rules.inc" so Rules module will include these automatically.

| improve this answer | |
0

Try entity_import like this:

  $items['rules_take_cr'] = entity_import('rules_config', '{ "rules_take_cr" : {
      "LABEL" : "Take CR",
      "PLUGIN" : "reaction rule",
      "OWNER" : "rules",
      "TAGS" : [ "Customization request", "Logo NG" ],
      "REQUIRES" : [ "rules", "flag" ],
      "ON" : { "flag_flagged_take_customization_request" : [] },
      "DO" : [
        { "data_set" : { "data" : [ "flagged-node:author" ], "value" : [ "flagging-user" ] } }
      ]
    }
  }');
  return $items;
| improve this answer | |
  • Already tried that before, I forgot to mention it. But it doesn't work. Do I have to add something in the .module file? – Jiang Jul 4 '14 at 13:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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