Example: Action

  1. drupal
    1. 8
    2. 7

Creating actions in Drupal 7

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 also

trigger_example

hook_action_info()

Writing Actions in Drupal 6

Triggers and Actions in Drupal 6

Functions & methods

NameDescription
action_example_action_infoImplements hook_action_info().
action_example_basic_actionAction function for action_example_basic_action.
action_example_menuImplements hook_menu().
action_example_node_sticky_actionAction function for action_example_node_sticky_action.
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_validateValidates settings form for action_example_node_sticky_action(). Verifies that user exists before continuing.
action_example_unblock_user_actionAction function for action_example_unblock_user_action.
_action_example_pageA simple page to explain to the developer what to do.

examples/action_example/action_example.module, line 8