Community Documentation

Data types

Last updated February 8, 2013. Created by fago on September 8, 2010.
Edited by eosrei, gollyg, mradcliffe, drupalshrek. Log in to edit this page.

Supported data types

The list of data types supported is:

  • date
  • duration
  • integer
  • decimal
  • text
  • token
  • boolean
  • uri
  • list
  • entity
  • 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.

<?php
/**
* 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:

<?php
/**
* 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;
    }
  }
}
?>

About this page

Drupal version
Drupal 7.x
Audience
Programmers
Level
Intermediate
Drupal’s online documentation is © 2000-2013 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. Comments on documentation pages are used to improve content and then deleted.