Customizing the User Login Page in Drupal 7
I was recently tasked with theming a customized user login page in Drupal 7. I could not find a whole lot of documentation for this so the first place I looked was in the core modules folder hoping to find something like user--login.tpl.php that I would be able to copy and put in my theme folder for an override. No such luck, I looked in /modules/user but nothing there I could use. Next I looked around drupal.org and found some vague references to doing this in scattered posts. This took me a several hours but finally I pieced together code from a bunch of posts and came up with a method that worked well for me.
The Code
You can add this code to your theme's template.php file where "your_themename" is the machine name of your theme. Here we tell Drupal that we want a user-login.tpl.php file in our theme folder.
function your_themename_theme() {
$items = array();
// create custom user-login.tpl.php
$items['user_login'] = array(
'render element' => 'form',
'path' => drupal_get_path('theme', 'your_themename') . '/templates',
'template' => 'user-login',
'preprocess functions' => array(
'your_themename_preprocess_user_login'
),
);
return $items;
}You can now create a user-login.tpl.php and put it in your theme folder or the theme's template folder if there is one. Note that there are no "double dashes" in this file name as is typically traditional with Drupal 7 template file names.
A Few Ways to Theme it.
<?php print drupal_render_children($form) ?>
This is probably fine in some cases but I needed to have something more granular. For this, I print out the individual variables at the top of my user-login.tpl.php file to see what's available for theming.
<?php /* print the variables if needed to allow for individual field theming or breaking them up. */ print '<pre>'; print_r($variables); print '</pre>'; ?>
Now clear your cache and reload /user/login and the variables should print out. You will see a huge mostly undecipherable list of variables here. What we are looking for are the top level array items. These will include:
[name] => Array [pass] => Array [form_build_id] => Array [form_id] => Array [actions] => Array
Themable Output
I can covert these into themable output as such:
<?php
/* split the username and password from the submit button
so we can put in links above */
print drupal_render($form['name']);
print drupal_render($form['pass']);
print drupal_render($form['form_build_id']);
print drupal_render($form['form_id']);
print drupal_render($form['actions']);
?>Well, you can see where this is going, you can really theme this page however you want now that all the fields are separate. I also needed to add an image to this page so I can do that using the theme path for my image.
<div class="login-photo">
<img src="<?php print base_path() . drupal_get_path('theme', 'your_themename')
. '/images/login-photo.jpg'; ?>"
alt="Login" title="Login" width='327' height='221' />
</div>Again, be sure to change your_themename to the machine name of your theme. I've attached the finished theme file below so to use if for Drupal 7, you simply need to add in your template.php code above and then theme it however you want. Note, to achieve the screen capture above of my custom login page, I used a little module I created to remove the login tabs so I could have a more simple and elegant look to the page. I was now able to print those links just above the login button.
Resources
Files
| Attachment | Size |
|---|---|
| 803 bytes |

Comments
Simon (not verified)
Tue, 10/02/2012 - 13:18
Permalink
Thanks a bunch!
Just leaving a comment to say thanks, exactly what I was looking for. I hope they make these type of changes easier within version 8, will you guys be making the switch?
Danny Englander
Tue, 10/02/2012 - 13:28
Permalink
Hi Simon, glad this helped. I
Hi Simon, glad this helped. I probably won't start to develop production sites with Drupal 8 until much later next year if not early 2014...
mikesuptown (not verified)
Mon, 11/19/2012 - 13:12
Permalink
machine name use
i have a really basic question... i'm new to drupal so apologize in advance... in your code you mention modifying code to theme name so where
<div class="your-themename-user-login-form-wrapper">would be
"<div class="zen_theme-user-login-form-wrapper">and
<img src="<?php print base_path() . drupal_get_path('theme', 'your_themename') ?>would be
<img src="<?php print base_path() . drupal_get_path('theme', 'zen_theme') ?>Danny Englander
Tue, 11/27/2012 - 10:15
Permalink
Sort of, wherever you see
Sort of, wherever you see "your_themename", yes that's the machine name of your theme. The machine name is based on the folder name of your theme in /sites/all/themes or /sites/yourdomain.com/themes/. So if my theme name is "My Theme", the machine name would probably be my_theme in the theme folder but it could very well be called mytheme as well.
Clint (not verified)
Mon, 11/26/2012 - 12:31
Permalink
Modifying the entire page?
How would I go about modifying the entire page, and not just the form? For example, on the registration page I may want a very different layout
Danny Englander
Tue, 11/27/2012 - 10:10
Permalink
Offhand, I am not sure, I'd
Offhand, I am not sure, I'd have to look into to it, not sure when I will have time. Thanks.
Luke (not verified)
Mon, 11/26/2012 - 23:20
Permalink
Use mutliple for several form?!?
This was awesome. Worked no problems but I want to change the register form as well. I can use the _theme function on either one or the other but not both as I don't know how to write the code.
Any help?
Danny Englander
Tue, 11/27/2012 - 10:08
Permalink
You would use the same exact
You would use the same exact process and create a "user-register-form.tpl.php".
Luke (not verified)
Tue, 11/27/2012 - 14:57
Permalink
Thanks
Figured it out, I was calling the _theme function twice instead of combining the two $items in the same function. Now my login pages finally look remotely non-drupal! Thanks danny.
Danny Englander
Tue, 11/27/2012 - 15:01
Permalink
Hi Luke, great!
Hi Luke, great!
Chris (not verified)
Mon, 01/07/2013 - 02:54
Permalink
Devel module - dpm($variables) function
By the way, if you install the devel module (one that every theme developer should have anyway) then you can use the dpm() function to out put that $variables array, as it does a much more beautiful job than what you did, just saying.
Supriya Rajgopal (not verified)
Thu, 01/17/2013 - 01:28
Permalink
Thanx a ton!
Thank you so much for this article.I'd been searching for days
Nick (not verified)
Wed, 01/30/2013 - 11:33
Permalink
Great post
Thanks so much for sharing this!
stomerfull (not verified)
Thu, 03/14/2013 - 09:02
Permalink
Hello working for user/login
Hello working for user/login page but what about user/1/edit the user profil form page
triyng something like this
function MYTHEME_theme($existing, $type, $theme, $path) {
$items = array();
$items['user_profile_form'] = array(
'render element' => 'form',
'path' => drupal_get_path('theme', 'MYTHEME') . '/templates',
'template' => 'user-profile-form'
);
return $items;
}
?>
and create the template file user-profil-form.tpl.php Put it inside the template file
print "TEST";
print drupal_render($form);
?>
But it's not working Thank you for your help or it's impossible ?
Add new comment