Customizing the user profile layout per role

Last modified: August 26, 2009 - 03:00

Using the redirect for the user pages is great but if you have several roles and want to have different pages for those roles you have to tweak it a bit more.

The roles information is in the $fields variable. For one role per user it is easy, you just get the key of the $field array and do a switch like this:

<?php
/**
* Catch the theme_user_profile function, and redirect through the template api
*/
function phptemplate_user_profile($user, $fields = array()) {
 
// Pass to phptemplate, including translating the parameters to an associative array. The element names are the names that the variables will be assigned within your template.
// potential need for other code to extract field info
 
switch ( key($fields) ) {
      case
"Role1" :
        return
_phptemplate_callback('user_profile_role1', array('userObj' => $user, 'fields' => $fields));
        break;
    case
"Role2":
        return
_phptemplate_callback('user_profile_role2', array('userObj' => $user, 'fields' => $fields));
        break; 
  }
}
?>

For several user roles per user there are more array elements in the $field variable thus if you want to go fine grained you have to do a foreach or something.

drupal6

IckZ - December 19, 2008 - 14:40

Hello,
how to solve this with drupal6?

Thanks for any help.

http://api.drupal.org/api/file/modules/user/user-profile.tpl.php

abcprabhu - December 27, 2008 - 16:03

Try This Link - Which may help you out!

http://api.drupal.org/api/file/modules/user/user-profile.tpl.php

;-lord-c

User Profile Preprocess Function

duellj - February 3, 2009 - 01:19

Not sure why I didn't see this before, but you can use an user-profile preprocess function to add template_file suggestions for user roles:

<?php
function phptemplate_preprocess_user_profile(&$variables) {
 
// Add template suggestions based on roles
 
if (in_array('role1', $variables['account']->roles)) {
   
$variables['template_files'][] = 'user-profile-role1';
  }
}
?>

Or, if you want to automatically generate template suggestions based on the users role:

<?php
function phptemplate_preprocess_user_profile(&$variables) {
 
// Add template suggestions based on roles
 
foreach ($variables['account']->roles as $role) {
   
$variables['template_files'][] = 'user-profile-'. preg_replace('/[^a-zA-Z0-9_-]+/', '-', $role);
  }
}
?>

The second example has one disadvantage though: roles are alphabetically sorted, so the last role with a template will be the one used for the user profile. With the first example, you can control the weight of role template suggestions.

Drupal 6.x User profile by role

Fr0st - January 28, 2009 - 23:28

In this example, I wanted the role named "band member" to display their user profiles using only the Content Profile data authored with their username. I did not want to display the Picture or History.

For all other users, I wanted to display the default user profile page, including their Picture and History.

The following code goes in a new file named user-profile.tpl.php which resides in sites/all/themes/[your_custom_theme_name]:

<?php /* user profile page displayed by role*/

$user_obj = user_load(arg(1));
if (
in_array('band member', $user_obj->roles)) { ?>

<div class="profile">
    <?php print $profile[content_profile]; ?>
</div>
<?php }
else {
?>

<div class="profile">
    <?php print $user_profile; ?>
</div>
<?php }

 
 

Drupal is a registered trademark of Dries Buytaert.