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 new variables for Actions (Become an expert)

This example explains how to modify existing or provide new variables and data for Rules in Actions.

We'll extend our previously defined action with a new one that provides additional data to Rules after the action is executed. In this case, the data provided to Rules is the number of currently registered users on the site.

Getting ready

This recipe is based on the recipe Providing new Events, Conditions, and Actions (Become an expert) in this book.

How to do it...

  1. Add a new associative array to our hook_rules_action_info() function and instead of "parameters" we'll use the "provides" property:

    'custom_registered_users' => array(
    'label' => t('Get number of registered users'),
        'provides' => array(
          'number_of_users' => array(
            'type' => 'integer',
            'label' => t('Number of users')
        'group' => t('Rules Custom')
  2. Create the callback function that returns an array in the format Rules expects it:

    * Callback function that returns the current number
    * of registered users and returns it to Rules in an
    * array
      function custom_registered_users() {
        $result = db_query("SELECT count(*) FROM {users} WHERE uid > 1")->fetchField();	
        // Return an array for Rules with the array key
        // being the machine readable name defined in the
        // 'provides' property
        return array(
           'number_of_users' => $result
  3. After clearing caches, the newly created action will be available in the list of Actions:

  4. Optionally we can modify the variable's label and suggested machine readable name in the next configuration screen.


When adding additional Actions our new variable becomes available to Rules. For the purpose of this example, we'll add the Action, System | Show a message on the site and display the results in the MESSAGE field. Note that the created variable doesn't become available as a token, so we need to Switch to data selection and select the variable from the drop-down list.

How it works...

Actions can provide new variables to Rules by making use of the provides property in hook_rules_action_info(). The data structure is almost identical to the way we declare parameters, the only difference is that user input is not allowed. By providing new variables to Rules, we can execute complex functions in an action and then work with their return data while still in Rules.