Data types
Drupal 7 will no longer be supported after January 5, 2025. Learn more and find resources for Drupal 7 sites
Supported data types
The list of data types supported is:
- boolean
- date
- decimal
- duration
- entity
- integer
- ip_address
- list
- list<integer>
- list<text>
- list<token>
- log_entry
- text
- text_formatted
- token
- uri
- struct
Non-entity data structures & Entities
Any Drupal module can integrate with Rules by providing new entity types and entity metadata by using the Entity API to describe entity properties or directly by using non-entity data structures. Rules core builds heavily on the data types provided by the Entity API, however it is possible to add support for data structures which are not entities.
Additional data types are registered using hook_rules_data_info()
, similar to the definition of "entity" type. This allows Rules to work with variables contained by any data or entity type and integrate at the most direct level. With this hook, you may also define the 'parent' type of a data type, that Rules makes use of for type-matching only. E.g. this is used to make it possible to apply an action working with an "entity" parameter to a node variable. However these data types are not available in Direct Input mode.
The example below uses Rules without Entity API to provide support for watchdog log entries. Property metadata needs to be specified, and a direct input form may be specified.
/**
* Implements hook_rules_data_info() on behalf of the system module.
* @see rules_core_modules()
*/
function rules_system_data_info() {
return array(
'log_entry' => array(
'label' => t('watchdog log entry'),
'wrap' => TRUE,
'property info' => _rules_system_watchdog_log_entry_info(),
),
);
}
/**
* Defines property info for watchdog log entries, used by the log entry data
* type to provide an useful metadata wrapper.
*/
function _rules_system_watchdog_log_entry_info() {
return array(
'type' => array(
'type' => 'text',
'label' => t('The category to which this message belongs'),
),
'message' => array(
'type' => 'text',
'label' => t('Log message'),
'getter callback' => 'rules_system_log_get_message',
'sanitized' => TRUE,
),
'severity' => array(
'type' => 'integer',
'label' => t('Severity'),
'options list' => 'watchdog_severity_levels',
),
'request_uri' => array(
'type' => 'uri',
'label' => t('Request uri'),
),
'link' => array(
'type' => 'text',
'label' => t('An associated, HTML formatted link'),
),
);
}
Token replacement in Rules is handled by token_generate(). To make additional data type properties available for replacement in Rules Actions, you must implement hook_tokens(). Here is an example:
/**
* Implements hook_tokens().
*/
function mydata_tokens($type, $tokens, array $data = array(), array $options = array()) {
if ($type == 'mydata') {
if(isset($data['mydata'])) {
$replacements = array();
$mydata = $data['mydata'];
foreach ($tokens as $name => $original) {
switch ($name) {
case 'name':
$replacements[$original] = $mydata['name'];
break;
case 'address':
$replacements[$original] = $mydata['address'];
break;
}
}
return $replacements;
}
}
}
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion