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.
This recipe is based on the recipe Providing new Events, Conditions, and Actions (Become an expert) in this book.
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') )
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 ); }
After clearing caches, the newly created action will be available in the list of Actions:
Optionally we can modify the variable's label and suggested machine readable name in the next configuration screen.
Tip
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.

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.