action_example.module

Version 1.1 (checked in on 2010/12/13 at 17:44:36 by rfay)

Action definition example module.

Triggers and actions are a matched pair of Drupal features allowing some Drupal programming without using PHP. Using the appropriate action in a specific event, a site administrator can add new functionality. Examples are:

  • Send an email after a node is published or edited.
  • Display a message after a user has logged in.
  • Display a message and send an email after a node has been deleted.

A trigger is a special function which can enqueue actions. The trigger module provides the interface allowing us to associate certain actions with certain triggers.

Actions are the functions designed to be run by triggers.

A trigger should build the appropriate context for the action to be fired. Actions are very often grouped by functionality: examples are 'user', 'node', 'taxonomy'. When actions are grouped it is because they expect the same arguments. This way, you can enqueue as many actions understanding the 'user' object as you want.

Not all actions can be used in all triggers because they require different contexts. But some actions are generic enough that they do not require special objects in their contexts, and so can be used on every available trigger. This 'group' type is used by actions to be available for this trigger.

What are good candidates to be triggers? Any function can be a trigger, as long as it has the code to call the enqueued actions, but to make Drupal more extensible, you will find hooks (from Drupal and contributed modules) very good candidates. A trigger should build the arguments, ask for enqueued actions and run them. You may define a function being a trigger, and run it through a button in the front page, or you may prepare a trigger for a hook, and everytime that hook is fired, your trigger will be.

What are good candidates to be actions? any function is a possible action, the only problem is finding a trigger able to run it.

This module describes how to create actions for Drupal. In this example we are providing three actions:

  • A generic action that can be used in any trigger, which is the most basic example of an action.
  • An action which which extends the capabilities of User triggers, even if associated with node or comment events.
  • An action which extends the capabilities of node triggers, but limited to certain events only, and using a customizable option.

See:

@todo this is a drupal 6 link Writing Actions

@todo this is a drupal 6 link Triggers and Actions in Drupal 7

See also

hook_action_info() In addition, the Trigger Example provides detailed information on how to create a trigger.

Functions & methods

NameDescription
action_example_action_infoImplements hook_action_info().
action_example_basic_actionBasic example action.
action_example_menuImplements hook_menu().
action_example_node_sticky_actionPromote and set sticky flag action. This is the special action that has been customized using the configuration form.
action_example_node_sticky_action_formGenerates settings form for action_example_node_sticky_action().
action_example_node_sticky_action_submitSubmit handler for action_example_node_sticky_action.
action_example_node_sticky_action_validateValidate settings form for action_example_node_sticky_action(). Verify that user exists before continuing.
action_example_unblock_user_actionUnblock an user. This action can be fired from different trigger types:
_action_example_pageA simple page to explain to the developer what to do.