How do I customize the default user registration page in Drupal 7
The default user registration page in Drupal 7 has fields to enter a username, password and an email address.
I have added some extra fields that show up in this form, but I would like to change the way it is displayed.
For example, I would like to change the order of the form items and add CSS.
Importantl开发者_如何学运维y, I would like to change the label that is displayed for Username - I would like it to say "Nickname" instead.
How can I do this?
You can change this on several layers:
Translations:
In settings.php you see some examples already, just add:
$conf['locale_custom_strings_en'][''] = array( 'Username' => 'Nickname', 'username' => 'nickname', );
You might want to override more strings, that contain username, such as "The username is invalid". The UX is bad when you start mixing up these words, so in order to do what you want properly, you will have to change several such strings anyway, even if you go with one of the other options below. Most notably you will need to give the validation and error strings some attention.
This, however, will change "username" to "nickname" sidewide! Also for admins and moderators. This might have unwanted side-effects, based on you broader specs and wishes.
Theme override:
In the template.php, you can override anything that gets passed to screen. In this case, you will need to override the login form template.
The explicit code is too much to type in here. But in rough lines you need to:
- Tell Drupal you want to use a template for the user login form with
hook_theme
. - Create the template file. And use any HTML you wish in there.
Form override:
In your theme and in modules, you can use hook_form_alter to override any theme.
In your theme, you would add to template.php:
function your_template_form_user_register_form_alter(&$form, &$form_state, $form_id) {
$form['account']['name']['#title'] = t('Nickname');
}
Or, if you want more business logic (which does not belong in the theme layer), you'd create a module and add a form_alter there:
function your_modulename_form_user_register_form_alter(&$form, &$form_state, $form_id) {
if (_your_module_is_person_from_irc()) {
$form['account']['name']['#title'] = t('Nickname');
}
}
I had a similar task a few days ago and did it with the help of this post.
You do need to have a custom theme set-up for this but that is quite easy to do there are lots of examples for this.
On a sidenote, consider posting drupal questions to http://drupal.stackexchange.com, you would probably get more answers.
Good-luck!
In your theme's template.php, add:
function [YOUR-TEMPLATE-NAME]_form_user_register_form_alter(&$form, &$form_state, $form_id) {
$form['account']['name']['#title'] = t('Nickname');
}
To see everything you can alter, do a print_r($form);
inside of that function.
I just had to tackle the same problem.
I tried for several hours to understand the user_register_submit and user_register_form functions.
The easiest and most flexible solution I found was to retrieve the form data (username, email, and password) through ajax/jquery, and create a custom module that saves the data to a new user programmatically.
In fact I find that more often then not it is faster to just bypass 'the drupal way' through ajax/custom modules than try to understand layers and layers of legacy code.
And this is coming from a guy who started drupaling with no prior php knowledge.
精彩评论