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)
  2. + Add new rule
  3. Name it
  4. Event: Before Saving Content
  5. + Add Condition
  6. Content is of Type
  7. Value = Article
  8. + Add action
  9. Set a data value
  10. Data - “node:title”
  11. Value - “node:title – node:nid”
  12. Save

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

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

Using Rules with Views Bulk Operations to unpublish nodes?

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

Can Rules make me a sandwich? … Maybe?