Last updated 4 September 2014. Created on 16 August 2009.
Edited by khanz, westie, Uersu, klausi. Log in to edit this page.

An updated tutorial for Drupal 7 can be found at https://www.drupal.org/node/1027546

This tutorial will give you an example how to use the Rules Forms module.

We want to have a workflow on our story content type: Content should be created as draft, then marked as "Needs review", then set to "Reviewed" and finally it should be "Published". Content can also be marked as "Needs work" to indicate that it is not ready yet. There will be 2 user roles: Editors, that create and review content, and Publishers that are allowed to publish the content.

Preparations

  • At first you have to download and install the Rules module, the CCK module and the Token module.
  • Go to "admin/build/modules" on your site and activate "Content", "Content Permissions", "Option Widgets", "Text", "Rules", "Rules Administration UI", "Rules Forms", "Rules Scheduler" and "Token" modules.
  • Go to the configuration page of the story content type ("admin/content/node-type/story"), deactivate the checkbox "Published" and click "Save content type" (we do not want to publish content by default, we will do publishing rule-based).

Create workflow CCK field

  • Go to the "Manage fields"page of the story content type ("admin/content/node-type/story/fields").
  • Fill out a new field with "Workflow" as label, "workflow" as machine readable name, select "Text" as data type and select "Select list" as form element.
  • Click "Save" and you get a detailed form, where you set the "Required" checkbox and the allowed values to "Draft", "Needs Review", "Reviewed", "Needs work" and "Published" (one item per line).
  • Hit "Save field settings".

Create user roles "Editor" and "Publisher"

  • Go to the user roles page ("admin/user/roles").
  • Fill out the empty text field with "Editor" and click "Add role".
  • Click "edit permissions" of the Editor role and grant the following minimum permissions:
  • edit field_workflow
  • view field_workflow
  • access content
  • create story content
  • edit any story content
  • Do exactly the same for the "Publisher" role.
  • Go to the users page ("admin/user/user"), edit users and assign them editor or publisher roles, when appropriate.

Create rules to publish the content

We want to publish the content, if a publisher sets the CCK field to "Published".

  • Go to the triggered rules page ("admin/rules/trigger") and click on "Add a new rule".
  • Fill out the label with "Publish story" and select "Content is going to be saved" from the event box.
  • Hit "Save changes" to create the rule.
  • On the resulting page click "Add a condition" and select "Content has type".
  • Click "Next", select "Story" from the content types and click "Save".
  • Add another condition: "User has role(s)". Click "Next", check the role "Publisher" and click "Save".
  • Add another condition: "Field has value". Click "Next", select "field_workflow" as field, click "Continue". Select "Published" from the Workflow select box and "Save".
  • Now add an action: "Publish content". Click "Next" and then "Save".

Enable form events

We want to display an error message to users of the editor role when they want to publish content (only publishers are allowed to do so). Therefore we need Rules form support and we need to activate events on the "Create story" form.

  • Go to the Rules form events page ("admin/rules/forms")
  • Check "Enable event activation messages on forms" and "Display form element IDs" and click "Save settings".
  • Go to to the "Create story" form ("node/add/story").
  • You should see a drupal message saying "Activate events for story_node_form", click the link "story_node_form" and confirm the activation on the following page.

Create a rule to display a form error to editors

  • Go to the triggered rules page and create a rule with the label "Display form error to editors" and select "Story node form is being validated".
  • Add a condition "User has role(s)" and select "Editor".
  • Add a condition "Form element has value", enter "field_workflow" into the form element ID field and "Published" into the value(s) field.
  • Note: you can lookup form element IDs on the "Create story" form, make sure that you have enabled "Display form element IDs" from the Rules Forms settings (admin/rules/forms).
  • Add an action "Set a form error", enter "field_workflow" into the form element ID field and "Only publishers are allowed to publish content" into the message field.

Create a rule to set unpublished content automatically to "Needs work" after 2 weeks

Now we want to set the workflow to "Needs work", if there has been no activity in 2 weeks. We will use Rules Scheduler for that (have a look at Tutorial 2 that deals with a similar issue).

  • Add a rule set (admin/rule/rule_sets), label "Set workflow to needs work", machine name "set_workflow_needs_work".
  • Specify an argument, type "Content", label "Content", machine name "node" and click "Save".
  • Click on the newly created rule set and add a new rule with label "Set workflow to needs work".
  • Add a condition "Field has value", select "field_workflow", select "Published" as Workflow and check the "Negate" box.
  • Add an action "Populate a field", select "field_workflow" as field and select "Needs work" as Workflow.
  • Got to the triggered rules page and add a new rule, label "Schedule Needs work after 2 weeks" and event "After creating new content".
  • Add a condition "Content has type" and select "Story".
  • Add a condition "Field has value", select "field_workflow", select "Published" as Workflow and check the "Negate" box.
  • Add an action "Schedule Set workflow to needs work", insert "workflow [node:nid]" into the identifier field and "+2 weeks" into the scheduled evaluation date field.
  • Go to the triggered rules page, clone the new rule and change the event to "After updating existing content" (we also want to affect updated content).

