Posted by bartl on June 30, 2011 at 7:47am
Is it possible to execute a ruleset from within PHP?
I know I can call PHP code from within a rule action, but what I want is the reverse: call some function in PHP which in turn triggers a bunch of rules, for example to send a mail...
I'm using D7, so integration with other modules, like Flag, is, at best, dubious...
Comments
From the docs...
There is some pretty good documentation on how to code for Rules, and how to build modules that work together with the Rules framework. I think this is the page that will help you the most: http://drupal.org/node/905262
Good luck!
//Johan Falk, NodeOne
**
Learn Drupal with Nodeone! Drupal 7 introduction | The Views module | Drupal learning curve | Rules for Drupal 6 | Theming Drupal 6
So here's what I ended up doing...
Thanks, but looking at that page (and related pages) for hours, I just got nowhere. Googling brought no example of code using this API to the surface, so it looks like not many people are doing it. Not in open source, anyway.
Then, thanks to looking at the source of the Rule Forms project, I found it used a custom event (specified using hook_rules_event_info — careful: the word "rules" and the module name may swap places(!!) in the name of this hook, for no reason that is obvious to me), which you can trigger using code with rules_invoke_event. Then, I can just define rules that get triggered by this event, and the rest is plain rules.
BTW you can add parameters to
rules_invoke_eventwith other types of variables than the usual objects (comment, node, user), but the list of data types that I found for Rules 1.x for Drupal 6, is wrong for Rules 2.x/Drupal 7: "string" and "number" are not recognized as data types, but "text", and "integer" and "decimal", are.p.s. Rule Forms doesn't work. I get error messages indicating that global variable
$usergets set to null, and the admin interface is not accessible at all.