View source  
  <?php
function trigger_example_help($path, $arg) {
  switch ($path) {
    case 'trigger_example':
      return '<p>' . t('Use this page to call hook_trigger_example() with the ping and pong operations. You can create actions to display a message or email the user on the <a href="@actions-url">Actions settings page</a> and assign these actions with the ping and pong events on the <a href="@triggers-url">Triggers settings page</a>.', array(
        '@actions-url' => url('admin/settings/actions'),
        '@triggers-url' => url('admin/build/trigger/trigger_example'),
      )) . '</p>';
    case 'admin/build/trigger/trigger_example':
      $explanation = '<p>' . t('Triggers are system events, such as when new content is added or when a user logs in. Trigger module combines these triggers with actions (functional tasks), such as unpublishing content or e-mailing an administrator. The <a href="@url">Actions settings page</a> contains a list of existing actions and provides the ability to create and configure additional actions.', array(
        '@url' => url('admin/settings/actions'),
      )) . '</p>';
      return $explanation . '<p>' . t('Below you can assign actions to run when certain comment-related triggers happen. For example, you could promote a post to the front page when a comment is added.') . '</p>';
  }
}
function trigger_example_menu() {
  $items['trigger_example'] = array(
    'title' => t('Trigger Example'),
    'description' => t('Provides a form to test the triggers.'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'trigger_example_form',
    ),
    'access callback' => TRUE,
  );
  return $items;
}
function trigger_example_hook_info() {
  return array(
    'trigger_example' => array(
      
      'trigger_example' => array(
        'ping' => array(
          'runs when' => t('A ping happens'),
        ),
        'pong' => array(
          'runs when' => t('A pong happens'),
        ),
      ),
    ),
  );
}
function trigger_example_ping() {
  $count = variable_get('trigger_example_pings', 0) + 1;
  
  module_invoke_all('trigger_example', 'ping', $count);
  variable_set('trigger_example_pings', $count);
}
function trigger_example_pong() {
  $count = variable_get('trigger_example_pings', 0);
  
  module_invoke_all('trigger_example', 'pong', $count);
  variable_set('trigger_example_pings', max(0, $count - 1));
}
function trigger_example_trigger_example($op, $count) {
  
  if (!module_exists('trigger')) {
    break;
  }
  
  if ($aids = _trigger_get_hook_aids('trigger_example', $op)) {
    
    $context = array(
      'hook' => 'trigger_example',
      'op' => $op,
      'count' => $count,
    );
    
    $dummy = new stdClass();
    foreach ($aids as $aid => $action_info) {
      actions_do($aid, $dummy, $context);
    }
  }
}
function trigger_example_action_info_alter(&$info) {
  if (isset($info['system_message_action']['hooks'])) {
    $info['system_message_action']['hooks']['trigger_example'] = array(
      'ping',
      'pong',
    );
  }
  if (isset($info['system_send_email_action']['hooks'])) {
    $info['system_send_email_action']['hooks']['trigger_example'] = array(
      'ping',
      'pong',
    );
  }
}
function trigger_example_form(&$form_state) {
  $form['help'] = array(
    '#type' => 'item',
    '#value' => format_plural(variable_get('trigger_example_pings', 0), 'There is only @count ping out there.', 'There are @count pings out there. Come on pong them back.'),
  );
  $form['ping'] = array(
    '#type' => 'submit',
    '#value' => t('Ping'),
  );
  if (variable_get('trigger_example_pings', 0)) {
    $form['pong'] = array(
      '#type' => 'submit',
      '#value' => t('Pong'),
    );
  }
  return $form;
}
function trigger_example_form_submit($form, &$form_state) {
  if ($form_state['values']['op'] == t('Ping')) {
    trigger_example_ping();
  }
  else {
    trigger_example_pong();
  }
}