[Drupal Development] How to create Drupal Triggers and Actions - Part II
We had discussed about the basics of Drupal Actions and Triggers in our previous article. Have a look at the article How to create Drupal Triggers and Actions - Part I if you have not done so. In this article we are going to show you how to define Actions and Triggers in code.
In this part I show sample code showing how to define triggers and triggering that for assigned Actions.
Defining Triggers
A default Drupal installation comes with some predefined Triggers. We can also define custom Triggers in our modules. When proceeding to define the Triggers, our first job is to implement the hook - hook_trigger_info(). The function needs to return a array containing three levels associative arrays. Consider the code sample below:
function mymodule_trigger_info() {
return array(
'user' => array(
'user_first_time_login' => array(
'label' => t('After a user has logged in for the first time'),
),
),
'trigger_example' => array(
'triggersomething' => array(
'label' => t('After the triggersomething button is clicked'),
),
),
);
}
(Code samples in this modules taken from the trigger_example module available at Drupal web site and modified accordingly)
In the above code, two custom Triggers are defined. The First level array key indicates which category of Triggers are specified under it. The key is the module name that defines the Trigger but we can use other module names like 'node', so it will come under that category.
The second level key is the hook name of the trigger within that array you will define the label for that trigger which will be visible to the user.
Finally we will initiate a request to perform all Actions associated with the trigger, using something like
// Ask the trigger module for all Actions enqueued for the 'triggersomething' trigger.
$aids = trigger_get_assigned_actions('triggersomething');
// prepare a basic context, indicating group and "hook", and call all the
// Actions with this context as arguments.
$context = array(
'group' => 'trigger_example',
'hook' => 'triggersomething',
);
actions_do(array_keys($aids), (object) $options, $context);
Then all Actions assigned by admin for this particular trigger will be called and executed. The second and third arguments of the function call actions_do() will be passed to first and second parameters of the action function(s).
Defining Actions
You can also create custom Actions. The first step in creating Actions is to declare them by implementing hook_action_info() as shown below.
function mymodule_action_info() {
return array(
'mymodule_basic_action' => array(
'label' => t('Action Example: A basic example action that does nothing'),
'type' => 'system',
'configurable' => FALSE,
'triggers' => array('any'),
),
'mymodule_advanced_action' => array(
'type' => 'node',
'label' => t('Action Example: An advanced action that needs configuration before using'),
'configurable' => TRUE,
'behavior' => array('changes_property'),
'triggers' => array('node_presave', 'node_insert', 'node_update'),
),
);
}
With the above code we are declaring two Actions one is basic and another is advanced. Implementing the basic Action is simple, we just create a function for that:
function action_example_basic_action(&$entity, $context = array()) {
// Function code goes here...
}
To implement advance Actions we have to create configuration form along with its validation and submit functions.
function action_example_node_sticky_action_form($context) {
// Form code...
}
function action_example_node_sticky_action_validate($form, $form_state) {
// Form validation code
}
function action_example_node_sticky_action_submit($form, $form_state) {
// Form submission handler
}
function action_example_node_sticky_action($node, $context) {
// Action code
}
The form function and its validation and submission handlers are like normal Drupal forms, but the form function will receive only one parameter called $context, which will contain the configuration data already saved if anything has been configured before. Whenever the admin tries to configure the advance action this form will be show to him.