after digging through rules source for a while I decided to ask.

I want to execute a rule programmatically by knowing its machine name. I'm aware of the fact, that it would be much more simple a task to create a component and invoke that, or issue the according event. But neither is what I want to do.

What I've been trying so long:

// Load the rule(s).
$rules = rules_config_load_multiple(array('my_rule'));

// Try to run the rule(s).
foreach ($rules as $rule) {
  $state = new RulesState();
  $rule->evaluate($state);
}

Though I can tell it does something, it does not really evaluate the rule ;-).

Any help or hint is appreciate.

Comments

attisan’s picture

could the following snippet be the way to go?

foreach ($rules as $rule) {
  $state = $rule->setUpState(array());
  $result = $rule->fire($state);
}
TR’s picture

Status: Active » Fixed

When in doubt, first look at the Simpletest cases to see how to programmatically create and invoke rules.

Here's how the test runs a rule that has a "node" parameter as input:

    $node = $this->drupalCreateNode();
    $rule = rules_config_load('rules_test_rule');
    $rule->execute($node);

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.