Drupal Actions and Triggers A Quick Code Example of How to Create a Trigger
Actions and Triggers are awesome, and they made it into core with Drupal 6. The combination of the modules creates an event and responder system for Drupal. On a basic level, Triggers are like events in (for instance, creating a comment). Actions are then the responding procedure that Drupal takes based on that Trigger.
Actions
Creating actions is explained well in this article, Writing an Action for Drupal 6, and if you have the Pro Drupal Development: 2nd Edition book, there is a good chapter about creating actions.
Triggers
Triggers, on the other hand, have little documentation on how to create new ones. There are many built in triggers that provide a lot of events to attach actions to. But, when creating any critical modules, it is important to be able to set events so that logging and notifying can happen at the right points.
So, here is some example code for creating your own trigger.
Example
Permissions
This hook is not necessary to implement. But as I think it is good practice to implement specific permissions, I am including it. There is also a bug/problem with the trigger module, which restricts access control to triggers based on module name. Which I volunteered to patch and have not followed through with; my apologies. Nonetheless, we can work around this with hook_menu_alter().
/** * Implementation of hook_perm(). */ function example_perm() { }
Menu Alter
We want the permissions for administering our trigger to be unique, so we implement this hook.
/** * Implementation of hook_menu_alter */ function example_menu_alter(&$items) { // By default, the trigger system uses the module name // (i.e. example) as the access for the trigger // configuration menu item. we want to change that. $items['admin/build/trigger/example']['access arguments'] = array('administer our modules triggers'); }
Hook Info
This hook tells the trigger module about our triggers.
/** * Implementation of hook_hook_info */ function example_hook_info() { // Define triggers ), ), ), ); return $items; }
Attaching Actions
The next step is to define which actions will be able to be run with our triggers. We will add two actions that are already provided: the Message and Email actions. We don't want to step on any other trigger toes. If we are not careful, we will make it so that actions will only be allowed on our trigger. So, we implement some basic conditional logic.
/** * Implementation of hook_action_info_alter */ function example_action_info_alter(&$info) { // We want to add this modules's triggers to the email action } else { } // We want to add this modules's triggers to the message action } else { } }
Invoke Wrapper
This is unnecessary, but helps a lot. We want to make sure that all modules know whats going on when we call our trigger, so we create this wrapper function.
/** * Invoke trigger * * This function is a wrapper/helper for triggers/hooks * * @param $hook * string describing hook * @param $op * string describing operation * @return * boolean, true or false if successful */ function example_invoke_hook($hook = 'example', $op = null, $object = null) { // Perform hook invoke // Given the complexity of your module, you might already have hooks and might need to change this // Call trigger/actions return true; }
Calling Our Trigger
This is just a sample function where we call our new trigger.
/** * Some function */ // ... // We are doing stuff, and then BAM! trigger example_invoke_hook('example', 'our_new_trigger'); }
- Log in to post comments
Comments
alternative: rules Permalink
As an alternative you can add support for the rules module: http://drupal.org/project/rules
There adding new events (which you can trigger on) is much easier, see http://drupal.org/node/298549.
Added to DrupalSightings.com Permalink
Added to DrupalSightings.com
What FILE? Permalink
You do not state what file to put your code in and where to put that file. Please add this to your tutorial. Thanks
not in scope Permalink
How to create a Drupal module is not in the scope of this article, but check this tutorial out:
http://drupal.org/node/206753