Example: Action

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.

Writing Actions in Drupal 6 Triggers and Actions in Drupal 6

See also

Example: Trigger

hook_action_info()

Parent topics

File

action_example/action_example.module, line 8

Functions

Title Sort descending File name Summary
action_example_action_info action_example/action_example.module Implements hook_action_info().
action_example_basic_action action_example/action_example.module Action function for action_example_basic_action.
action_example_menu action_example/action_example.module Implements hook_menu().
action_example_node_sticky_action action_example/action_example.module Action function for action_example_node_sticky_action.
action_example_node_sticky_action_form action_example/action_example.module Generates settings form for action_example_node_sticky_action().
action_example_node_sticky_action_submit action_example/action_example.module Submit handler for action_example_node_sticky_action.
action_example_node_sticky_action_validate action_example/action_example.module Validates settings form for action_example_node_sticky_action().
action_example_unblock_user_action action_example/action_example.module Action function for action_example_unblock_user_action.
_action_example_page action_example/action_example.module A simple page to explain to the developer what to do.

Classes

Title Sort descending File name Summary
ActionExampleTestCase action_example/action_example.test Default test case for the action_example module.