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 PHP in Conditions and Actions (Should know)


This section explains how to use PHP input in Conditions and Actions.

In this simple example we'll display a message on the front page every Monday. To do that, we'll use a PHP input in our Condition to evaluate to TRUE if we're currently on the front page of our site, and if it's Monday today.

Getting ready

Enable the PHP Filter module on Drupal's module list page and assign relevant permissions if necessary. Take extra care as to whom you assign these permissions to, as the PHP input may cause security concerns so you probably don't want everyone on your website to be able to use it.

How to do it...

  1. Add a new rule configuration and set the Event to System | Drupal is initializing.

  2. Add a new Condition, Data | Data comparison and set it to site:current-date.

  3. In the DATA VALUE field set, click on Switch to data selection and enter site:current-date.

  4. In the PHP EVALUATION field, enter the Code value, as shown in the following screenshot, and save the Condition:

  5. Add a new Condition and set the handler to PHP | Execute custom PHP code and enter this code in the text area:

  6. Add a new Action, System | Show a message on the site and enter the following Value, as shown in the screenshot:

How it works...

In the first Condition, we compare the current date value to figure out what day it is today. In the PHP Evaluation field, we always receive the value of the selected field in the $value variable, which in this case is a timestamp of the current date. We're using this value in Drupal's format_date() function to return TRUE if it's Monday today.

In the second Condition, we're returning TRUE if the current page we're visiting is the front page of our website.

There's more...

PHP can be put to use in many other ways too. Some are described as follows:

Using PHP in Actions

We can also use PHP in Actions to execute functions, update database entries, and perform other tasks as required. To do that we can add an Action, Execute custom PHP code, and enter the PHP code we want to execute.

Best practice

Using a PHP input in Rules is a very effective way to create custom Conditions and Actions if we don't want to programmatically create new ones in our custom module (more on that in the Providing new Events, Conditions, and Actions (Become an expert) recipe in this book). However, there are a number of things we want to keep in mind:

  • Permissions

    It is highly advised that we don't let regular users use the PHP input filter, as it is a high security risk.

  • Never use delimiters

    We should never use the <?php ?> delimiters in our custom code. Rules takes care of that for us. If we use the delimiters in our Condition or Action, it won't work.

  • Always test on a development site

    Of course, it is advised that all Rules configurations are tested on a development site before using them on production sites. This is particularly valid for configurations that include the PHP code in Conditions or Actions. We always want to make sure we enter code without typos, execute the right database commands, or update the right user information.

    It is also advised that Debugging is turned on on our development site, that way we can save a lot of time testing our configuration.