Introduction to the Module Rules Introduction to the Module
Rules - https://www.drupal.org/project/rules 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 determine which entities are available to the rule. Some events also provide special conditions and actions. EVENTS “When” Defined triggers Determine which entities are available in the rule CONDITIONS Entities provide variables (e.g. fields). “If” Help narrow down/fetch specific entities ACTIONS Variables are used in conditions and actions. “Then” Can also be used to fetch entities
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 Condition: User has role(s) Authenticated user Action: Set a data value Field: account:[image-field]:file Value: image file ID (fid) Create actions Define conditions Choose event Determine rule type
Rules Breakdown 2 Components, Data Types, Parameters
Rules Breakdown 2—Components, Variables Events determine entities. Sub-rules Don’t require events Do require variables to be passed to them Allow you to add more conditions, form complex rule chains Entities provide variables. VARIABLES Variables are also used by components. The pieces of data the component will use Will be provided by the entity in the main rule Connections
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 Create components Choose event Determine rule type
Pick Your Pokémon—Component Creation Component type: Rule Set Condition: User has role(s) Action: Set a data value Field: account:[image- field]:file Value: image file ID (fid) Add components to main rule Create actions Define conditions Determine component type
Rules Breakdown 3 Lists, Loops
Rules Breakdown 3—Lists, Loops 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 Other Resources Introduction to Rules - https://drupalize.me/videos/introduction-rules?p=1157 Or Drupal 7 – All About Rules - https://www.drupal.org/node/1866108 Coding for Rules - https://drupalize.me/videos/coding-rules-overview?p=1152 Other Resources Test Drupal online - https://simplytest.me/ Pokémon images from Wikipedia