(How to) Create Triggers in Drupal - Actions and Triggers

How to : Create Triggers in Drupal - Actions and Triggers

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() {
return array('administer our modules triggers');
}

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');
}

| Read More..

Courtesy:- zzolo.org