Using Rules Scheduler programatically

Last updated on
30 November 2019

Drupal 7 will no longer be supported after January 5, 2025. Learn more and find resources for Drupal 7 sites

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 enable the rules_scheduler submodule (ships with 'rules' module) and 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:

  $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

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

Help improve this page

Page status: No known problems

You can: