This recipe explains how to execute Actions, Rules, or Rule sets programmatically.
In this example, we'll create a simple component that sends an e-mail to the site administrators and execute this component programmatically.
Add a new action set component, call it Send message to all admins:
Add a new Action, System | Send message to all users of a role.
Select administrators in the ROLES select box:
Enter some text to the SUBJECT text field:
Enter a message and save the component:
Now that we've created our component, we can execute it in our custom module using
rules_invoke_component()
:<?php rules_invoke_component('send_message_to_all_admins'); ?>
Components can be executed programmatically using the rules_invoke_component()
function. The first parameter of the function will receive the machine readable name of the component, followed by any additional parameters that the component requires. This way we can execute complex Actions, Rules, Rule sets, Conditions, or additional plugins defined by other modules.
The following section describes the execution of standalone plugins programmatically.
It's also possible to programmatically execute plugins without combining them into a component. We can, for example, execute a Condition in the following way:
<?php $condition = rules_condition('user_has_role', array('role' => array('editor'))); $condition->execute($user); ?>