Book Image

Drupal Rules How-to

By : Robert Varkonyi
Book Image

Drupal Rules How-to

By: Robert Varkonyi

Overview of this book

Rules is what every Drupal site builder and developer has to use when creating event ñ action-based applications. The framework provides a highly flexible way to create sophisticated, condition-based functions any Drupal based system into an interactive application. Rules makes Drupal rule the CMS world."Drupal Rules How-to" is a practical, hands-on guide that provides you with a number of clear step-by-step exercises, which will help you take advantage of the real power of the Rules framework, and understand how to use it on a site builder and developer levelThis book demonstrates the power and flexibility of the Rules framework. It discusses the main aspects of the module both from the site builder and developer perspective, from basic and advanced Rule configurations using Events, Conditions, Actions and Components to getting familiar with the Rules API. You will also learn how to use additional modules together with Rules to further extend the possibilities of your Drupal system, such as Rules Scheduler to schedule the execution of your Rule configurations and Views Bulk Operations to execute Rule configurations on a view result list. The book also demonstrates the main API features that enable you to create your own Events, Conditions and Actions, provide new data types to Rules and execute your configurations programmatically
Table of Contents (7 chapters)

Providing new entity tokens (Become an expert)

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.

How to do it...

  1. 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;
  2. We've defined custom_number_of_users as the callback function in the getter 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;
  3. The newly created entity token will be available to use in Conditions and Actions in REPLACEMENT PATTERNS:

How it works...

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.