How to write custom actions and triggers in Drupal

This tutorial is based on very useful comment by bboldi. I'll copy his code and do some modifications to it to show the right way of firing configurable actions. Here is the code that will create three custom triggers "On every page", "On random page", "On specific page" and two custom actions - configurable and non-configurable. All of the descriptions i'll do in the code comments.

<?php
/**
* @author Bednarik Boldizsar
* @editor Aleksey Tkachenko
* @copyright 2008
*/
/*
* Implementation of hook_init()
*/
function atsample_init()
{
 
// Fire the triggers. We run this functions here because hook_init is running on every page request
 
_atsample_invoke_every_page_trigger(); // Trigger actions on every page request
 
_atsample_invoke_specific_page_trigger(); // Trigger actions on specific page request
 
_atsample_invoke_random_page_trigger(); // Trigger actions on random page request
}
/*
* Callback for trigger that fires actions assigned to him on every page request
*/
function _atsample_invoke_every_page_trigger() {
 
// Get array of assigned actions ids for this trigger
 
$aids = _trigger_get_hook_aids('atsample', 'on_every_page');
  foreach (
$aids as $aid => $action_info) {
   
// Run needed actions. Will work with non-configurable action
   
actions_do($aid, $message);
  }
}

/*
* Callback for trigger that fires actions assigned to him on random page request. Function works just the same as function for every page request, except it runs actions based on random seed.
*/
function _atsample_invoke_random_page_trigger() {
  function
make_seed()  {
    list(
$usec, $sec) = explode(' ', microtime());
    return (float)
$sec + ((float) $usec * 100000);
  }
 
srand(make_seed());
 
$randval = rand();

  if((

$randval % 3) == 0) {  
   
$aids = _trigger_get_hook_aids('atsample', 'on_random_page');
    foreach (
$aids as $aid => $action_info) {
     
$action = actions_load($aid);  // This is the main difference between original code and my improvement. Without it you will not get action to run properly
     
actions_do($aid, $object, unserialize($action->parameters));
    }
  }
}

/*
* Implementation of hook_info(). Not very good documentation on drupal.org for this. So, if you want custom tab for your triggers on triggers admin page (node/build/trigger) use this example
*/
function atsample_hook_info() {
  return array(
   
'atsample' => array(
     
'atsample' => array(
       
'on_every_page' => array(
         
'runs when' => t('On every page'),
        ),
       
'on_random_page' => array(
         
'runs when' => t('On random pages'),
        ),
       
'on_specific_page' => array(
         
'runs when' => t('On specific pages'),
        ),
      ),
    ),
  );
}

/*
* Implementation of hook_action_info().
*/
function atsample_action_info() {
  return array(
   
'atsample_show_message' => array(
     
'description' => t('Show a Hello World message!'),
     
'type' => 'atsample',
     
'configurable' => false,
     
'hooks' => array(
       
'atsample' => array('on_every_page', 'on_random_page'),
      )
    ),
   
'atsample_show_message_on_page' => array(
     
'description' => t('Show a specific message on a specific page!'),
     
'type' => 'atsample',
     
'configurable' => true,
     
'hooks' => array(
       
'atsample' => array('on_every_page', 'on_random_page'),
      )
    ),
  );
}

/*
* The simple action callback function
*/
function atsample_show_message(&$object,$context)
{
   
drupal_set_message('Hello world!');
}

/*
* Configurable action callback function
*/
function atsample_show_message_on_page(&$object,$context) {
  if (
$_GET['q'] == $context['page']) {
   
drupal_set_message($context['message']);
  }
}

/*
* Configurable action configuration form
*/
function atsample_show_message_on_page_form($context) {
 
$form['message'] = array(
   
'#type' => 'textfield',
   
'#title' => t('Message'),
   
'#description' => t('Message to show to user'),
   
'#default_value' => $context['message'] ? $context['message'] : 'Hello world',
  );
 
$form['page'] = array(
   
'#type' => 'textfield',
   
'#title' => t('Page'),
   
'#description' => t('Show message above on this page'),
   
'#default_value' => $context['page'] ? $context['page'] : 'node',
  );

  return

$form;
}
/*
* Submit callback to configurable action configuration form
*/
function atsample_show_message_on_page_form_submit($form, $form_state) {
  return array(
'message' => $form_state['values']['message'], 'page' => $form_state['values']['page']);
}
?>

Trackback URL for this post:

http://alexisyes.com/trackback/34

No comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • 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
Copy the characters (respecting upper/lower case) from the image.