How to customize the user registration, logon, and password reset page of drupal

Source: Internet
Author: User
Tags php file php print vars wrapper drupal

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:

The code is as follows: Copy code
<? 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
The template can be the same for all three. The example above uses a different template for each case: user-login, user-register, and user-pass
The template names must use a dash, not an underscore
Note: It's user_pass not user_password

For D7:

The code is as follows: Copy code
<? Php
Function yourtheme_theme (){
$ Items = array ();
$ Items ['User _ login'] = array (
'Render element' => 'form ',
'Path' => drupal_get_path ('theme ', 'yourtheme'). '/templates ',
'Template' => 'User-login ',
'Preprocess functions' => array (
'Yourtheme _ preprocess_user_login'
),
);
$ Items ['User _ register_form '] = array (
'Render element' => 'form ',
'Path' => drupal_get_path ('theme ', 'yourtheme'). '/templates ',
'Template' => 'User-register-form ',
'Preprocess functions' => array (
'Yourtheme _ preprocess_user_register_form'
),
);
$ Items ['User _ pass'] = array (
'Render element' => 'form ',
'Path' => drupal_get_path ('theme ', 'yourtheme'). '/templates ',
'Template' => 'User-pass ',
'Preprocess functions' => array (
'Yourtheme _ preprocess_user_pass'
),
);
Return $ items;
}
?>

Notes about the D7 version:

The 'Path' lines tell Drupal where to find. tpl. php files. this is optional, and in the code above, 'path' tells Drupal to find the files in the templates subdirectory of the theme's base directory.
Note the "_ form" added to the user_register element.
Note: As it is the case for D6, it's user_pass not user_password

Step 3.

Now you implement three preprocess functions. There may be more concise ways to code this, but this works very well and is easy to read, so here we go!

For D6:

The code is as follows: Copy code
<? Php
Function yourtheme_preprocess_user_login (& $ variables ){
$ Variables ['intro _ text'] = t ('this is my awesome login form ');
$ Variables ['rendered'] = drupal_render ($ variables ['form']);
}
Function yourtheme_preprocess_user_register (& $ variables ){
$ Variables ['intro _ text'] = t ('this is my super awesome reg form ');
$ Variables ['rendered'] = drupal_render ($ variables ['form']);
}
Function yourtheme_preprocess_user_pass (& $ variables ){
$ Variables ['intro _ text'] = t ('this is my super awesome insane password form ');
$ Variables ['rendered'] = drupal_render ($ variables ['form']);
}
?>

Notes about that code:

Change the function name by replacing "yourtheme" with the name of your theme
The line $ variables ['intro _ text'] adds the text that follows to the $ variables array, which gets passed to the template as $ intro_text
The second line renders the form and adds that code to the $ variables array, which gets passed to the template as $ rendered

For D7:

The code is even simpler for D7 because we don't need to pass a variable containing the form content we want rendered. the variable exists already in the $ vars array and can be rendered in. tpl. php file.

The code is as follows: Copy code
<? Php
Function yourtheme_preprocess_user_login (& $ vars ){
$ Vars ['intro _ text'] = t ('this is my awesome login form ');
}
Function yourtheme_preprocess_user_register_form (& $ vars ){
$ Vars ['intro _ text'] = t ('this is my super awesome reg form ');
}
Function yourtheme_preprocess_user_pass (& $ vars ){
$ Vars ['intro _ text'] = t ('this is my super awesome request new password form ');
}
?>

The above preprocess functions simply add a variable into the $ vars array that is then displayed in the. tpl. php file. Much more complex manipulation of the content of the render array is possible.

Please note, that the preprocess functions shoshould go into the template. php file.

Step 4.

Create template files to match the 'template' values defined above.

For D6

We need the following template files (make sure to use a dash, not an underscore ):

User-login.tpl.php
User-register.tpl.php
User-pass.tpl.php

For D7

Same as D6, counter t using user-register-form.tpl.php instead of user-register.tpl.phpfor the register form.

Step 5.

Paste the following into user-login.tpl.php. Modify as necessary for user-register.tpl.php (D6) and user-register-form.tpl.php (D7 ):

For D6:

The code is as follows: Copy code
<P> <? Php print $ intro_text;?> </P>

<Div class = "my-form-wrapper">

<? Php print $ rendered;?>

</Div>

For D7:

The code is as follows: Copy code
<P> <? Php print render ($ intro_text);?> </P>

<Div class = "yourtheme-user-login-form-wrapper">

<? Php print drupal_render_children ($ form)?>

</Div>

Note the change to the syntax for causing Drupal to render the form. Also, the D7 sample uses a different class for the div, but that's just a matter of preference.

Step 6.

Save your template. php file to the theme's main directory. save your. tpl. php files in the same place for the D6 examples, or, in the case of the D7 examples, to the directory you specify in the 'path' element of the $ items array.

Step 7.

Rebuild the cache. Go to Administration> Performance and click on "Rebuild Cache" on the bottom of the page.

Now, the user login page will contain in the new text from the preprocess function, and the tpl. php file (s) can be modified to suit the site's needs.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.