Triggers in Drupal

To make a Drupal module that provides triggers you need to

  1. Use module_invoke_all at the point your code triggers an event.

    <?php
    module_invoke_all
    ('mymodule', $op, $arg1, $arg2, ....);
    ?>

    This basically defines a hook that your module provides - it has to be called after the name of your module because of a bug

  2. Define your modules implementation of this hook to run any actions registered for it.

    <?php


    function mymodule_mymodule($op, $arg1, $arg2...){
      
    $aids = _trigger_get_hook_aids('mymodule', $op);
      
    $context = array(
        
    'hook' => 'mymodule',
        
    'op' => $op,
        
    'arg2' => $arg2,
          (
    arg3 etc)
       );
      
    actions_do(array_keys($aids), $arg1, $context);

    }
    ?>

    You can wrap up as many arguments as you like in the context array, $op can be used to define related actions like the way the user module has op for insert, delete, update etc.

    $arg1 is passed by reference as the $object parameter to actions.

  3. Tell Drupal about your triggers by implementing hook_hook_info

    This adds the triggers on the triggers admin page

    <?php
    function mymodule_hook_info() {
      return array(
         
    'mymodule' => array(
           
    'mymodule' => array(
             
    'some_op' => array(
               
    'runs when' => t('Text to explain when the trigger runs'),
               ),
              
    'some_other_op' => array(
                
    'runs when' => t('more user text'),
               ),
             ),
           ),
       );
    }
    ?>

  4. Now you can use the admin interface to run actions when your triggers fire.

See Writing triggers (Drupal 6.x) for more details - I wrote this up as I found it helpful to approach the issue in reverse order to that page.

Tags

Post new comment

Got something to add - just enter a comment
all other fields are optional.

Your email address will not be published.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image without spaces, also respect upper and lower case.