[Drupal] How to bring custom rules action in Drupal

| | 2 min read

In this article I will introduce you to programatically custom actions for Drupals' Rules module. Updating your events or actions in rules can benefit you widely later, when you start to reuse the rule in the Drupal site or even port them on other Drupal sites.

Following steps to create a custom rules action in Drupal

  • create a basic custom module in your Drupal site installation.

    No need of any codes on .info or .module files, but create a file "my_module.rules.inc" which will governs the code for your rules' action.

      /**
     * Implement hook_rules_action_info().
     */
    function my_module_rules_action_info() {
      return array(
        'my_module_rules_action_example' => array(
          'label' => t('example'),
          'group' => t('Custom'),
          'parameter' => array(
            'string' => array(
              'type' => 'text',
              'label' => t('label for example'),
              'description' => t('Description text for the field'),
            ),
            'length' => array(
              'type' => 'integer',
              'label' => t('length descriptions'),
              'description' => t('Description text for the field.'),
            ),
            'algorithm' => array(
              'type' => 'text',
              'label' => t('Algorithm'),
              'description' => t('Description text for the field.'),
              'options list' => 'my_module_example',
              'restriction' => 'input',
            ),
          ),
          'provides' => array(
            'example_name' => array(
              'type' => 'text',
              'label' => t('example label'),
            ),
          ),
        ),
      );
    } 
    
  • Implement "hook_rules_action_info" and add our own action to it.Here we describe the parameters to be passed to the rules action, which will be passed to the action callback.
  • Next we also pass parameter "algorothm" with option list pointing to function to make the implementation simple.
  • The last parameter "provides" specifies rules what machine name should be provided to the rules.
  • The last part of the code is the action callback. Here we return the function according to our condition parameters.
  • Next clear your cache in order for Drupal to add new rules action. Hope the above helps :-)