Drupal 7 – Create your own Rules event

Update @ 2012-10-25: Please make sure you have the PHP filter module enabled. Thanks Beneto.

The Rules module is must have module for developers who needs to deal with some workflow features. Basically each rule contains 3 parts.

  • Event
  • Condition
  • Action

By default the Rules module already provides some basic events, conditions and actions. But some times we may want to create custom components. For me, the most critical part is create a custom event which i would like to show in this post. For the other two, it is less important because the default components already include Execute custom PHP code. That means even without custom module, i could still do whatever i want in Condition and Action.

So let’s create a module and name the folder as eureka_rule.
eureka_rule.info

name = Eureka Rule
description = Custom rules event in Drupal 7 by ykyuen
core = 7.x
package = Eureka

 

eureka_rule.module

/**
 * Implementation of hook_rules_event_info()
 */
function eureka_rule_rules_event_info() {
  return array(
    'example_rule_event' => array(
      'label' => t('Example rule event'),
      'module' => 'eureka_rule',
      'group' => 'Eureka Rule' ,
      'variables' => array(
        'current_user' => array('type' => 'user', 'label' => t('The current logged in user.')),
        'article' => array('type' => 'node', 'label' => t('The article node.')),
        'some_text' => array('type' => 'text', 'label' => t('Some arbitray text.')),
      ),
    ),
  );
}

 

In the .module file, we define a custom rule which takes 3 input parameters. They are user, a node and a string. These parameters will be passed to the condition and action so we could manipulate them later.

Enable the module and go to admin/config/workflow/rules/reaction/add, you could find your newly created event.

 

A new rule is created but it is not yet completed. The rule does not have its condition and action.

 

Add a new condition and select Execute custom PHP code. As you can see in the variable list($current_user, $article and $some_text), we could make use of those input parameters defined in the event. For simplicity, let’s always return TRUE.

 

Next we add the Show a message on the site action.

 

Your rule is completed but how could we trigger it? We could trigger rule by the rules_invoke_event(). So in the theme_preprocess_html(), i added the following piece of codes.

// Trigger the example rule
global $user;
$article = node_load(2);
rules_invoke_event('example_rule_event', $user, $article, 'Hello Eureka!');

 

Refresh your page and see what you get.

 

Done =)

Reference:

About these ads

2 thoughts on “Drupal 7 – Create your own Rules event

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s