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:
Hello, Thank’s for this helpfull post !!
You just forget to say that the php filter module have to enable ^^
LikeLike
Thanks for your reminder, i have added a note on the top of this post. =D
LikeLike
Thanks, great example. So easy.
P.S. all variables are avaivable via tokens. So we don’t need PHP filter here. And it’s cool
LikeLike
Thanks for your comment Alex~ =D
LikeLike
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
LikeLike
Hi Richel,
In your case, it could be broken down like this
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
LikeLike
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.
LikeLike
Thanks for your comment and good to know that i could help~ =D
LikeLike
Thanks for this simple explanation of how to add your own custom event and invoke it!
LikeLike
Good to know that i could help. =D
LikeLike
Hi,
Running Drupal 7 and I have a rule setup that checks for event “check for automatic discounts” then checks some conditions and add a coupon in the cart if the conditions are met. Everything works great.. BUT in my recent log history.. it looks like it’s firing up on every page! How can I only have this rule only run on the /cart page?
Thank you!
LikeLike