MJC SEO & Web Development Blog > Add Account Creation to your Webform with only an e-mail required, using the Webform Rules module (regardless of site registration settings)

Add Account Creation to your Webform with only an e-mail required, using the Webform Rules module (regardless of site registration settings)

Create a versatile alternative account registration system for Drupal powered by Webform and Rules, with zero lines of code

The power of the webform module allows sophisticated registration forms to be put together far more quickly than would be possible by modifying the standard system. Webforms and Rules can also easily be cloned, allowing the possibility of simple setup of a multi-faceted registration system as site-wide settings for user registration no longer apply – everything (sending e-mails, assigning roles etc) is handled by Rules.

Restrictions of Drupal’s built-in user registration system

Drupal sites allows one of these three options for user registration:

  • Registration Disabled
  • Registration Approved (with admin approval)
  • Registration Approved (no approval needed)

There is also a checkbox to select whether e-mail verification is required or not.

In my previous post on Drupal Commerce’s powerful integration with Rules, I explained a way to improve the anonymous user experience on completing an order, but it required the user registration settings to be set to the most lenient option “Registration approved, no approval needed, no e-mail verification needed”.

In the past when I have used this setting, sites are likely to be inundated with bot created accounts. To prevent this, I added a rewriterule from /user/registration back to /user and hid the “register” tab on the user page with CSS.

The side effect of this was to effectively lock registration to one of only two methods – via placing an order in the store, or via manual admin creation of accounts.

A need arose on a site configured like this for user registration functionality for a new ‘members only’ section. There are a number of ways I could have tackled this (the most obvious being a ‘subsite’), but I decided to see what was possible using Rules and Webform. I was very pleased with how it turned out and believe it can be useful for any Drupal site looking to make a departure from the standard user registration system.

Adding user registration to your Drupal Webform

You must install the Webform Rules module, which provides relevant events, conditions and data selectors to rules. You must be using version 4+ of Webform (which, though still an alpha release, is worth upgrading to for improved built-in error handling presentation on existing forms and built in ‘conditions’, which are similar to a stripped down version of rules conditions for use internally in the webform module). This guide also assumes you have Drupal Commerce installed, though you only need it in order to clone one if its rules.

Update November 2013
It turns out that the commerce_checkout module must be installed in order to have the “Entity exists by propery” condition used below available in Rules. This is an extremely useful condition and a patch has been made to add it to the Rules module, though at the time of writing it has not been committed to dev. If you wish to use this on a site without commerce_checkout, you’ll need to apply the patch, and make the edit to the patch to change the type to text (I will make a new patch with this done and add it to the issue).

  1. If you don’t have an existing form you wish to use, create your registration form. A password field is not required or accounted for in this guide, it is set by the user on first logging into their account from a link in e-mail sent to them. The only necessary field is ‘e-mail‘. You can set up the Webform with whatever other fields and functionality you like though.
  2. Make a clone of the rule from commerce, “Create a new account for an anonymous order”, calling it “Create a new account on Webform submission“.
  3. The first step is to delete the event “Completing the checkout process” (this will throw a bunch of errors on reloading the page), replacing it with a new event “After a Webform has been submitted“.
  4. Delete all the conditions except “NOT entity exists by property“.
  5. Add a new condition “Webform has name” and select your registration form.
  6. Move “send account e-mail” out of the loop. Select the type of account registration e-mail relevant. If you have already customised the relevant one, as in my case, just use one of the other options and alter the template to fit (in the configuration > people > account settings page).
  7. Delete the other actions in the loop to fetch the created account, and the loop itself (unless you wish to manipulate areas outside the user entity based on its data too).
  8. There will still be errors shown for most of the rules. This is because they are still using tokens from commerce. These need to be replaced with tokens from the Webform submission. These are formed similar to [data:email-value] or [data:email-value-raw], where ‘email’ is the key of your Webform component.
  9. (Optional) Create a new role for the users registering via this form.
  10. Add an action below “Create a new Entity” and above “Save entity”.  Set a data value [acount-created:roles] – select the role to assign user accounts created via this form from list.
  11. Finally, add an action “Show a message on the site“, last in the actions list – The Webform module does not know whether the rule was successful or not, so it goes to the confirmation page even if there was a problem in the rules evaluation. So we show a success message as part of the rule instead. I have not fully looked into the conditions aspect added to Webform 4 to see if there is any provision for integration in this area.
  12. Test your new Webform based registration form

Update November 2013

Stay tuned for a followup article on advanced entity manipulation based on submitted webform data using the rules_data_transforms module – including addressfields, times and dates, integer values and multiple value fields (e.g. checkboxes) – both input and output

One Comment

  1. SEO Expert
    Posted June 18, 2013 at 6:53 pm | Permalink

    Easy! Thanks for sharing.

Post a Comment

Your email is never shared. Required fields are marked *