Hi,

I am trying to create a complex workflow with rules but I am facing an annoying issue. I try to create some component rules which can be fired by some reaction rules or by other component rules. The problem is that Components defined in the hook_default_rules_configuration function cannot be used in Components or Rules defined in this same hook, even if it is done in another module.

I made a little sample to illustrate this issue :

/**
 * Implements hook_default_rules_configuration().
 */
function test_default_rules_configuration() {
  $rules = array();
  
  $rule = rule(array());
  $rule->label = 'Rule Test';
  $rule->active = TRUE;
  $rule->action('drupal_message', array(
    'message' => 'test',
  ));
  $rules['test_rule'] = $rule;
  
  $rule = rules_reaction_rule();
  $rule->label = 'Réaction rule Test';
  $rule->active = TRUE;
  $rule->event('init')
    ->action('test_rule', array());
  $rules['test_reaction_rule'] = $rule;
  
  return $rules;
}

Attached screenshots show the result of this piece of code.

I cleared cache thrice, tried with all in the same module as above or in two different modules, well sorted using their weight.
I also tried to use hook_default_rules_configuration_alter to add actions after the Components creation and the save method of the Rule object.
The result is always the same : "Error : unknown action XXX"

What is particularly strange is that these components are aviable and working well with the UI.

Am I doing something wrong ?
Regards.

CommentFileSizeAuthor
rules.png6.61 KBduaelfr
components.png3.89 KBduaelfr

Comments

duaelfr’s picture

Category: bug » support
Priority: Major » Normal
Status: Active » Fixed

After digging a lot I found that rules were adding a "component_" prefix to component names.

So the working code is

/**
 * Implements hook_default_rules_configuration().
 */
function test_default_rules_configuration() {
  $rules = array();
  
  $rule = rule(array());
  $rule->label = 'Rule Test';
  $rule->active = TRUE;
  $rule->action('drupal_message', array(
    'message' => 'test',
  ));
  $rules['test_rule'] = $rule;
  
  $rule = rules_reaction_rule();
  $rule->label = 'Réaction rule Test';
  $rule->active = TRUE;
  $rule->event('init')
    ->action('component_test_rule', array());
  $rules['test_reaction_rule'] = $rule;
  
  return $rules;
}

I hope it could help someone

mitchell’s picture

Title: Cannot use Components defined in hook_default_rules_configuration » How to define components in hook_default_rules_configuration
Component: Rules Core » Documentation
Category: support » task
Status: Fixed » Active

Hi, I'm from the Rules Documentation Team, helping with an initiative to improve the completeness and consistency of Rules' documentation handbook.

This issue contains valuable information missing from the Rules handbook. The Default rules page, might be a good match for recording this.

(the formatting style is just me messing around, trying to start a new workflow of moving issues like these into the right direction)

duaelfr’s picture

Hi mitchell. Happy to read you.
Feel free to use my code samples if you think it can help anyone.
You can consider all my contributions to this issue and more generally in d.o as being released in the public domain :)

tr’s picture

Status: Active » Postponed

I have used hook_default_rules_configuration() to define rules components.

I have used hook_default_rules_configuration() to define reaction rules.

I have used hook_default_rules_configuration() to define BOTH rules components AND reaction rules in the same hook.

I have NOT tried to define a component then use it in a reaction rule in the same hook. But it is my experience that Rules does not ever rename the components or reaction rules that I define - they always show up in the Rules UI with the same machine_id as the array key I defined in hook_default_rules_configuration().

Regardless, if you can document otherwise, please edit the Rule documentation (link above) to reflect your knowledge. It does no good to wait for others to fix things when anyone has the ability to correct the documentation.

tr’s picture

Status: Postponed » Closed (cannot reproduce)