The Rules module has become very popular. It allows responding to certain events, adding conditions, and performing actions based on them.
Rules operate with three entities:
- event (user login, node deletion, etc.)
- condition (node type, user role, etc.)
- action (creating a node, updating a field value, redirect, etc.)
The Rules module has a very good API that allows you to extend its standard features. The following code adds a custom action to clear messages set by drupal_set_message
:
/**
* @file
* Extension of rules
*/
/**
* Implements hook_rules_action_info
*/
function extend_rules_action_rules_action_info() {
return array(
'clear_messages' => array(
'label' => t('Clear message'), // action name
'group' => t('System'), // group where it will be displayed
'base' => 'extend_rules_action_clear_message', // callback
'parameter' => array(
'user' => array('type' => 'user', 'label' => t('User')), // parameter for selecting a user
'node' => array('type' => 'node', 'label' => t('Node')), // parameter for selecting a node
),
),
);
}
/**
* Rule clear_message callback
*/
function extend_rules_action_clear_message($user, $node) {
drupal_get_messages();
}
In the implementation of hook_rules_action_info
, the main action information is defined. Details are described in the comments. The parameter
element lets you create an action settings form, allowing the selection of values available to the callback function.