displaying all variables available and debugging your advanced custom login form

Last modified: August 26, 2009 - 23:07

description

This snippet shows you how to display all the variables available to your user_login.tpl.php file and simple instructions on how to use that information in your custom login form.

If you want to customise the full page layout, click through to the Customising the login, registration and request password full page layout handbook page.

step 1 of 2

In a text editor like notepad.exe, create a file called template.php using the the following snippet. If you already have a template.php file, simply add it to your existing one (remembering to omit the opening and closing PHP tags if you're adding it to an existing template.php file).

<?php
/**
   * This snippet catches the default login form and looks for an
   * user_login.tpl.php file in the theme folder
   */

function phptemplate_user_login($form) {
    return
_phptemplate_callback('user_login', array('form' => $form));
}
?>

step 2 of 2

  1. In a text editor create a new text file and paste the following snippet into it. Save the file awiththe filename user_login.tpl.php
  2. Edit the style sheet classes and content to suit
  3. Upload your edited user_login.tpl.php to your active theme folder

For use with Drupal 4.7.x

/**
   * This snippet displays the login form and all the variables
   * available to your custom user_login.tpl.php file
   * 
   */

<div class="login_form">
<?php
    print_r
(form_render($form)); // this displays the login form.
?>

</div>
<?php
print "<div style=\"width:500px;\"><pre>";
print_r($form); // displays all the variables available
print "</pre></div>";
?>

For use with Drupal 5.x

/**
   * This snippet displays the login form and all the variables
   * available to your custom user_login.tpl.php file
   * 
   */
<div class="login_form">
<?php
    print_r
(drupal_render($form)); // this displays the login form.
?>

</div>
<?php
print "<div style=\"width:500px;\"><pre>";
print_r($form); // displays all the variables available
print "</pre></div>";
?>

Default output (Drupal 5.x)

Below is the default login form variables that are available to your custom user_login.tpl.php file (Drupal 5.x). Please note that earlier versions of Drupal may vary.

Array
(
    [name] => Array
        (
            [#type] => textfield
            [#title] => Username
            [#size] => 10
            [#maxlength] => 60
            [#required] => 1
            [#attributes] => Array
                (
                    [tabindex] => 1
                )

            [#description] => username
            [#post] => Array
                (
                )

            [#programmed] =>
            [#tree] =>
            [#parents] => Array
                (
                    [0] => name
                )

            [#weight] => 0
            [#processed] =>
            [#input] => 1
            [#autocomplete_path] =>
            [#name] => name
            [#id] => edit-name
            [#value] =>
            [#sorted] => 1
            [#printed] => 1
        )

    [#parameters] => Array
        (
            [0] => user_login
        )

    [#type] => markup
    [#post] => Array
        (
        )

    [#programmed] =>
    [#value] =>
    [#theme_used] => 1
    [#id] => user-login
    [#method] => post
    [#description] =>
    [#required] =>
    [#tree] =>
    [#parents] => Array
        (
        )

    [#attributes] => Array
        (
        )

    [#action] => /version5/user
    [#validate] => Array
        (
            [user_login_validate] => Array
                (
                )

        )

    [#processed] =>
    [#theme] => user_login
    [#submit] => Array
        (
            [user_login_submit] => Array
                (
                )

        )

    [pass] => Array
        (
            [#type] => password
            [#title] => Password
            [#description] => password
            [#required] => 1
            [#attributes] => Array
                (
                    [tabindex] => 2
                )

            [#post] => Array
                (
                )

            [#programmed] =>
            [#tree] =>
            [#parents] => Array
                (
                    [0] => pass
                )

            [#weight] => 0.001
            [#processed] =>
            [#input] => 1
            [#size] => 10
            [#name] => pass
            [#id] => edit-pass
            [#value] =>
            [#sorted] => 1
            [#printed] => 1
        )

    [form_id] => Array
        (
            [#type] => hidden
            [#value] => user_login
            [#id] => edit-user-login
            [#post] => Array
                (
                )

            [#programmed] =>
            [#tree] =>
            [#parents] => Array
                (
                    [0] => form_id
                )

            [#weight] => 0.003
            [#processed] =>
            [#description] =>
            [#attributes] => Array
                (
                )

            [#required] =>
            [#input] => 1
            [#name] => form_id
            [#sorted] => 1
            [#printed] => 1
        )

    [submit] => Array
        (
            [#type] => submit
            [#value] => Log in
            [#weight] => 2
            [#attributes] => Array
                (
                    [tabindex] => 3
                )

            [#post] => Array
                (
                )

            [#programmed] =>
            [#tree] =>
            [#parents] => Array
                (
                    [0] => submit
                )

            [#processed] =>
            [#description] =>
            [#required] =>
            [#input] => 1
            [#name] => op
            [#button_type] => submit
            [#executes_submit_callback] => 1
            [#id] => edit-submit
            [#sorted] => 1
            [#printed] => 1
        )

    [#children] =>


    [#printed] => 1
)

How to use that information for your custom login form

To illustrate how to use that information, I'm going to use the example of changing the text displayed on the login submit button.

Here's the full list of variables, just for the SUBMIT array, which includes a value of 'log in' for the [#value]. Or in other words [submit][value] =>'login'

[submit] => Array
        (
            [#type] => submit
            [#value] => Log in
            [#weight] => 2
            [#attributes] => Array
                (
                    [tabindex] => 3
                )

            [#post] => Array
                (
                )

            [#programmed] =>
            [#tree] =>
            [#parents] => Array
                (
                    [0] => submit
                )

            [#processed] =>
            [#description] =>
            [#required] =>
            [#input] => 1
            [#name] => op
            [#button_type] => submit
            [#executes_submit_callback] => 1
            [#id] => edit-submit
            [#sorted] => 1
            [#printed] => 1
        )

Translating that into your snippet, you can apply it in your template.php override.

<?php
/**
   * This snippet overrides the default login form,
   * changes the submit button text to Click here to login
   * and looks in your theme folder for a custom user_login.tpl.php
   *  layout file.
   */

function phptemplate_user_login($form) {
 
$form['submit']['#value'] = 'Click here to login';
return
_phptemplate_callback('user_login', array('form' => $form));
}
?>

notes

  • This snippet was tested with Drupal 5.x (June 24th 2007) by Dublin Drupaller (Note: If you have the locale.module enabled you may need to refresh your search_index by editing any text string related to the user page before your changes take effect, such as 'password'. )
 
 

Drupal is a registered trademark of Dries Buytaert.