Last updated May 21, 2012. Created by fago on September 8, 2010.
Edited by mitchell, August1914. Log in to edit this page.

The Rules API may be used in various ways as Rules is built pretty modular. First off it is possible to re-use all Rules plugins on their own, that way e.g. it is possible to build upon the Rule conditions only. Also the UI is reusable on a plugin basis by using the methods as defined by the RulesPluginUIInterface.

Example using the condition API:

<?php
 
// Configure a condition and execute on a node:
 
$condition = rules_condition('node_is_of_type', array('type' => array('page')));
 
$condition->execute($node);
 
// Alternatively it could be executed directly too:
 
rules_condition('node_is_of_type')->execute($node, array('page'));
?>

For allowing users to configure any condition/action you may want to use a condition or action set + embed its UI.

<?php
  $variables
= array('node' => array('label' => t('Content'), 'type' => 'node');
 
$condition_set = rules_and($variables);
 
// Save.
 
$condition_set->save('my_name', 'my_module');
 
// Embed the form.
 
$options = array('show settings' => TRUE, 'button' => TRUE);
 
$condition_set->form($form, $form_state, $options);
 
// Validate (in your validation handler).
 
$condition_set->form_validate($form, $form_state);
 
// And on the final submit, make changes permanent. (in your submit handler)
 
$condition_set->form_submit($form, $form_state);
  .....
?>

Each configured component is prepared and cached for evaluation, so to execute it use:

<?php
list($providedVar1, $providedVar2) = rules_invoke_component('my_name', $variable1, $variable2, ..);
?>

Building on that example, it's possible to embed a form for managing a bunch of user created components or reaction rules, e.g. restricted to the events a module provides.

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.