This recipe demonstrates how to provide new entity tokens for Rules. Entity tokens provides a way to use placeholders in Rules (and other modules) and dynamically replace them with chunks of data.
In this example, we'll provide the current number of registered users on our site as a globally available token for Rules.
Implement
hook_entity_property_info()
to provide our new entity token:/** * Implements hook_entity_property_info() * We extend the natively available 'site' properties */ function custom_entity_property_info() { $info = array(); $properties = &$info['site']['properties']; $properties['registered_users'] = array( 'label' => t("Number of registered users"), 'type' => 'integer', 'description' => t("Returns the current number of registered users on the site."), 'getter callback' => 'custom_number_of_users' ); return $info; }
We've defined
custom_number_of_users
as the callback function in thegetter callback
property, so we'll create this function:/** * Callback function that returns the current number of registered users */ function custom_number_of_users() { $result = db_query("SELECT count(*) FROM {users} WHERE uid > 1")->fetchField(); return $result; }
The newly created entity token will be available to use in Conditions and Actions in REPLACEMENT PATTERNS:
By implementing hook_entity_property_info()
, we're providing the Entity API information about our new entity token. The function that returns data needs to be defined in the getter callback
property. Implementing this hook makes it possible to use new tokens in the rule configurations, or any other configuration that uses Entity API.