Tuesday 19 October 2010

Move user picture to a seperate tab in user profile tab menus

Normally if you go to user/%/edit there are all the user settings put together, which might be little confusing. I decided to move User Picture fieldset to a new tab, just to make it easier for users to upload their avatars.

To accomplish this you need to create a helper module, let's call it custom_module.
Now inside custom_module directory create two files custom_module.info and custom_module.module




1. custom_module.info
 
; $Id$
name = "Custom module"
description = "Move user picture fieldset to a new tab 'Picture'."
core = 6.x
version = 6.1

2. custom_module.module
 
<?php
// $Id$

/**
* Implementation of hook_user()
*/
function custom_module_user($op, &$edit, &$account, $category = NULL) {
    switch($op) {
      case 'categories':
        $categories = array();
        $categories['picture'] = array(
          'name' => 'picture',
          'title' => 'Picture',
          'weight' => 5,
        );
        return $categories;            
        break;
      case 'form':
          if ($category == 'picture') {  
              $form['_category'] = array('#type' => 'value', '#value' => $category);
              $form['_account'] = array('#type' => 'value', '#value' => $account);
                  
              if (variable_get('user_pictures', 0)) {
                  $form['picture'] = array('#type' => 'fieldset', '#title' => t('Picture'), '#weight' => 1);
                  $picture = theme('user_picture', (object)$edit);
                   if ($edit['picture']) {
                      $form['picture']['current_picture'] = array('#value' => $picture);
                      $form['picture']['picture_delete'] = array('#type' => 'checkbox', '#title' => t('Delete picture'), '#description' => t('Check this box to delete your current picture.'));
                   }
                   else {
                      $form['picture']['picture_delete'] = array('#type' => 'hidden');
                   }
                  $form['picture']['picture_upload'] = array('#type' => 'file', '#title' => t('Upload picture'), '#size' => 48, '#description' => t('Your virtual face or picture. Maximum dimensions are %dimensions and the maximum size is %size kB.', array('%dimensions' => variable_get('user_picture_dimensions', '85x85'), '%size' => variable_get('user_picture_file_size', '30'))) .' '. variable_get('user_picture_guidelines', ''));
                  $form['#validate'][] = 'user_profile_form_validate';
                  $form['#validate'][] = 'user_validate_picture';
                  $form['#uid'] = $account->uid;
              }
              return $form;
          }
          break;

      case 'submit':
          if($category == 'picture') {
          return _user_edit_submit((isset($account->uid) ? $account->uid : FALSE), $edit);
          }
          break;
                 
  }
}

/**
* Implementation of hook_form_alter()
*/
function custom_module_form_alter(&$form, $form_state, $form_id) {
    switch ($form_id){
        case 'user_profile_form':
            if(arg(3) == NULL) {
          unset($form['picture']);
            }
            break;
    }
}

No comments:

Post a Comment