Book Image

Drupal Rules How-to

By : Robert Varkonyi
Book Image

Drupal Rules How-to

By: Robert Varkonyi

Overview of this book

Rules is what every Drupal site builder and developer has to use when creating event ñ action-based applications. The framework provides a highly flexible way to create sophisticated, condition-based functions any Drupal based system into an interactive application. Rules makes Drupal rule the CMS world."Drupal Rules How-to" is a practical, hands-on guide that provides you with a number of clear step-by-step exercises, which will help you take advantage of the real power of the Rules framework, and understand how to use it on a site builder and developer levelThis book demonstrates the power and flexibility of the Rules framework. It discusses the main aspects of the module both from the site builder and developer perspective, from basic and advanced Rule configurations using Events, Conditions, Actions and Components to getting familiar with the Rules API. You will also learn how to use additional modules together with Rules to further extend the possibilities of your Drupal system, such as Rules Scheduler to schedule the execution of your Rule configurations and Views Bulk Operations to execute Rule configurations on a view result list. The book also demonstrates the main API features that enable you to create your own Events, Conditions and Actions, provide new data types to Rules and execute your configurations programmatically
Table of Contents (7 chapters)

Providing default rule configurations (Become an expert)

This recipe explains how to provide default rule configurations in code. The advantage of that is that we can keep our configurations in code and use version control, such as, SVN or Git.

How to do it...

  1. In our custom module's folder, we add a new file called and declare the rule configuration by implementing hook_default_rules_configuration(). The contents of the file are as follows:

    * Implements hook_default_rules_configuration()
    function custom_default_rules_configuration() {
      $rule = rules_reaction_rule();
      $rule->label = 'Default Rule';
      $rule->active = TRUE;
      ->condition('data_is', array('data:select' => 'node:type',       'value' => 'article'))
      ->condition(rules_condition('data_is', array('data:select' =>       'node:author:uid', 'value' => 1))->negate())
      ->action('drupal_message', array('message' => 'Hey [node:author], thanks for creating a new article!'));
      $configs['custom_default_rule'] = $rule;
      return $configs;
  2. After clearing the caches, our newly created default rule will become available in the list of configurations, as shown in the following screenshot:

How it works...

Using hook_default_rules_configuration(), we can define our rule configuration in code using Rules' methods for Events, Conditions, and Actions. Rules will look for a file * in our module's folder, and automatically add our default rule to the available configurations after clearing the caches.

There's more...

Rules is compatible with the Features module, which provides a centralized API for exporting and importing configuration from the database. This is also an effective way to manage configuration in code and version control systems.

Altering default rule configurations

It is also possible to modify a default rule configuration in code. For that we could use hook_default_rules_configuration_alter() in our * file.

   * Implements hook_default_rules_configuration_alter()
  function custom_default_rules_configuration_alter(&$configs) {
    $configs['custom_default_rule']->condition('data_is', array('data:select' => 'node:is_new', 'value' => TRUE));

Making changes to the configuration on the UI

Rules tracks the state of a Rule configuration that has been added programmatically. What that means is that it can determine whether an imported configuration is in its default state (not modified compared to the code) or overridden (modified using the UI, but not in code). When a configuration is modified, Rules allows to revert it back to its original state.

By clicking on that, we're telling Rules that it should re-read the configuration that we've defined in code and revert it to its original state.