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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
The 
Tiny  
Book 
of 
Rules

Tiny Drupal Books #1

CreaTive Commons LiCense

The Tiny Book  
of Rules

This is Rules

Rules is a module usually used to automate 
actions on your Drupal site. Rules can react 
on events occurring on your site – such as a 
user logging in or a node being created – and 
perform customized follow-up actions such as 
redirecting to a certain page or setting field 
values. Optionally, Rules can evaluate condi-
tions before executing any actions.

Rules is also a framework to use by other 
modules, allowing them to evaluate user-con-
figured Rules components, such as action and 
condition sets, or expose data about variables 
and entities to other parts of Drupal.

3

This book was written by Johan Falk (Itangalo)  
with the help of Wolfgang Ziegler (fago),  
and layout by Leander Lindahl (leanderl). 

It is available under Creative Commons license at  
http://www.archive.org/details/TheTinydrupalBookOfRules 

Feel free to use, study, share and improve.

Published in March 2012

Rules in site building

Using actions

Rules actions are reactions that Rules may 
perform. Some actions return new data that 
Rules can work with – such as loading the most 
recent comment written by a user – but most 
actions only deal with the data already avail-
able to Rules.

Some common actions are:

•	Add a variable: This adds a new variable into 
your configuration, which then can be ma-
nipulated and used.

•	Calculate a value: This allows you to perform 

basic arithmetics with numbers.

•	Create a new entity: This asks for required 

data for an entity, and then creates it.

•	Fetch entity by id: This allows for fetching 

nodes and other entities by id.

•	Fetch entity by property: This allows for fetch-
ing all entities matching selected criteria, for 
example a selected field value. 

5

 
•	Show a message on the site: This displays an 

on-site message.

•	Publish/unpublish content.

•	Create or delete any URL alias.

•	Send an e-mail.

•	Set a data value (described below).

Actions can be combined in chains, which al-
lows for rules loading new objects and then 
acting on them. Contributed modules may pro-
vide more actions to Rules.

Data in Rules

Actions, and other plugins in Rules, act on 
specified data types. These data can be simple, 
such as text, integers and dates; or complex 
like nodes – which in turn have properties rep-
resented by data, such as node id (integer), title 
(text), or creation date (date). Complex data 
types may have properties which are complex, 
too – each node has, for example, an author 
represented by a user account (which in turn 
has a number of properties).

Some properties are writable, which means 
that you can alter these with Rules actions. The 
action Set a data value is particularly useful, 
and can be used to set any writable data.

For most action parameters, it is possible to 
switch between direct input, meaning manu-
ally written input, and data selection, a tool 
for drilling down into the nested properties of 
complex data types.

The data object site is always available in Rules, 
containing global site data such as the account 
for the acting user, the site name, and some 
other information.

6

7

Finally, it should be noted that field values are 
read- and writable only if Rules can be certain 
that the relevant entity has that field. If work-
ing with a node, for example, a field will only 
appear if conditions have been set that limits 
the rule to selected content types. (See using 
conditions for more information.) If the entity 
doesn’t have separate bundles – such as users – 
Rules can access the fields right away.

Using events

Rules provides a number of events – places in 
a Drupal page load that can be used to fire off 
actions. Rules that trigger on events are called 
reaction rules (in contrast to stand-alone rule 
components that must be triggered by other 
means – see the section about components).

Each event may provide some associated vari-
ables, which can be used in the rule configura-
tion. For example, the event After saving new 
content provides the saved content as a node 
variable while After updating existing content 
provides both the updated and original content 
as variables – thereby allowing comparison be-
tween the two.

A reaction rule may have more than one trig-
gering event. If they provide different vari-
ables, only the variables provided by all events 
may be used in the configuration, assuring that 
the rule may be evaluated properly regardless 
of which of the events is acting as trigger.

Using conditions

When a reaction rule is fired, or in other oc-
casions where rules are used, you have the 
options to check a number of conditions and 
abort any actions if the conditions aren’t met. 
Conditions can be pretty specific, such as Con-
tent is promoted to front page, Data value is 
empty or the comparison between two text 
strings – but the most used condition is called 
Data comparison. This one is used to compare 
two variables available to Rules, or to compare 
a data variable to a manually entered value 
– for example to check if a field has a certain 
value or if the node author is the same as the 
acting user.

Conditions can be logically grouped to create 
and and or groups. These groups may in turn 
contain other logical groups.

8

9

