Customizing the User Registration Form in Drupal 6

Customizing the User Registration Form in Drupal 6

//

If you are coming to Drupal from another cms, right away, you appreciate the flexibility of Drupal’s theming system. I wanted to take advantage of this on a recent project and create a custom form for the user registration. If you have been using Drupal for any period of time, you’ve already experienced the disappointment in the Drupal documentation, or lack there of. Luckily, there are some intelligent developers out there sharing their experiences, solutions, and frustrations. I came across a post describing in two simple steps how to override the user registration form, and virtually any form and it ALMOST worked.

Now I hate plagiarism, but the author (cited below) never updated his post with the corrections to what was needed to make his code work. So, here is another step by step guide in how to customize the drupal user registration form.

Step 1: Use ‘HOOK_theme’ to tell Drupal to use your custom template

We will look at changes to template.php first, you will be modifying a function called ‘HOOK_theme’ (substitute the name of your theme for ‘HOOK’ in that statement). If this function is not already there, create it using the example below. Here, we are passing on the name of the template file for the user registration form, and calling it ‘user-register’. You can feel free to use any name you wish for the template file.

      /**
     * Implementation of HOOK_theme().
     */
   
function HOOK_theme(&$existing, $type, $theme, $path) {
     
//...
     
return array(
       
// tell Drupal what template to use for the user register form
       
'user_register' => array(
         
'arguments' => array('form' => NULL),
         
'template' => 'user-register', // this is the name of the template
       
),
      );
     
//...
   
}
 
?>

After that is done, reset the registry by clearing cached data at the bottom of the Administer > Performance page

Step 2: Build Your Template

The next step is you will want to create the template for the user registration form itself. In your theme directory, create a file called user-register.tpl.php. Notice how the name exactly corresponds to the name we used in the code for template.php. If your form is not displaying properly, there is a good chance it is because there is a mispelling in one of these places. The following example shows you how to create some basic template code that will display the username and password fields, along with the submit button. Within user-register.tpl.php, paste the following code:

 

   

              print drupal_render($form['account']['name']); // prints the username field
     
?>

   

   

              print drupal_render($form['account']['mail']); // print the email field
     
?>

   

   

              print drupal_render($form['account']['pass']); // print the password field
     
?>

   

   
   

              print drupal_render($form['Personal Information']['profile_birth_date']); // prints a custom profile field
     
?>

   

   
   

              print drupal_render($form['Terms and Conditions of Use']['profile_terms_of_use']); // prints a custom profile field
     
?>

   

   
   

              print drupal_render($form['submit']); // print the submit button
     
?>

   

          print drupal_render($form['timezone']);
      print
drupal_render($form['form_build_id']);
      print
drupal_render($form['form_id']);
   
?>

 

Pay careful attention to the drupal_render of these hidden form fields:

      print drupal_render($form['timezone']);
    print
drupal_render($form['form_build_id']);
    print
drupal_render($form['form_id']);
 
?>

This was the key ingredient missing from the original authors post. Without those hidden fields your form will not submit. Clicking the submit button will cause a post back but no user will be created. I have read also you may need to include the hidden token field, but it wasn’t necessary in my case:

      print drupal_render($form['form_token']);
 
?>

If everything went as planned, Drupal should now display your custom template when the user registration page loads. Don’t forget to add some css and style up that ugly form.

As you probably noticed above, I added a few fields in my form that do not come standard with the user registration. They were created through the profile module. I would suggest using that method just because its easy. There’s probably a more flexible way to go about adding custom fields to the user registration page, but I didn’t have time to scour the interwebs for an answer and this worked, so there you go. After adding your custom fields through the profile module (make sure you specify to show the field on the user registration) if you need to get a list of all the form fields that are part of the user registration form, put the following code anywhere within your template to generate a list of fields that are ‘defined’ for it.

      print '

'

;
    print
var_export($form);
    print
'

'

;
 
?>


Source: Marc Ingram, Theming the User Registration Form in Drupal 6

Related

Need Expert Help?

See Our Full Menu of Data Services

InterWorks uses cookies to allow us to better understand how the site is used. By continuing to use this site, you consent to this policy. Review Policy OK

×

Interworks GmbH
Ratinger Straße 9
40213 Düsseldorf
Germany
Geschäftsführer: Mel Stephenson

Kontaktaufnahme: markus@interworks.eu
Telefon: +49 (0)211 5408 5301

Amtsgericht Düsseldorf HRB 79752
UstldNr: DE 313 353 072

×

Love our blog? You should see our emails. Sign up for our newsletter!