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.

<?php
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();
 
array_shift($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':

<?php
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:

<?php
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:

hook_form()

<?php
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

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

Customizing and Overriding User Login page, Register, and Password Reset in Drupal 6 and 7

Customizing the user login, register, and password reset pages is fairly simple, and uses the following concepts:

  • preprocessing to set variables
  • registration of functions in the theme registry
  • creation of one or more theme templates.

Step 1.
In the site theme directory, create or edit your template.php file.

Step 2.
The first step is to implement hook_theme for your theme. In the template.php file for your theme, look for a function named yourtheme_theme() and modify it to add these return values. If the function doesn't exist, add the following:

For D6:

<?php
/**
 * Registers overrides for various functions.
 *
 * In this case, overrides three user functions
 */
function yourtheme_theme() {
  return array(
   
'user_login' => array(
     
'template' => 'user-login',
     
'arguments' => array('form' => NULL),
    ),
   
'user_register' => array(
     
'template' => 'user-register',
     
'arguments' => array('form' => NULL),
    ),
   
'user_pass' => array(
     
'template' => 'user-pass',
     
'arguments' => array('form' => NULL),
    ),
  );
}
?>

Notes about that code:

  • Change the function name by replacing "yourtheme" with the name of your theme
Subscribe with RSS Subscribe to RSS - User Register