Tutorial

How to add a custom Action in Drupal 7 Rules and handle the variable from inside the module

Sometime you happen to find yourself in a situation, where you want to handle Rules inputs from your custom module. So in this short tutorial we will add a custom Rules action and handle the variable from inside your module.

Pridal/a lubo dňa Po, 03/09/2020 - 03:38
Add the following code to your_module.module file

Using the hook_rules_action_info() you can add your own Action which you will be able to select from inside Rules UI.

/**
 * Implementation of hook_rules_action_info().
 */
function devlubo_rules_action_info() {
  return array(
    'my_custom_action' => array(
      'label' => t('DevLubo - Custom Action'),
      'arguments' => array(
        'node_id' => array('type' => 'integer', 'label' => t('Node ID')),
      ),
      'provides' => array(
        'devlubo_custom_variable_output' => array(
            'type' => 'decimal',
            'label' => t('This will provide custom variable output'),
          ),
        ),
      'base' => 'devlubo_rules_alter_the_custom_variable',
      'module' => 'devlubo'
    ),
  );
}

Explaining the code in line by line fashion you can see the following:

Defining your custom action:

'my_custom_action' => array(

How will your action be listed in a select list in Rules UI:

'label' => t('DevLubo - Custom Action'),

Which arguments does your custom action take (in this example Node ID)

'arguments' => array(
    'node_id' => array('type' => 'integer', 'label' => t('Node ID')),
),

Which variables will the custom action provide to work with later in Rule execution

'provides' => array(
    'devlubo_custom_variable_output' => array(
       'type' => 'decimal',
       'label' => t('This will provide custom variable output'),
    ),
),

Custom function to work with the variable

'base' => 'devlubo_rules_alter_the_custom_variable',
Working inside the custom variable function

As you can see, we have defined in hook_rules_action_info() which function will handle the variable. So we can as an example set the value to custom number as a return value.

/**
 * Action: Set a variable to custom value.
 */
function devlubo_custom_variable_output($args, $element) {
  return array('devlubo_custom_variable_output' => "500");
}

Might interest you

Tutorial
There is a very handy function in Drupal 8 / 9, allowing developers refresh view when needed from javascript. The syntax goes by: $('.…
Tutorial
Does not your Contact Form 7 work properly anymore after plugin or theme update? Check your console to see any error messages. Today I…

Recommended

Article
32 views
For the past few days I am trying to comprehend why / how this blockchain even gained it'…
Tutorial
97 views
This sketch is quite easy, I used Arduino Nano with OLED 0.96″ display 128×64 resolution…
Tutorial
158 views
While working on a fairly complex website with very complex views setup, including tens…
Tutorial
14 views
In this case we have two options, either we use hook_user_presave() or we can create new…
Tutorial
15 views
When using Swiftmailer under Drupal 8 / 9 it automatically sets the headers for sender to…
Tutorial
7 views
Yes, IOS / Safari is the new internet explorer. Amount of time I spend on debugging…
Tutorial
43 views
There is a very handy function in Drupal 8 / 9, allowing developers refresh view when…
Tutorial
22 views
Often, when doing SEO checkups, SEO specialist come up with adding Schema.org…
Tutorial
175 views
I needed to test my contracts against USDC contract, specifically I needed ERC-721 mint…
Tutorial
85 views
If you are a newbie like I am and struggling with setting the proper MYSQL my.cnf config…
Tutorial
25 views
I had trouble to set this up properly, because documentation is quite misleading or often…
Article
72 views
As the title says, DO NOT in any circumstances install ANY bitcoin price extension to ANY…
Tutorial
278 views
This is (or should be) a working example of sending some Ether between two addresses.…
Module
45 views
This list was fetched from Zapper, with their /v1/token-list endpoint. Which you can…
Tutorial
143 views
In the last months I am being pretty much bombarded by my clients with asking what…