Robin van Emden's blog on things Drupal
Robin van Emden's blog on things Drupal
We regularly need to create workflows that enable anonymous users to add content to one of our Drupal sites. Often it is desirable to make anonymous users confirm the nodes they post by following a generated 'secret' link they have received by email.
Thanks to the indispensable Rules and Flag modules this can be accomplished without ever leaving the administration area of a Drupal site. Now don't misunderstand me. I love to code. But the Rules module often offers a faster and more easily maintainable way to create complex processes. It also keeps Drupal installs relatively clean, since Rules is able to sit in for a whole bunch of contrib modules.
But let's get back to work on the anonymous email confirmation process! I do assume some basic experience with Rules. For a first introduction I can do no better then refer you to the great series of Rules tutorials by Johan Falk at Node One.
My screencasts below are of a Drupal 6 install. I had to recreate the rules for D7 last week, follows the same logic as the D6 version *. An export of both D6 and D7 rules are attached. Both have been exported using a content type called "Complaint" (machine name "complaint") - different from the content type name seen in the screencasts.
1. Download and enable the necessary modules
Download and enable the following modules:
- email (for the email field)
- rules_forms (only necessary for D7)
- pathrules (only necessary for D6)
2. Create the Content Type you want anonymous users to submit
Create the relevant Content Type.
Add a regular text field to the Content Type named "random" (do not hide this field under the "Manage Display" tab, this would mess with the first rule we will create - I will hide the Random form field later on using a form rule).
Add an e-mail field called "email".
Set the permissions for the content type, anonymous users should be able to create this particular content type, the "random" field should be editable and viewable by anonymous users.
3. Add a new node Flag
Add a new Flag at the flag administration page. Make it of type "nodes", set it to "global" and assign it to the Content Type created in step 2.
It's as easy as one, two, three!
5. First rule: Send e-mail on submit IF anonymous user
The first rule checks if someone who tries to submit a the relevant content type is an anonymous visitor of the site. If that is the case, it will generate a random string (borrowing the Drupal function user_password()), add it to the "random" field, save the content type and mail the user (who entered an e-mail address in the form) a link that is a combination of the content type's URL and the generated string.
Relevant code :
return array(0 => array('value' => user_password()));
6. Second rule: Flag content as visible when correct URL from mail
The second rule checks if the value contained in the link received by the anonymous user conforms to the hidden saved value of the random field.
7. Third rule: If content NOT flagged hide for anonymous user
The third rule is simple: if an anonymous user tries to view content that is not flagged visible, redirect the user to the front page.
9. The proof of the pudding
If everything went well, an anonymous user should receive the following mail on submitting our specially prepared content type:
Click the link - and our previously hidden content is visible! It worked!
The fourth rule makes sure that content added by logged in users is automatically be made visible. The fifth hides the random entry field on the submit page of our content type. These last two rules are complementary, so I did not include screencasts for them. All rules for both D6 and D7 versions of Rules are attached below (remember: content type "Complaint", machine name "complaint").
* Though I had to remember to set the correct user for my flag rule to make it fire for anon users - the user "on whose behalf to flag" does not need to be the currently logged in or acting user in D7 Rules. You can specify any existing user that has sufficient rights to flag. Just switch to "direct data selection" and enter a uid of a user with sufficient rights to set a flag.