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

Article
As the title says, DO NOT in any circumstances install ANY bitcoin price extension to ANY of your browsers. Why? I got malware by this…
Module
This list was fetched from Zapper, with their /v1/token-list endpoint. Which you can effectively try yourself at Swagger UI website. But I…

Recommended

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