Customizing your Drupal user profile page

Posted last month in Drupal

Customizing your Drupal user profile page is easier than you'd think. There are really only two steps:

  1. Create any custom profile fields
  2. Customize the output of the profile page

Creating custom profile fields

The first thing you'll need to do before you can begin adding custom profile fields is to enable the core module 'Profile'. This module is shipped with every Drupal installation. Simply head over to '/admin/build/modules' and enable 'Profile' under the 'Core - optional' module list.

Next, go to 'Administer -> User management -> Profiles' and from there you can add new profile fields.

Once you've got your profile fields all set, you'll notice that your user account page has automatically reflected these new fields. However, they're probably not structured the way you want them to be, so the next step is to override the default user account page.

Customizing the output of the profile page

First, create a new template file in your theme directory named 'user-profile.tpl.php'. Drupal looks for this file every time the user account page is loaded for theme overrides.

Within this template, you have two important variables at your disposal:

$user_profile and $account

The '$user_profile' variable stores the rendered HTML for the default profile page. You'll remove this variable if you plan to customize the structure and layout of the user profile page.

The '$account' variable is an object that you may use to pick and choose user and profile data, including the profile fields that you just created.

You can use the following code in your 'user-profile.tpl.php' template to take a peek at what's inside your '$account' variable:

<?php var_dump($account); ?>

As mentioned, your new profile fields will also exist here. As an example, if you wanted to output a profile field with an ID of 'zip_code', you would use the following code:

<?php print t('%zip', array('%zip' => $account->profile_zip_code)); ?>

For info on what the t() function does, checkout the API doc on Drupal.org.

That's pretty much it!

9 Comments

Ben
last month

There's a security risk when you print data without filtering or escaping. In this particular case the profile module loads in the fields but it does nothing to filter the data that is put in the user object. Printing anything to the browser from the user object could potentially contain malicious code. The appropriate example code should be:
<?php print check_plain($account->profile_zip_code); />

check_plain() is one of several methods for filtering. Find out more at http://drupal.org/writing-secure-code.

last month

Ah, yes, of course. Thanks, Ben. You would actually want to run it through t() instead, to allow for proper localization. I've updated the code, thanks!

Nick

last month

We did a similar thing on PPOnline when we relaunched. For example...
http://www.pponline.co.uk/user/njt1982

That's based on this principal:
http://www.thingy-ma-jig.co.uk/blog/12-06-2007/sprucing-up-your-user-pro...

Basically its a bunch of views in a panel with an arg being passed around.

last month
Anonymous
last month

You should never run user supplied data through t(). It does not work properly, and pollutes the locale table.

last month

@Anonymous:

According to Drupal's API via http://api.drupal.org/api/function/t:

All human-readable text that will be displayed somewhere within a page should be run through the t() function.

Do you have any supporting discussion or documentation against the use of t()?

tomchuk
last month

Continue reading the api docs for t():

Special variables called "placeholders" are used to signal dynamic information in a string which should not be translated. Placeholders can also be used for text that may change from time to time (such as link paths) to be changed without requiring updates to translations.

What would happen in the above example is that every single account's zip code would end up in the locale table as a translatable string. This is most definitely not what you want.

the correct usage would be:

<?php print t('%zip', array('%zip' => $account->profile_zip_code)); ?>

but since there is nothing there to translate, passing just a placeholder to t() is a waste of time. Now if you were doing the following:

<?php print t('Your zipcode is: %zip', array('%zip' => $account->profile_zip_code)); ?>

That would make sense, as the string "Your zipcode is: %zip" could be translated to "Votre code postal est: %zip" or even "%zip est votre code postal" for french.

last month

tomchuk,

Thanks for your comment. I was thinking about this last night after reading deeper into the documentation for t() and was going to clarify these points, but you did it for me, so thanks!

Nick

13 hours ago

Just got into Drupal, which doesn't have as much documentation on the topic of themes in 6.x as I'd like. Thanks for the article, it was a major time saver.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]".

More information about formatting options