Book Image

Drupal Rules How-to

By : Robert Varkonyi
Book Image

Drupal Rules How-to

By: Robert Varkonyi

Overview of this book

Rules is what every Drupal site builder and developer has to use when creating event ñ action-based applications. The framework provides a highly flexible way to create sophisticated, condition-based functions any Drupal based system into an interactive application. Rules makes Drupal rule the CMS world."Drupal Rules How-to" is a practical, hands-on guide that provides you with a number of clear step-by-step exercises, which will help you take advantage of the real power of the Rules framework, and understand how to use it on a site builder and developer levelThis book demonstrates the power and flexibility of the Rules framework. It discusses the main aspects of the module both from the site builder and developer perspective, from basic and advanced Rule configurations using Events, Conditions, Actions and Components to getting familiar with the Rules API. You will also learn how to use additional modules together with Rules to further extend the possibilities of your Drupal system, such as Rules Scheduler to schedule the execution of your Rule configurations and Views Bulk Operations to execute Rule configurations on a view result list. The book also demonstrates the main API features that enable you to create your own Events, Conditions and Actions, provide new data types to Rules and execute your configurations programmatically
Table of Contents (7 chapters)

Using the Rules Scheduler (Must know)


In this recipe we create a rule configuration that sends a reminder e-mail to a user that hasn't logged in to the website for a week.

Getting ready

We need to make sure that the Rules Scheduler module is enabled.

How to do it...

  1. Create a new Action set component and provide a user object as a parameter.

  2. Add a new Action, System | Send mail and configure the various fields, set the To field to [user:mail], enter a subject and fill in the MESSAGE field with something such as Hey, you haven't logged in to our site for a week now....

  3. Add a new rule configuration and set the Event to User | User has logged in.

  4. Add an Action, Rules scheduler | Schedule component evaluation.

  5. Select the component which we created in step 1.

  6. Click on Switch to direct input mode and enter +7 days.

  7. Set a unique identifier to this scheduled component.

  8. Provide the account object to the component.

How it works...

In this example we wanted to send reminder e-mails to individual users, who haven't logged in to the website for a week. For that we've created a component (that executes the Send mail Action), which we use in our rule configuration as a scheduled component. In the rule configuration, we set the Event to User has logged in because we want to set the scheduled date to a week from the user's last login. Please note that you'll need cron running for the scheduler to work.

There's more...

Additionally, we would probably want to add an Action Delete scheduled tasks, using the same identifier we've used for the scheduled component and place it before the Schedule component evaluation Action.

This way we make sure that the scheduled date always gets updated when the user logs in and new reminders get scheduled.

Schedule UI

Rules Scheduler provides a user interface through the Views module which can be found at Configuration | Workflow | Rules | Schedule. This interface can be very useful as it displays all the components that are scheduled for execution. It is also a very useful tool for debugging scheduled components.