Example: Trigger

  1. drupal
    1. 8
    2. 6
    3. 7

Trigger definition example module.

Triggers and actions are a pair of special-purpose functions 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 able to enqueue actions. The trigger module provides the interface allowing us to associate certain actions with certain triggers.

Actions are functions intended 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 some 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 to 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 triggers and actions for Drupal. In this example we are providing two triggers:

  • A custom trigger, in its simplest form. We provide a page with a button. This button does nothing at all, but when you click (and submit the empty form), any actions you have configured will be executed.
  • A trigger which extends the capabilities of User triggers. This creates a new event which fires the first time a user ever logs in. In the module we will create it, and then provide a trigger for the administrator to be able to enqueue actions. They will be executed only the first time the user logs in the system.

See:

Creating Triggers

Writing Actions

Triggers and Actions in Drupal 6

Also see the Action Example.

See also

hook_trigger_info()

hook_trigger_info_alter()

Functions & methods

NameDescription
trigger_example_formTrigger example test form.
trigger_example_form_submitSubmit handler for the trigger_example_form().
trigger_example_helpImplements hook_help().
trigger_example_menuImplements hook_menu().
trigger_example_triggersomethingtriggersomething trigger: Run actions associated with an arbitrary event.
trigger_example_trigger_infoImplements hook_trigger_info().
trigger_example_trigger_info_alterImplements hook_trigger_info_alter().
trigger_example_user_loginImplements hook_user_login().
_trigger_example_first_time_loginTrigger function for "User first time login"

examples/trigger_example/trigger_example.module, line 8