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)

Executing Rules programmatically (Become an expert)


This recipe explains how to execute Actions, Rules, or Rule sets programmatically.

In this example, we'll create a simple component that sends an e-mail to the site administrators and execute this component programmatically.

How to do it...

  1. Add a new action set component, call it Send message to all admins:

  2. Add a new Action, System | Send message to all users of a role.

  3. Select administrators in the ROLES select box:

  4. Enter some text to the SUBJECT text field:

  5. Enter a message and save the component:

  6. Now that we've created our component, we can execute it in our custom module using rules_invoke_component():

    <?php
    rules_invoke_component('send_message_to_all_admins');
    ?>

How it works...

Components can be executed programmatically using the rules_invoke_component() function. The first parameter of the function will receive the machine readable name of the component, followed by any additional parameters that the component requires. This way we can execute complex Actions, Rules, Rule sets, Conditions, or additional plugins defined by other modules.

There's more...

The following section describes the execution of standalone plugins programmatically.

Executing standalone plugins

It's also possible to programmatically execute plugins without combining them into a component. We can, for example, execute a Condition in the following way:

<?php
$condition = rules_condition('user_has_role', array('role' => array('editor')));
$condition->execute($user);
?>