There are two conditions that are particularly 
important when working with fields: Content is 
of type and Entity has field. These conditions al-
lows Rules to know that an entity has a particu-
lar field, and subsequently makes the field data 
available in configuration. The condition Entity 
is of type has a similar role to provide entity-
specific data, but is used much less frequently. 
(You can also use the Data comparison condi-
tion to check the entity bundle.)

Lists and loops

Each data type declared to Rules automati-
cally gets a sibling data type, being a list of this 
particular data. Thus, Rules cannot only handle 
nodes, text strings and dates, but also lists of 
nodes, strings and dates. This is very useful 
when working with multiple-value fields, but 
also for a few multiple-value entity properties, 
such as the roles of a user.

Rules provides a few actions and conditions 
that are handy when working with lists: List con-
tains item, Add a value to a list and Remove a 
value from a list. The latter two can for example 
be used to add/remove a user from a multiplev-
alue user reference field.

The most useful thing with lists, though, is the 
ability to loop through them. By adding a loop 
to your set of actions, you have each action 
inside the loop being evaluated once for every 
item in the list you select. This can for example 
be used to send an e-mail to all users in a multi-
ple-value user reference field.

It is possible to use loops within loops, though 
you should be aware that evaluation may be-
come performance heavy if there are many 
items to loop over

Components

Reaction rules are the simplest and usually 
most common Rules configurations. But you 
can also create Rules components – re-usable 
condition sets, action sets, single rules or sets 
of rules. These components can then be called 
by reaction rules, saving you the work of re-
peating the same conditions in three different 
reaction rules.

Apart from avoiding repetitive work, compo-
nents may also be invoked by other modules. 
Views Bulk Operations, for example, can make 
use of Rules components as actions; Rules 

10

11

Scheduler can schedule the evaluation of any 
component for a future point in time; and with 
the help of Rules Bonus Pack, CTools can use 
condition components as access plugins in 
modules like Page manager and Panels.

Components are mainly created and edited like 
reaction rules – but they have no triggers. They 
also have configurable variables that are either 
parameters or provided (or both). Parameters 
are required input data for the component 
– variables that have to be specified when call-
ing the component. Provided variables are re-
turned to the caller after evaluation.

Components are very useful both in site build-
ing and coding.

Useful additional modules

There are a number of modules taking advan-
tage of Rules as a framework, or providing new 
actions, conditions and plugins. The most no-
table are:

•	Rules Scheduler. This module is included in 

the Rules project, and allows scheduling com-
ponents to be evaluated at a future point in 

time. The module can for example be used to 
schedule content deletion, upgrading of user 
accounts, or managing recurring e-mails.

•	Views Bulk Operations (VBO). This module 
extends Views and allows users to perform 
actions on selected rows. Apart from some 
base-level actions, Rules components may 
be used as actions by VBO. An equally useful 
feature of VBO is that it allows calling Views 
from Rules configuration, to load a list of enti-
ties listed by Views.

•	Rules Bonus Pack. This module serves as an 
experimental lab for new Rules functional-
ity, and provides a number of new actions 
and conditions. It also allows using condition 
components as CTools access plugins, as well 
as providing a new component type used for 
validating and modifying arguments (contex-
tual filter values) in Views.

•	Rules Link. This module can be used to attach 

links to entities, and firing rule sets when 
these links are clicked. Links can also be ren-
dered using Views, and link visibility can be 
controlled by condition sets.

12

13

Debugging Rules configuration

Rules is to a great tool for automation config-
ured in a point-and-click web interface. Just as 
when automating things with code, you will 
be happy for a debugger every once in a while. 
The Rules debugger is found at the Rules con-
figuration page – you will want to write out 
the debug messages directly on the web pages 
to easily follow what rules are executed, what 
conditions are evaluated TRUE or FALSE, and 
how long time each step takes.

If variables are not behaving as you expect, you 
will find the Rules action Debug value useful 
(provided by the Devel module). It prints out 
a krumo dump of a variable that you choose, 
making it available for inspection.

Coding for Rules
Normally, extensions of Rules are put into the 
file mymodule.rules.inc. This way, the file is 
only loaded when Rules is actually used on a 
page load, which is good for performance. In 
this section of the book, all code examples as-
sume that your module is called mymodule.

The following sections only contain the very 
basics of extending Rules. More detailed 
documentation can be found at the online 
documentation (http://www.drupal.org/
node/878720) and in rules.api.php included in 
the Rules project.

Writing conditions

