Reader small image

You're reading from  Drupal Rules How-to

Product typeBook
Published inNov 2012
Reading LevelIntermediate
PublisherPackt
ISBN-139781849519984
Edition1st Edition
Languages
Tools
Concepts
Right arrow
Author (1)
Robert Varkonyi
Robert Varkonyi
author image
Robert Varkonyi

Robert Varkonyi is a senior Drupal developer and author, originally from Hungary. He has worked with clients across the globe, such as NBC Universal and Ericsson. Robert is currently working with Tincan, a leading Drupal agency within the arts, cultural, and NfP sectors. He is also the author of Drupal Rules How-to.
Read more about Robert Varkonyi

Right arrow

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.

Previous PageNext Page
You have been reading a chapter from
Drupal Rules How-to
Published in: Nov 2012Publisher: PacktISBN-13: 9781849519984
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Robert Varkonyi

Robert Varkonyi is a senior Drupal developer and author, originally from Hungary. He has worked with clients across the globe, such as NBC Universal and Ericsson. Robert is currently working with Tincan, a leading Drupal agency within the arts, cultural, and NfP sectors. He is also the author of Drupal Rules How-to.
Read more about Robert Varkonyi