Thursday, April 2, 2009

Customizing your Drupal user profile page

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

Create any custom profile fields
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:


<?php $user_profile and $account ?>

code hosted by snipt.net
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); ?>

code hosted by snipt.net
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)); ?>

code hosted by snipt.net
For info on what the t() function does, checkout the API doc on Drupal.org.

That's pretty much it!

No comments: