the slides - BYU Drupal Camp 2016

Rules
Introduction to the Module
Introduction
• Rules - https://www.drupal.org/project/rules
• Triggers (events) and actions, with conditions
• Coding with a UI
• More advanced version of the core Trigger
module
Uses
• Solve problems on your site
(custom modules)
• Create automated systems
(scheduled or triggered)
• Re-use functionality
• Integrate with VBO,
Commerce
Rules Breakdown 1
Events, Conditions, Actions
Rules Breakdown 1—Events, Conditions, Actions
EVENTS
• “When”
• Defined triggers
• Determine which entities are available in the rule
CONDITIONS
• “If”
• Help narrow down/fetch specific entities
ACTIONS
• “Then”
• Can also be used to fetch entities
Events determine
which entities are
available to the rule.
Some events also provide special
conditions and actions.
Entities provide
variables (e.g. fields).
Variables are used in
conditions and
actions.
You found a Pikachu!
Create a Basic Rule
You found a Pikachu!—Setup
GOAL
Display a Pikachu on authenticated users’ account page
MODULES
• Rules
• File Entity
SET UP
• Upload an image of Pikachu
• Add an image field to the user account page
You found a Pikachu!—Rule Creation
• Rule type: Rule
• Event: User account page is
viewed
• Better option: User is created
Determine rule type
Choose event
• Condition: User has role(s)
• Authenticated user
• Action: Set a data value
• Field: account:[image-field]:file
• Value: image file ID (fid)
Define conditions
Create actions
Rules Breakdown 2
Components, Data Types, Parameters
Rules Breakdown 2—Components, Variables
COMPONENTS
• Sub-rules
• Don’t require events
• Do require variables to be passed to them
• Allow you to add more conditions, form complex
rule chains
Events determine
entities.
Entities provide
variables.
VARIABLES
• The pieces of data the component will use
• Will be provided by the entity in the main rule
• Connections
Variables are also
used by components.
Pick Your Pokémon
Create a Layered Rule
Pick Your Pokémon—Setup
GOAL
Display the correct Pokémon on the user’s account page
SET UP
• Upload two more Pokémon images
• Create three roles
NOTE
This Rule assumes you have created a way for users to be sorted into roles – like
creating a field on the registration form that allows users to choose their team (role).
Pick Your Pokémon—Rule Creation
• Rule type: Rule Set
• Event: User account
page is viewed
• Condition: -• Components: Three
almost identical rule
components
Determine rule type
Choose event
Create components
Pick Your Pokémon—Component Creation
• Component type: Rule Set
Determine component type
• Condition: User has role(s)
• Action: Set a data value
• Field: account:[imagefield]:file
• Value: image file ID (fid)
Define conditions
Create actions
Add components to main rule
Rules Breakdown 3
Lists, Loops
Rules Breakdown 3—Lists, Loops
LISTS
• Multi-value fields
• Example: Taxonomy
LOOPS
• Affect each value in the list
• PHP For Loop
• Work well with components
Rules Breakdown 3—Examples from BYU Studies
On Journal
Publishing
• Loops + components
• When a journal is
published (a new
journal product is
created), expires
subscribers, sends
out emails.
On License Activation, Set Expiration
Issue Number
• Components
• When a customer buys or renews a
subscription, the expiration issue is set
Tips and Tricks
• Use “Entity is of type” and
“Entity has field” conditions to
use custom fields in actions
• Use “Fetch entity by ID” and
“Fetch entity by property”
actions to bring in another
entity or access entities that are
not provided by events
• If using Commerce, clone their
rules. Also, the Stock module
allows you to modify the Add to
Cart form with rules.
• Use the debug option!
• Configuration > Workflow >
Rules > Settings > Show debug
info (always [when testing – turn
in off after])
• To ensure that an entity has just
changed, use two conditions
• Data comparison
changed/updated status =
[whatever state]
• Data comparison unchanged
status NOT = [same state]
Resources
Tutorial Links
Tutorials
Drupalize.me
• Introduction to Rules - https://drupalize.me/videos/introductionrules?p=1157
• Or Drupal 7 – All About Rules - https://www.drupal.org/node/1866108
• Coding for Rules - https://drupalize.me/videos/coding-rulesoverview?p=1152
Other Resources
• Test Drupal online - https://simplytest.me/
• Pokémon images from Wikipedia