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

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

  1. theorichel

    Hi!
    Though I am a non coder, your short example is inspiring…. maybe I can do it myself at least partially. I work with the Rooms module where one can reserve rooms in hotels and so. These reservations have a start and an en-date and I would very much like to create rules that fire on these events (for instance send a survey after the end-date).
    I could only find so far Tokens that refer to start and end dates, but I do not know if those are what I need and how to implement them. Can you give some hints please?

    Thanks

    Theo Richel
    The Netherlands

    Like

    Reply
    1. ykyuen Post author

      Hi Richel,

      In your case, it could be broken down like this

      • Events – Cron maintenance tasks (already available on the Rules UI)
      • Condition – Write your own condition to check the end date of every reservations
      • Action – Send email (already available on the Rules UI)

       

      You have to figure out how to pass the parameters from event to condition as well as Action such then you condition knows which reservation has passed the end date and which user drupal should send the email to.

      Hope this help.

      Kit

      Like

      Reply
  2. Matt

    Thank you. Finally a simple explanation of how to integrate with rules! One mistake I made was to have more than one event – I then did not get the variables exposed to the event action.

    Like

    Reply

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 )

Google+ photo

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

Connecting to %s