question about rules api and how to "attach" or "listen" to an event...

Events happening in the community are now at Drupal community events on www.drupal.org.
dlirit's picture

if a module does something like:

rules_invoke_event('some_event', $some_args)

this means it's "triggering" an action to all modules which have "attached" or are "listening" to this "some_event" event.
how do I "listen" to that event to fire off my own function in my module? havent seen anything on the documentation that
explains that.

Comments

bump

dlirit's picture

bump.

seriously, no one? ...

Did you solve this?

Crom's picture

...I have a similar requirement: I need to intercept a rules event and change it based on certain parameters but I can't find the hook to do this on the fly.

Write Rules actions

itangalo's picture

dlirit:
If you want to have things happening on a Rules event, the best way is probably to use the Rules module and set up a reaction rule. If the actions you want to perform are not available you could write your own actions – but there are quite flexible actions available already.

Crom:
If you want to change the parameters sent by the event invokation, I don't really know how to do this – possibly the best way is simply to write your own event plugin. If you want to have conditions and actions based on the available information, though, the standard Rules configuration is your friend.

I made some screencasts on how to write Rules plugins some time ago, that probably could be of some help. They require that you're fairly familiar with the Rules module, though. (Check out Coding for Rules 2 for the full screencast series.)

Good luck!
//Johan Falk
**
Check out NodeOne's Drupal Learning Library! 150+ screencasts and exercises, for Drupal introduction, advanced configuration, and coding.

Great screencasts - really

Crom's picture

Great screencasts - really interesting - thank you!!!

Unfortunately, I am unable to find a way to resolve my possibly unique situation where I am trying to either re-write or override an existing rule that is stored in a feature that I really don't want to hack/edit due to upgrading problems. I cannot overwrite the existing rule because of this problem: http://drupal.org/node/1203480

So, I thought about hooking into the rule at the point of execution and either cancelling or modifying the action, but there doesn't appear to be a way to do that.

I've also tried looking at the problem the other way here: http://drupal.org/node/1269872 seemingly also to no avail.

I've fudged a solution at the moment but it's not efficient.

Thanks again for the link to the screencasts!
Crom

Changing the feature

itangalo's picture

Ok, now I understand better what you want to do.

I think the sane approach to this is to change the rule that you don't want to have. That is the actual problem – a rule that captures more event than you are interested in. This would require "forking" Drupal Commons, but I think that is prefered over writing a new hook or event. To allow updates in a nice way, you could re-export your feature and save the changes as a patch – to apply after fetching any updates to Drupal Commons.

If you don't want to do this, then the hook you already have implemented is probably the best approach. It is non-intrusive, even if it is fired on all heartbeat messages, and allows the rest of your site to work as expected.

There is no Rules event that you can use to "revoke" messages, as far as I know, and even if it is possible to write one you would probably have to rewrite parts of the original hook to allow modifications. (Or just work with the _alter hook you are already using.) So there is no easy solution for this. :-/

Good luck!
//Johan Falk
**
Check out NodeOne's Drupal Learning Library! 180+ screencasts and exercises, for Drupal introduction, advanced configuration, and coding. Creative Commons license!

Thank you

Crom's picture

Yes, I came to the same conclusion. I think I will leave it for now and do some performance testing to see the impact on the site.

Cheers,
Crom

Rules

Group organizers

Group categories

Categories

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: