Saturday, October 02, 2010

Drupal: Create User Accounts Through the Webform Module

There are various ways to create user accounts through Drupal, but if you need to integrate it with a Webform (required: Webform module), maybe you have also stumbled through http://drupal.org/project/webform_register and to your surpirse, there is no module for download! So here are some helpful lines of code that could get you by that I found from the "Recent issues" page of the said module:

In the webform additional-processing field (PHP), I added this routine to create & notify a new user:
<?php
if (user_load(array('mail' => $form['#post']['submitted']['email'])) == FALSE) {

$newUser = array(
'name' => $form['#post']['submitted']['name'],
'pass' => user_password(), // note: do not md5 the password
'mail' => $form['#post']['submitted']['email'],
'status' => 1,
'init' => $form['#post']['submitted']['email'],
'roles' => array('4' => 'school')
);
$account = user_save(null, $newUser);
_user_mail_notify('register_no_approval_required', $account);
}
?>

Change the $form identifiers to your specific needs.

3 comments:

  1. Hey! I'm a Druapl Newbie but this is EXACTLY what I need! Any way I can ask for your help in integrating this code with our Join Us form?

    ReplyDelete
  2. you'll also need the webform_php module for this

    ReplyDelete