Conditions are the least complex Rules exten-
sion. To provide new conditions to Rules, you 
need two things – a function returning TRUE or 
FALSE, and an implementation of hook_rules_
condition_info() to tell Rules about your condi-
tion. The info hook should return an array on 
the following form:

14

15

$conditions = array( 
  ‘mymodule_rules_condition_check_a_node_
value’ => array( 
    ‘group’ => t(‘My module’), 
    ‘label’ => t(‘My condition’),

    ‘parameter’ => array( 
      ‘var_1’ => array( 
        ‘type’ => ‘node’, 
        ‘label’ => t(‘Condition parameter 1 
(node)’), 
      ), 
      ‘var_2’ => array( 
        ‘type’ => ‘text’, 
        ‘label’ => t(‘Condition parameter 2 
(text)’), 
      ), 
    ),

  ), 
);

return $conditions;

The outermost key in the array above is by de-
fault the name of the callback function for the 
condition – more than one condition can be 
declared by adding more keys. As usual, make 
sure to prefix your functions with your module 
name. Especially note the property parameter, 
used for telling Rules which type of data the 
condition uses as input. There can be any num-

ber of parameters – including none – for a con-
dition callback. These are used by the callback 
function, which returns TRUE or FALSE:

function mymodule_rules_condition_check_a_
node_value($var_1, $var_2) { 
  // Condition logic goes here... 
  return $result;  
}

Writing actions

Conditions are very similar to conditions in 
their structure: a function carrying out the 
action, and an info hook implementation to 
tell Rules about it – in this case hook_rules_ac-
tions_info(). An important difference is that 
while conditions return TRUE or FALSE, actions 
may return new data objects to Rules. See ex-
ample below.

$actions = array( 
  ‘mymodule_rules_action_fetch_user_recent_
content’ => array( 
    ‘group’ => t(‘My module’), 
    ‘label’ => t(‘My action’),

    ‘parameter’ => array( 
      ‘var_1’ => array( 
        ‘type’ => ‘user’, 

16

17

        ‘label’ => t(‘Fetch latest content 
from this user’), 
        ‘save’ => TRUE, 
      ), 
    ),

    ‘provides’ => array( 
      ‘var_2’ => array( 
        ‘type’ => ‘node’, 
        ‘label’ => t(‘Most recent content’), 
      ), 
    ),

  ), 
);

return $actions;

Especially note the provides property, describ-
ing the variable(s) returned from the action. 
When writing the action callback, make sure it 
returns an array containing the variable(s) that 
Rules expects. The keys for the return array 
must match the keys in the info hook. The save 
property for the var_1 parameter means that 
Rules saves the passed variable if the action 
was successfully executed. Rules will normally 
wait with saving the changed entities and other 
data until execution is finished, to avoid mul-
tiple saves.

function mymodule_rules_action_fetch_user_
recent_content ($var_1) { 
  // Code to fetch a node object goes here... 

18

  return array( 
    ‘var_1’ => $account, 
    ‘var_2’ => $node, 
  ); 
}

Declaring and invoking events

Events are declared with an info hook – hook_
rules_event_info() – very similar to the previous 
ones. An important difference is the key used 
to declare event variables:

$events = array( 
  ‘mymodule_rules_event_id’ => array( 
    ‘group’ => t(‘My module’), 
    ‘label’ => t(‘My event’),

    ‘variables’ => array( 
      ‘var_1’ => array( 
        ‘type’ => ‘node’, 
        ‘label’ => t(‘Node provided by the 
event’), 
      ), 
    ),

  ), 
);

return $events;

Events are not invoked by their own callbacks. 

19

Rather, they are invoked by adding the rules_in-
voke_event() function somewhere in the execu-
tion cycle. The first parameter used with this 
function is the name of the event to call, and 
any subsequent parameters are being passed 
as variables to the Rules event:

// Placed in your custom module to react in 
the execution cycle 
rules_invoke_event(‘mymodule_rules_event_id’, 
$node);

An alternative to rules_invoke_event is the 
rules_invoke_event_by _args() function. It takes 
two parameters only, the first being the name 
of the event and the second being an array of 
variables to pass to the event.

Invoking components 
programmatically

Rules components are handy tools for allow-
ing site builders to customize their own actions 
and conditions. If you are writing a module that 
allows site builders to set custom conditions 
(or actions), you can let them set these up as 
Rules components and then evaluate selected 
components programmatically. This works in 
a way analogous to invoking events, by using 
rules_invoke_component().

