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" 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_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']);
}
?>









2 comments
1. Bart (6 July, 2009 - 15:19) says:
Just to let you know, _atsample_invoke_specific_page_trigger() seems to be missing.
2. Alex (6 July, 2009 - 15:51) says:
Yes, sorry for this, corrected by removing.
Post new comment