Community Documentation

8. Core style actions and rules

Last updated February 5, 2009. Created by fago on August 23, 2008.
Log in to edit this page.

Rules is capable of executing drupal's core style actions too. Core style actions of the type 'node', 'comment', 'user' or 'system' are available to be used with rules automatically.

However some features of the rules module, like better default labels through label callbacks are not supported by the core style actions. But one can add label callbacks for core style actions - so that rules makes use of them.

Rules automatically converts the action info return by hook_action_info() to the rules format and add it's to it's own action infos (hook_rules_action_info()). This means you can use hook_rules_action_info_alter() to customize the action info for rules to add a custom label callback or to do other things:

<?php
/**
* Implementation of hook_rules_action_info_alter().
*
* Adapts the action info of the core actions to better fit for rules.
* @ingroup rules
*/
function node_rules_action_info_alter(&$actions) {
 
// Add a label callback to improve the labels of the actions.
 
foreach (array_keys(node_action_info()) as $name) {
   
$actions['rules_core_'. $name]['label callback'] = 'rules_core_node_label_callback';
  }

 
// The rules action is more powerful, so hide the core action
 
unset($actions['rules_core_node_assign_owner_action']);
 
// We prefer handling saving by rules - not by the user.
 
unset($actions['rules_core_node_save_action']);
}
?>

The names of the core actions are prefixed with 'rules_core_'.
Note that you can also set 'rules_ignore' to TRUE in the usual hook_action_info() and rules will ignore the action.

Mapping types to arguments

To let rules make use of the action it needs to know the arguments of the action. Core actions support only one argument, where all actions from the same type work with the same type of arguments. So rules determines the needed arguments by the type of the core style action. For this modules may define a mapping through hook_rules_action_type_map(). E.g.:

<?php
/**
* Implementation of hook_rules_action_type_map().
*
* Maps core action types to rules action info. This provides mappings for all
* action types used in core.
* @ingroup rules
*/
function rules_rules_action_type_map() {
  return array(
   
'node' => array(
     
'module' => 'Node',
     
'arguments' => array(
       
'node' => array(
         
'label' => t('Content'),
         
'type' => 'node',
        ),
      ),
    ),
   
'comment' => array(
     
'module' => 'Comment',
     
'arguments' => array(
       
'comment' => array(
         
'label' => t('Comment'),
         
'type' => 'comment',
         ),
      ),
    ),
   
'user' => array(
     
'module' => 'User',
     
'arguments' => array(
       
'user' => array(
         
'label' => t('User'),
         
'type' => 'user',
        ),
      ),
    ),
  );
}
?>

So if you create a new type of actions and you want rules to support these action, you'll need to provide a suiting mapping. Note that rules doesn't provide support for core actions of the type 'system' as most system actions are not working well without further fixes. But still you can provide support for a single core action of an unsupported type.

Provide support for single core actions

If the type of core actions is not supported by a mapping as above, you can still provide the rules action info on your own. Here is an example which does so for the action of the twitter actions module, which is of the unsupported core action type 'system'.

<?php
/**
* Implementation of hook_rules_action_info_alter().
*
* Actions of type system are not supported, so rules won't make use of it automatically. So
* we have to provide some information about it to make it work.
*/
function twitter_actions_rules_action_info_alter(&$actions) {
 
$actions['rules_core_twitter_actions_set_status_action'] = array(
   
'label' => t('Post a message to Twitter'),
   
// Make sure there is something passed for $object to the action.
   
'arguments' => array('object' => array('type' => 'value', 'default value' => NULL)),
   
'module' => 'Twitter',
   
'eval input' => array('screen_name', 'password', 'message'),
   
// Let the rules system for executing core style actions execute it.
   
'base' => 'rules_core_action_execute',
   
'action_name' => 'twitter_actions_set_status_action',
   
'configurable' => TRUE,
  );
}
?>

About this page

Drupal version
Drupal 6.x

Site Building Guide

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.
nobody click here