Building upon the API

Last updated on
30 November 2019

Drupal 7 will no longer be supported after January 5, 2025. Learn more and find resources for Drupal 7 sites

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:

  // 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.

  $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:

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.

Help improve this page

Page status: No known problems

You can: