Drupal 7 - Customize The Default User Registration Form

Firstly, in your theme's root directory add the following to template.php. If the file does not exist, create it.

function YOURTHEME_theme(&$existing, $type, $theme, $path){
  $hooks = array();
   // Make user-register.tpl.php available
  $hooks['user_register_form'] = array (
     'render element' => 'form',
     'path' => drupal_get_path('theme','YOURTHEME'),
     'template' => 'user-register',
     'preprocess functions' => array('YOURTHEME_preprocess_user_register_form'),
  return $hooks;

function YOURTHEME_preprocess_user_register_form(&$vars) {
  $args = func_get_args();
  $form_state['build_info']['args'] = $args; 
  $vars['form'] = drupal_build_form('user_register_form', $form_state['build_info']['args']);

Account fields

Account fields are rendered following the pattern of the two required fields, 'name' and 'mail':

print render($form['account']['name']);
print render($form['account']['mail']);

Profile2 Support

To load Profile2 fields into the user registration form manually, use this pattern:

print render($form['profile_theprofile_name']['field_thefield_name']);

Also, I make use of field group module (here) with Profile2 and noticed that I didn't need to include anything about the field group a Profile2 field is in to render that field.

Drupal 7 - Create a custom User Registration form with FAPI D7

There are many reasons to create a custom User Registration form in Drupal, so I won't get into the "Why?" of it. This isn't a tutorial about necessarily how to use FAPI, but rather a particular way to accomplish something with FAPI. I assume when writing this that you understand hook_form() and the basics of putting your own module together.

Essentially we start with FAPI and hook_form() to build out the form itself in our module:


function foo_user_register_form($form, &$form_state){
	$form['name'] = array(
		'#title' => 'username',
		'#description' => 'choose a username',
		'#type' => 'textfield',
		'#required' => TRUE,
	$form['mail'] = array(
		'#title' => 'email',
		'#description' => 'enter a valid email address',
		'#type' => 'textfield',
		'#required' => TRUE,
        $form['submit'] = array(
		'#type' => 'submit',
		'#value' => t('Save'),

The username and email are all that are specifically required to create the account, other fields created with Field UI can be added in at will. You could even call them something different if you wanted, here in the hook_form().

The real magic of saving the user happens in the submit handler of your module, which utilizes user_save().

Submit Handler

function foo_user_register_form_submit($form, &$form_state){
$edit = array(

Subscribe with RSS Subscribe to RSS - User Register