User profile tabs mélange


By Aaron - Posted on 07 April 2010

Drupal's profile module is basic, but sometimes it's just right for the job. You can add categories that appear as tabs when you edit your profile, and you can add a variety of basic fields to the categories.

Drupal comes with a user picture and signature that can be displayed in your comments, forum posts, and pretty much anywhere with a little theming. By default the picture and signature are entered on the "account" tab when you edit the profile. But if you have another category with additional personal information, like a biography, it can make more sense to move the pic and sig there, instead of its default placement with the username, email and password.

The api's hook_user() lets you move these fields, and lets you merge any number of tabs, including the account tab. There is a module that blasts them all into one tab: One page profile .

If you need a bit more finesse, here are a few tricks. This all goes in a custom module.

First we'll combine a couple of tabs into the account tab.

<?php
function custom_user($op, &$edit, &$account, $category = NULL) {
  if (
$op == 'form' && $category == 'account') {
   
$form = profile_form_profile($edit, $user, 'Personal Information');
   
$form += profile_form_profile($edit, $user, 'Historical Information');
   
$form['Historical Information']['#weight'] = 10;
   
$form['Personal Information']['#weight'] = 11;
  }
  return
$form;
}
?>

This checks that hook_user() is building the 'account' tab, and it tacks on the Personal Information and Historical Information tabs. Those are the names I assigned as categories when I added profile fields.

Depending on how many tabs you have, you might need to adjust the weight.

Next, you probably want to remove the Historical and Personal tabs. You do this in a hook_menu_alter(). Be sure to clear your menu cache.

<?php
function custom_menu_alter(&$items) {
 
// Unset these tabs on the user/edit page.
 
unset($items['user/%user_category/edit/Historical Information']);
  unset(
$items['user/%user_category/edit/Personal Information']);
}
?>

Next, in a separate example, let's move the picture and signature fields to another tab where they make more sense, my Personal Information category.

This is hook_user() again.

<?php
 
if ($op == 'form' && $category == 'Personal Information') {
   
   
// Load the entire account form into $picture. 
   
$picture = _user_forms($edit, $account, 'account');

   
// Add weight to personal info category fields so we can stick picture and signature where we want.
   
$i = 0;
    foreach (
element_children($form['Personal Information']) as $key => $element) {
     
$form['Personal Information'][$element]['#weight'] = $i;
     
$i++;
    }
   
   
// We must add the uid for use in the picture file name.
   
$form['#uid'] = $account->uid;
   
   
// Merge in the picture field.
   
$form['Personal Information'] += $picture['picture'];
   
   
// We need to add in the picture validation..
   
$form['#validate'][] = 'user_validate_picture';
   
   
// Set the weight and change the title.
   
$form['Personal Information']['picture_upload']['#weight'] = 0.1;   
   
$form['Personal Information']['picture_delete']['#weight'] = 0.2;
   
$form['Personal Information']['picture_upload']['#title'] = 'Upload Personal Photo';

   
// Repeat for the signature.
   
$form['Personal Information'] += $picture['signature_settings'];
   
$form['Personal Information']['signature']['#weight'] = 0.5;
   
$form['Personal Information']['signature']['#title'] = 'Peronal Motto';
   
// We don't want the confusing filter tips.
   
unset($form['Personal Information']['signature_format']);

    return
$form;
  }
?>

Now we need to remove those fields from our account tab, which will be left with just the email, username and password, like it should be.

This is done with hook_form_alter().

<?php
function custom_form_alter(&$form, $form_state) {
  switch (
$form['#id']) {

 
// Remove sig and pic from accout tab.
   
case 'user-profile-form' :
      unset(
$form['signature_settings']);
      unset(
$form['picture']);
      break;
  }
}
?>

Finally, Drupal will keep all the profile fields' data in the users table's data column until they are specifically saved to the profile_values table. This happens automatically until you start moving things around like we did above. In your hook_user() implementation you'll need to save the profile specifically for categories you merged in, but not for the the original category. Otherwise, your merged fields will live in limbo and not be availabe to the views module.

<?php
 
if (($op == 'update' || $op == 'insert) && $category == 'account') {
    profile_save_profile($edit, $account, '
Personal Information');
    profile_save_profile($edit, $account, '
Historical Information);
  }
?>

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> <img>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor.
Fill in the blank