Drupal user account pages
Written at 07:10, on Thursday 9 October 2008. Tags: drupal .
It’s the little things that count…
On a default Drupal installation, the user account could really use some love and attention. There are a number of predefined pages to allow users to register themselves on the website, but the usability of these pages is pretty bad. This post is about one little example: they all share the same title. I’m going to show you how you can fix that.
Have a look at the drupal.org register, log in and request new password pages. You’ll note that each one has a head title of “User account | drupal.org”, and a page title of “User account”. While each page certainly has a different function, why would they all have the same titles? That’s just weird and plain bad usability – a page title should be descriptive of the main function of a page.
Anyway, there are ways to override that. Drupal uses a placeholder variable to dynamically determine the page and head title based on the internal path. In your theme, you’ll find something like the following:
<title><?php print $head_title ?></title>
<h1><?php print $title ?></h1>
Since Drupal determines its context based on the path that is requested, simply checking for the arguments which are passed to the page is enough.
<h1><?php if (arg(0) 'user' && arg(1) 'register') : print t('Create new account'); elseif (arg(0) 'user' && arg(1) 'password') : print t('Request new password'); elseif (arg(0) 'user' && arg(1) 'login') : print t('Log in'); elseif (arg(0) 'user' && arg(1) '') : print t('Log in'); else: print $title; endif; ?></h1>
It’s really simple PHP, checking for the first and second arguments (
arg(1)). Note that instead of printing out the text, it’s wrapped in the t function, which will make it translatable. That way, you don’t have to edit the theme in order to translate your texts afterwards.
As you can see, there are two checks for the login page. That’s because both the user and the user/login pages present a login page, and the Drupal menu that is presented on the account pages defaults to the user page. So we have to check if the second argument is empty, and if it’s not, print the title of the login page. If we didn’t add that extra check, we’d have overridden the default user account pages, which all follow the form user/uid, where uid is the numerical id of the user.
Just adding the same checks to the
<head> title will suffice to change it there as well. Full credit goes to the Drupal community, as this snippet is based on drupal.org. You can see it in action on the new CJP website: login, register and forgot password.
Comments closed | Permalink