Triggers And Uninstalling Modules

Photo of Greg Harvey
Sat, 2008-12-13 16:01By greg

Note, Actions and Triggers are core in Drupal 6.x so this only applies to Drupal 5.x if you are using the Actions module as a 5.x contributed module:
http://drupal.org/project/actions

If you are using Triggers in Drupal 5.x then you have probably installed something like the Node Triggers module, which allows Actions to be triggered on node-related events (using hook_nodeapi()):
http://drupal.org/project/nodetriggers

Triggers and actions in Drupal are extremely useful. They allow you to perform automated tasks when a variety of different events occur in the system. Good examples are a node is created, a comment is edited, a module is installed, etc. etc. Using the Actions API in Drupal 6.x you can provide your own actions in your module which can then be fired by Triggers provided by the system.

By way of an example, I created a simple action which placed the node ID of a node in a CCK field. I then assigned this to a node insert trigger meaning whenever a new node is inserted in the database, immediately after my action is executed.

Cool stuff!

Here's the warning: if you write your own actions and assign them as triggers, whatever you do make sure you *unassign* your actions from any triggers before you uninstall your module. This is not handled automatically, so if you fail to do so you will end up with an orphan trigger trying to call a function that no longer exists... and the white screen of death! =(

In fact, I strongly advise that any module providing an action copies some portion of the code for unassigning actions from triggers in to the hook_uninstall() implementation, so you know for sure no triggers can be left set. At the moment it seems the only function for handling this in the core Trigger module is the admin form submit function, which is not ideal:
http://api.drupal.org/api/function/trigger_unassign_submit

For Drupal 5.x you'll need to examine the code of the trigger module you're using to understand if there is a function you can call for this or not.

There's probably scope in Drupal 7.x for a function to unassign all triggers belonging to a given module that can be called from hook_uninstall(), so contributed modules can easily and safely clear up after themselves... but that's for another day. For now, remember triggers and actions are great, but make sure you don't leave orphaned triggers behind when you uninstall modules. That goes for *all* modules - don't assume they uninstall actions gracefully by checking for triggers.