Everything which can be exported with rules, can also be provided as default configuration with a module. So you can create some rules and rule sets, export it and provide it as default.

To do so, just create a new include file for your module {modulename}.rules_defaults.inc. You don't need to included it yourself, rules does that for you. Note that the cache has to be cleared so that rules picks up the file, for more about that see this.

In this file, implement hook_rules_defaults() and just return your exported stuff there.

The export is of the following format, where the keys are the rule / rule set names.

<?php

/**
 * Implements hook_rules_defaults().
 */
function mymodule_rules_defaults() {
  $config = array(
    'rules' => array(
      'rules_default_rule_1' => array(  ... )
      'rules_default_rule_2' => array(  ... )
    ),
    'rule_sets' => array(
      'rules_default_set_1' => array(  ... )
      'rules_default_set_2' => array(  ... )
    ),
  );

  return $config;
}


?>

So you can just use the output of the export mechanism for providing defaults. However you need to make sure that the names of the items are unique - so set suiting names for your exported items and prefix them with your module name.

Furthermore you need to modify the '#status' or 'status' properties of rules and rule sets. Set it to 'default' or 'fixed'. Fixed means, that the rule (set) is hidden from the UI and can't be customized by users.

Using default rule sets to allow users to customize behavior

Default rule sets can be neat, if you want to build some functionality, which users can adapt to their needs afterwards. So all you have to do is to provide the functionality as a default rule set. Then you can call the rule set as easy as doing:

rules_invoke_rule_set('your_set_name', $arg1, $arg2, ..);

If you don't want to make a dependency on rules, just provide an alternative as code and only when rules is installed, use the rule set.

Comments

benoit.borrel’s picture

Exporting rules into files allows you to manage their configuration through your codebase versioning control system (i.e. Git, SVN...).

To achieve the export part:

  1. go to admin/rules/ie/export to export 1 rule at a time. It will give you array('rules' => array('rule_name' => array('#type' => 'rule', ...)))
  2. into a file named after the rule machine name and suffixed by .rule, prepend <?php and paste the 3rd array as $rule = array('#type' => 'rule', ...) into it
  3. modify the '#status' value from 'custom' to 'default': '#status' => 'default'

To achieve the auto loading part:

  1. implement hook_lists_rules_defaults() like this:
    your-module-name_lists_rules_defaults() {
      static $config = array();
    
      if (empty($config)) {
        // load rules
        $files = file_scan_directory(drupal_get_path('module', 'your-module-name'). '/rules', '.rule$');
        foreach ($files as $absolute => $file) {
          require $absolute;
          if (isset($rule)) {
            $config['rules'][$file->name] = $rule;
          }
        }
      }
    
      return $config;
    }