Create rules to send an e-mail to editors

We want to send an e-mail to all editors if content is created or the workflow state has changed.

  • Go to the triggered rules page and add a new rule, label "Send e-mail to editors on creation" and event "After saving new content".
  • Add a condition "Content has type" and select "Story".
  • Add an action "Send a mail to all users of a role" and select "Editor" as role.
  • Enter a subject like "New content: '[node:title]" and a
    message like "New content has been created by [author:user] and the workflow is set to [node:field_workflow-raw]. Link: [node:site-url]/node/[node:nid]".
  • Go to the triggered rules page and add a new rule, label "Send e-mail to editors on workflow change" and event "Content is going to be saved".
  • Add a condition "Field has changed" and select "field_workflow" as field.
  • Add an action "Send a mail to all users of a role" and select "Editor" as role.
  • Enter a subject like "Workflow on '[node:title]' has changed" and a message like "The workflow has changed from [node_unchanged:field_workflow-raw] to [node:field_workflow-raw]. Link: [node:site-url]/node/[node:nid]".

Create a rule to send an e-mail to publishers

We want to send an e-mail to all publishers if the workflow state is set to "Reviewed" (we do not want to send them mails on every workflow change).

  • Go to the triggered rules page and add a new rule, label "Send e-mail
    to publishers" and event "After saving new content" (we clone the rule later for updated content).
  • Add a condition "Content has type" and select "Story".
  • Add a condition "Field has value", select "field_workflow" and select "Reviewed" as Workflow.
  • Add an action "Send a mail to all users of a role" and select "Publisher" as role.
  • Enter a subject like "'[node:title]' has been reviewed" and a
    message like "Content has been marked as reviewed. Link:
    [node:site-url]/node/[node:nid]".
  • Got to the triggered rules page, clone the new rule and change the
    event to "After updating existing content" (we also want to affect
    updated content).

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

Jonathan Tan’s picture

Hi, the workflow is great. But there's one problem, the publishers cannot view the unpublished content of the editors. This is the default drupal permissions setting, where only the author of unpublished contents and the node administrators can view unpublished contents.

Any guide on how to solve this?

eriktoyra’s picture

For other users to be able to see an unpublished node they require the "administer nodes" permission. This is normally something that one do not wan't to spread out too widely in an organisation. I have just setup a similiar process using modules Revisioning, Workflow and Module Grants. Module Grants allows you to view unpublished content without having the "administer nodes" permission.

aiwata55’s picture

thank you very much for the info about Module Grants!

pria’s picture

Nice.. This is what I'm looking

What i should change if i run in localhost?
Of course in localhost i can't send an e-mail..
Can an email change with an confirmation that the story has been edited..

Thx.

monti’s picture

thanks for the great tutorial !

i have just finished my first ever rules & action task according to the tutorial.
it didn't work for me at first, and thought I just might add a note for others, who may experience the same.

so - if your user account is granted with both roles, it won't work.

I usually have drupal permissions set in role levels - i.e: if level-b is permitted to do more than level-a, i just give level-b the additional permissions, and assign the user both level-a and level-b roles.

the rule that checks whether the user is an editor or not in order to send an error message, doesn't care (rightfully) what other roles are given...

after re-order all permissions and removing the editors role from all publishers, it worked as it should.

thanks !!

aac’s picture

Thanks for a nice tutorial.

---~~~***~~~---
aac

robonoob’s picture

This help me a lot in understanding the power of Rules. However, I have a twisted scenario and I don't know how to do it.

"Editor / Publisher want to have option to receive email notification"

I met a publisher who want to view all pending content via login, instead of being notified via email all the time.

While I can do the following

  1. Build a view for pending content
  2. Add the notification option by creating a content profile page on the fly,

I cannot add condition to a rule which check if the content author want to receive email first (which is specified in author's content profile) before sending email notification.

How can this be done?

jweedman’s picture

Exactly what I was looking for... to the "T". I just needed to change content type from 'story' to my content type, and I was able to follow step by step. Thanks a million!

mcartmel’s picture

Hi all,

Is there any way to remove the workflow options that are not applicable to the 'editor' user?

jclaussen’s picture

I think bullet point #5

• Go to the triggered rules page and add a new rule, label "Send e-mail to editors on workflow change" and event "Content is going to be saved".

The event state should be changed to "after updating existing content". Otherwise node_unchanged value will be same as node.

Prom3theus7’s picture

I set an action to send an email to an arbitrary email that I define. Looking at recent log entries for my site, I see that the email has been sent, but neither of the email addresses I plugged into the action are receiving the emails. Any suggestions? Much appreciated.

Prom3theus7’s picture

Got it working!

Prom3theus7’s picture

So, I put in the company name instead of the company email in the sender field. Whoops!

mghatiya’s picture

How to do this in Drupal 7?

qasimzee’s picture

I have exactly the same question.

--
Qasim Zeeshan
http://qasimzeeshan.com