Last updated August 20, 2014. Created on April 11, 2013.
Edited by mradcliffe, dasjo, Jeff Veit. Log in to edit this page.

Rules Scheduler allows you to schedule the execution of a Component. While you can map the date of execution to a field, there's no way to calculate a date and pass it to the scheduler, but you can do this programatically.

Before using the code below, you will need to set up a Component in the Rules UI. You may need to set up parameters for the component if you want it to operate on an entity. For example you might set up a parameter named 'user_to_delete' of type User to pass to an action for blocking a user.

To schedule the execution of a component:

<?php
  $component
= 'machine_name_of_the_component';
 
$task_identifier = 'a_unique_task_identifier';
 
rules_action('schedule', array('component' => $component))->executeByArgs(array(
   
'date' => $timestamp,
   
'identifier' => $task_identifier,
   
// Add component parameters, prefixed with 'param_'
   
'param_x' => $x,
   
'param_y' => $y,
  ));
?>

Deleting a scheduled rule component

<?php
  rules_action
('schedule_delete', array('component' => $component))->executeByArgs(array(
   
'task' => $task_identifier,
  ));
?>

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

supriyarajgopal’s picture

Hi,
This seems to be exactly what I was searching for..but where do I include the above code?In .module file?Under which hook?

Thanking you.

Regards,

Supriya Rajgopal

supriyarajgopal’s picture

I found the answer..
You have to place this code in an appropriate hook.
Eg:- If you want to schedule the rule component when cron is run, you place this code in hook_cron()..or if you want it when a node is insert, then in hook_node_insert()
Just in case this helps anyone :)

Regards,

Supriya Rajgopal

Maestro232’s picture

I'm looking to essentially loop through any existing scheduled rules that have not yet fired and retrieve the time each is scheduled to fire. How would I do this?