The first argument is the (machine) name of 
the component, and any subsequent argu-
ments are variables sent to the component.

// Included somewhere in your custom module. 
$component_name = $settings[‘rules_compo-
nent_name’]; 
// Any variables provided from the component 
are returned from its evaluation. 
$result = rules_invoke_component($component_
name, $var_1, $var_2);

20

21

ample shows how a site-wide list of blacklisted 
words can be added to the site variable, to be 
used in Rules configuration.

function mymodule_entity_property_info_
alter(&$info) { 
  $info[‘site’][‘properties’][‘mymodule_black-
listed’] = array( 
    ‘type’ => ‘list<text>’, 
    ‘label’ => t(‘Blacklisted words’), 
    ‘description’ => t(‘Disallowed words on 
the site’), 
    ‘getter callback’ => ‘mymodule_get_black-
listed_words’, 
  ); 
}

function mymodule_get_blacklisted_words() { 
  return variable_get(‘mymodule_blacklisted’, 
array()); 
}

Extending and altering  
data types

Rules relies on information from the Entity API 
module to declare data types for each entity 
on your site. These are complemented by a 
number of other data types, which are (with  
machine names in parenthesis): date, decimal 
number (decimal), duration, formatted text 
(text_formatted), integer, text (text), text to-
ken (token), truth value (boolean), URI (uri) and 
watchdog log entry (log_entry). As previously 
mentioned, all data types and known entity 
types are also represented by lists being arrays 
of the data values.

The formatted text, watchdog log entry and all 
entities are complex data types, having a num-
ber of properties – each property being one 
of the declared data types. These properties 
can be altered and extended by modifying the 
properties array for the data type with help of 
hook_entity _property _info_alter() and hook_
rules_data_info_alter().

It should be noted that the globally available 
data is made available under the key site found 
in hook_entity _property(). The following ex-

22

23

Providing new entities and  
data types

Just as existing data types can be altered, new 
ones can be added. This is done by implement-
ing hook_rules_data_info(). An example of this 
is displayed below, where views are being intro-
duced as a data type in Rules.

Function mymodule_rules_data_info() { 
  $data_types = array( 
    ‘mymodule_view’ => array( 
      ‘label’ => t(‘view object’),  
      ‘wrap’ => TRUE,

      ‘property info’ => array( 
        ‘machine_name’ => array( 
          ‘type’ => ‘text’, 
          ‘label’ => t(‘Machine name’), 
        ), 
        ‘args’ => array( 
          ‘type’ => ‘list<text>’, 
          ‘label’ => t(‘Arguments’), 
          ‘setter callback’ => ‘entity_prop-
erty_verbatim_set’ 
        ), 
      ),

    ), 
  ); 
}

24

Especially note the wrap property, which 
makes Rules wrap the passed object and make 
it available for data selection. It is required for 
any complex data type. Two properties are 
declared in the property info array, which will 
make it possible to read these properties in 
Rules. Also, the args property has a setter call-
back – meaning that Rules cannot only read but 
also write the arguments of the view object.

It so happens that the properties’ machine 
names correspond directly to properties in 
the view object. If this wasn’t the case, a get-
ter callback would have been required to tell 
Rules how to read the property, just as it works 
for entity properties. Now the default callback 
entity _property _verbatim_get() is used, which 
like its setter sibling simply writes to the data 
object using the declared machine name as 
property name.

New entity types are declared using hook_en-
tity_info. Its usage is not documented here, 
but if your entities are declared properly for 
the Entity API modules, Rules will know about 
them too.

25

The Tiny Book of Rules is	tiny	and	it’s	
about	the	Rules	module	for	Drupal.	

Rules	is	a	module	usually	used	to	auto-
mate	actions	on	your	Drupal	site.	Rules	
can	react	on	events	occurring	on	your	site	
–	such	as	a	user	logging	in	or	a	node	being	
created	–	and	perform	customized	follow-
up	actions	such	as	redirecting	to	a	certain	
page	or	setting	field	values.	Optionally,	
Rules	can	evaluate	conditions	before	
executing	any	actions.

Rules	is	also	a	framework	to	use	by	other	
modules,	allowing	them	to	evaluate	user-
configured	Rules	components,	such	as	
action	and	condition	sets,	or	expose	data	
about	variables	and	entities	to	other	parts	
of	Drupal.

Tiny Drupal Books #1

CreaTive Commons LiCense

28