开发者

Overriding the user registration form in Drupal 6

开发者 https://www.devze.com 2023-01-10 07:57 出处:网络
I want to be able to customise the user registration form in Drupal 6 I have found thousands of tutorials that show me how to override the structure in which y开发者_如何学运维ou can output the form

I want to be able to customise the user registration form in Drupal 6

I have found thousands of tutorials that show me how to override the structure in which y开发者_如何学运维ou can output the form as a whole, but I want to move form elements around etc and I cant quite seem to see the best way to do this


To expand on Jeremy's answer, you're going to want to study Drupal's Form API and user_register(). In short, you build an associated array; each element in the array corresponds to one form element.

Each form element in the array is its own associated array. They can have a type: textfield, select menu, checkboxes, etc.: see the Form API reference for all the types.

Each form element can also have a weight: this is how you order elements around. Lower numbered weights show up before higher numbered weights in the form.

One of the element types available to you is fieldset: this is what will allow you to group elements together. When you use a fieldset, it creates a section of the form with its own weight values.

So, let's say you have a form with three fields: Name, Company, and E-mail address. The Name should show up first, Company second, E-mail address third. You could specify the form like so:

$form['name'] = array(
  '#type' => 'textfield',
  '#title' => t('Name'),
  '#weight' => 1,
);
$form['company'] = array(
  '#type' => 'textfield',
  '#title' => t('Company'),
  '#weight' => 2,
);
$form['email'] = array(
  '#type' => 'textfield',
  '#title' => t('E-mail address'),
  '#weight' => 3,
);

Note the #weight key. If you wanted Company to appear after E-mail address, you'd set $form['company']['#weight'] to something higher than 3.

Now let's say you wanted to group Name and Company into a fieldset called Personal Information. Your form would now look something like this:

$form['personal'] = array(
  '#type' => 'fieldset',
  '#title' => t('Personal information'),
  '#weight' => 1,
);
$form['personal']['name'] = array(
  '#type' => 'textfield',
  '#title' => t('Name'),
  '#weight' => 1,
);
$form['personal']['company'] = array(
  '#type' => 'textfield',
  '#title' => t('Company'),
  '#weight' => 2,
);
$form['email'] = array(
  '#type' => 'textfield',
  '#title' => t('E-mail address'),
  '#weight' => 3,
);

Note that Name and Company are now array elements of $form['personal'].

If you want to make Name show up after Company in the fieldset, set its #weight higher than 2. Because the Name is now part of a fieldset that has a lower #weight than the E-mail address field, even if you set $form['personal']['name']['#weight'] to 4, it wouldn't make the Name show up after E-mail address.

So what you're going to attempt to do is use hook_form_alter() to alter the user_register form to change the weights of certain form elements, create your own fieldsets, and move certain form elements into your newly created fieldsets.

There are ways to do this within your theme, but I prefer creating a custom module for this. Create your custom module, and implement hook_form_alter():

function test_form_alter(&$form, $form_state, $form_id) {
  if ($form_id === 'user_register') { // Only modify the user registration form
    // Before you can get down to business, you need to figure out the
    // structure of the user registration form. Use var_dump or kpr to dump
    // the $form array. 

    // Note: if you want to use kpr on the user registration form, give
    // anonymous permission to see devel information.
    // kpr($form);

    // Move Name field to after E-Mail field
    $form['name']['#weight'] = 2;
    $form['mail']['#weight'] = 1;

    // Group Name and E-mail together into a fieldset
    $form['personal_info'] = array(
      '#type' => 'fieldset',
      '#title' => t('Personal information'),
    );

    $form['personal_info']['name'] = $form['name'];
    $form['personal_info']['mail'] = $form['mail'];

    // The last block only copied the elements: unset the old ones.
    unset($form['name']);
    unset($form['mail']);
  }
}

In more complex forms, moving things from one fieldset to another might yield unexpected results when submitting the form. This is because $form['name'] isn't the same as $form['group']['name'], which isn't the same as $form['other_group']['name']. You don't have to worry about that on the user_register form for the most part, but check out the handbook page on #tree and #parents for more information about this.

This covers modifying existing fields in the user registration form: if you want to add new fields, I highly recommend using Content Profile. If you want to create custom fields on your own, it's going to get a lot more complex as you're going to have to implement your own validate and submit handlers. Content Profile handles this for you: check out its README to see how to activate it for registration forms.


Using hook_form_alter you can do whatever you want with a form.

For example changing the weight can change the position on the page.

If you try:

MYMODULE_form_user_profile_form_alter(&$form, $form_state) {
// do your processing here
var_dump($form);
}

replacing MYMODULE with the name of your module.

You will see the structure of the form, you can change values in there to alter, labels weights descriptions etc.


In a module, first use hook_theme() , now assuming the name of your module is 'd6_forms' :

function d6_forms_theme() {
  return array(
    'user_register' => array(
      'template' => 'templates/user-register-form',
      'arguments' => array('form' => NULL),
    ),        
  );
}

This will make the user_register form look for a template, in the specified folder. So make sure that in your module folder, there is a folder called 'templates', with a file 'user-register-form.tpl.php'.

You notice that in the hook_theme() , the extenstion of the template file ( .tpl.php ) is not supplied. That's normal, you don't need to specify it there. Do make sure however, that the template has that extension, and that it's not just named 'user-register-form.php' !

In that template file, you have access to the $form variable , so print it there to see what fields are in there. The devel module is recommened, since it's able to print big Drupal arrays in a fancy way ( using dpm() ). If you do not have Devel module, or don't want to use it, this also works : <?php print '<pre>' . print_r($form, 1) . '</pre>'; ?>.

To print a field, just use <?php print drupal_render($form[field_name]); ?>, this will print the field and make sure that it works as intended. So for example, if you want to print the 'name' field in the $form array, just use <?php print drupal_render($form['name']); ?>.

You don't have to print every field ! Just print the fields that you want to move somewhere ( which, with a basic Drupal register form, are about 3 : name, email & submit ). To print all the remaining fields, just end your template with <?php print drupal_render($form); ?>.

It is important that you don't forget this, since the $form var contains stuff that is absolutely needed for your form to work ( like a token, etc .. ). So good standard behaviour when templating a form, is to print that piece of code first at the bottom of your template.

This is an entire example of a small register form template, with some basic html :

<?php 
  // What is in that $form var ? To check, uncomment next line
  // print '<pre>' . print_r($form, 1) . '</pre>'; 
?>

<div style="background-color:#ddd;padding:10px;">
  <?php print drupal_render($form['name']); ?>
  <?php print drupal_render($form['mail']); ?>
</div>

<div>
  <?php print drupal_render($form['submit']); ?>
</div>                 

<?php print drupal_render($form); ?>


maybe this will help: http://drupal.org/node/44910


You just need to Enable the Profile Module which would give access to place more fields in sign up form.

Go through this Simple Video tutorial which would be very helpful for beginners in Drupal .

http://planetghost.com/add_more_fields_to_sign_up

0

精彩评论

暂无评论...
验证码 换一张
取 消