Overriding the user registration form in Drupal 6

I want to be able to set up a user registration form in Drupal 6

I found thousands of tutorials that show me how to redefine the structure in which you can display the form as a whole, but I want to move form elements around, etc., and I cannot find a better way to do this

+3
source share
5 answers

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

For example, a change in weight may 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.

, , , ..

+3

, Drupal API user_register(). , ; .

- . : , , ..: . API API .

: . .

fieldset: . , .

, , : , . -, , . :

$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,
);

#weight. , , $form['company']['#weight'] , 3.

, Personal Information. :

$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,
);

, $form['personal'].

, , #weight 2. , #weight, , $form['personal']['name']['#weight'] 4, .

, , hook_form_alter(), user_register, , .

, . 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']);
  }
}

. , $form['name'] $form['group']['name'], $form['other_group']['name']. user_register , # tree #parents .

: , Content Profile. , , . : README, , .

+9

hook_theme(), , - "d6_forms":

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

user_register . , "" "user-register-form.tpl.php".

, hook_theme() extenstion (.tpl.php) . , . , "user-register-form.php"!

$form, , , . devel , Drupal ( dpm()). Devel , : <?php print '<pre>' . print_r($form, 1) . '</pre>'; ?>.

, <?php print drupal_render($form[field_name]); ?>, , . , "" $form, <?php print drupal_render($form['name']); ?>.

! , - ( Drupal 3: , ). , <?php print drupal_render($form); ?>.

, , $form var , (, ..). , .

, 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); ?>
+2

You just need to enable the profile module, which will provide access to post more fields in the registration form.

Take this simple video tutorial that will be very useful for beginners in Drupal.

http://planetghost.com/add_more_fields_to_sign_up

0
source

Source: https://habr.com/ru/post/1757635/


All Articles