Thursday, April 7, 2011

Theming the Drupal 6 User Login Form

Note: for myself, this method seems good at first glance. However, it did not cache the form. That means, the form gets rebuild every time you reload the page.

=====================

There are a great deal of articles out there on theming the Drupal User Login Form, but most don't give you granular control over each individual form element. Rather, you are restricted to mainly editing the css. Here is a way to have complete granular control over each element within the user login form...

1. Place this function either in a module or in your template.php:


function get_user_login_form() {
$form_id = 'user_login';
$form = array();
$form['name'] = array(
'#type' => 'textfield',
'#maxlength' => USERNAME_MAX_LENGTH,
'#required' => TRUE,
'#attributes' => array('tabindex' => '1'),
);
$form['pass'] = array(
'#type' => 'password',
'#required' => TRUE,
'#attributes' => array('tabindex' => '2'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Log in'),
'#weight' => 2,
'#attributes' => array('tabindex' => '3')
);
$form['#validate'] = user_login_default_validators();
$form['#build_id'] = sprintf('form-%s', md5(uniqid(mt_rand(), TRUE)));
$form_state = array(
'storage' => NULL,
'submitted' => FALSE,
);

$form['#post'] = $_POST;
drupal_prepare_form($form_id, $form, $form_state);
drupal_process_form($form_id, $form, $form_state);
$out = new stdClass;
$out->form_start =
sprintf("<form method='post' accept-charset='UTF-8' action='%s'>",
url('user/login'));
$out->form_end = "</form>";
$out->name = drupal_render($form['name']);
$out->pass = drupal_render($form['pass']);
$out->submit =
drupal_render($form['submit']) .
drupal_render($form['form_id']) .
drupal_render($form['form_build_id']) .
drupal_render($form['form_token']);
return $out;
}


2. In your .tpl.php file, output the fields


<?php
$login_form = get_user_login_form();
?>
<?php print $login_form->form_start; ?>
Username: <?php print $login_form->name; ?><br />
Password: <?php print $login_form->pass; ?><br />
<?php print $login_form->submit; ?>
<?php print $login_form->form_end; ?>

Reference:
http://blog.aphexcreations.net/2009/04/theming-drupal-user-login-form.html

No comments: