Thursday, Sep 27, 2012

Slides

Overview

  • What is the Rules module?
  • What are Events, Conditions and Actions?
  • Using the Data Selector
  • Components and Scheduling
  • Looping Actions
  • Real world examples
    • Using Rules to change the article node title to include the node id?
    • Using Rules to email the administrators when a new comment is created?
    • Using Rules with Views Bulk Operations to unpublish nodes?

What is the rules module?

  • Rules consist of Events, Conditions and Actions
  • Simply it looks for an event
  • When that event happens (under certain conditions)
  • It fires off an action

What is an event?

  • Events are linked to things happening on your site
  • Comment (viewed, created, deleted)
  • Node (viewed, updated, created)
  • System (cron, bootstrapped)
  • Taxonomy (create term/vocabulary)
  • User (log in/out, created, updated)
  • Extended by other modules (Profile, Feeds etc)

What is a condition?

  • Conditions check what the specifications of the event are
  • Data (comparison, empty)
  • Entity (field exists)
  • Content (load all fields)
  • Path (check path)
  • User (check role)

What is an action?

  • Do something
  • Data (add variable, set value)
  • Entity (create entity, fetch entity)
  • Node (publish content, make sticky)
  • System (redirect, send email, set message)
  • User (add role)

The data selector

  • Choosing the data you want to interact with
  • Direct input mode
    • Using tokens
    • [site:current-date]
    • [site:current-user]
    • [node:title]
    • [node:nid]
  • Data selection mode – Interactive browse through tokens

What are components?

  • A set of rules/actions
  • To be used either by Rules or other modules
  • Views Bulk Operations
  • Drupal Commerce

What is scheduling rules?

  • Set your rules to be activated based on time
  • Set nodes to be unpublished 2 weeks after publishing
  • Disallow comments 1 year after publish date

What are looping actions?

  • Do the action for multiple items
  • Send email to all users
  • Unpublish referenced items

Real World Examples

Using Rules to change the article node title to include the node id?

  1. Go to Rules (Administration > Workflow > Rules)
    • Add new rule
  2. Name it
  3. Event: Before Saving Content
    • Add Condition
  4. Content is of Type
  5. Value = Article
    • Add action
  6. Set a data value
  7. Data - “node:title”
  8. Value - “node:title – node:nid”
  9. Save

Using Rules to email the administrators when a new comment is created?

  1. Go to Rules (Administration > Workflow > Rules)
    • Add new rule
  2. Name it
  3. Event: After a comment is saved
  4. (+ Add Condition) Not needed for this rule! Fool!
    • Add action
  5. Send an email to all users of a role
  6. Select role - “administrator”
  7. Values – Subject, Message (can include tokens)
  8. Save

Using Rules with Views Bulk Operations to unpublish nodes?

  1. Go to Rules (Administration > Workflow > Rules)
  2. Select components
    • Add new component
  3. Rule set (or action if no conditions needed)
  4. Name it
  5. Data type – Node
  6. Label, and machine name
    • Add rule
  7. Name rule - “Unpublish node”
    • Add condition (if required)
    • Add action
  8. Unpublish Content
  9. Data: node
  10. Go to your view (or create a new one)
  11. Add Bulk operations: content
  12. Choose the required component “Unpublish nodes”
  13. Save
  14. It will then use the rule on all the ids you send to it!

Can Rules make me a sandwich? … Maybe?