HI all,

I seem to have a problem with changing the layout of user profile pages. By default if we visit our own user page we get a "View" tab and a "Edit" tab. In the Edit tab we will get as many sub tab menus as there are categories defined by the Profile module (ie categories which identify groups of fields defined in the User Profile settings pages )

The main things that I would like to do are:
1) Place the image picture into a different tab and not the default "Account Settings" tab. So say into a new tab called "User Image" (and I cant seem to get this going.

2) Place location information provided by the location module into a different tab (ie: settings for house, street, country etc etc )

3) Be able to move around certain parts/fields of the user profile around. Note: these fields may be either provided by a third party module or maybe fields defined by the profile.module.

I have been looking for help in the community with some response (see http://drupal.org/node/254911 )

My actual situation is that I have a module which implements the _menu_hook() and by changing the "pathways" allows the creation of new profile tabs. The idea is that if I define a path in drupal like "user/$uid/edit/extra_tab" this will be automatically recognized and a tab "extra_tab" will be created.

Another option is to use the _user_hook() but I still am not that pro to fully appreciate how this could be used exactly..

My code at the moment is the following in where both methods are tried but for which I still cannot wrap up and get it to work, I would really appreciate if somebody could help me with the missing parts.

At the moment I am just trying to implement point 1) above and place the image in a different tab but I still cannot. The image, with the _hook_menu() method, does get displayed in a new tab (still not removed from the default tab though) but when I enter an image in the upload field and submit nothing happens. It seems as if it is doing something but then, no image. (If I do it under the standard "Account Settings" image upload field the image uploads flawlessly and is viewable, so no Image related problems)

Here is my current code for the costum module I needed to write, it is also in the referenced post:

<?php
// $Id: user_profile_menu.module,v 1.745.2.26 2008/01/07 02:30:35 drumm Exp $

/**
* Implementation of hook_menu().
*/

function user_profile_menu_menu($may_cache) {
  global
$user;

 

$items = array();
/**/
 
$admin_access = user_access('administer users');
 
$access_access = user_access('administer access control');
 
$view_access = user_access('access user profiles');

  if (

$may_cache) {

   

// Your personal page
   
if ($user->uid) {
     
$items[] = array('path' => 'user/'. $user->uid, 'title' => t('My account'),
       
'callback' => 'user_view', 'callback arguments' => array(arg(1)), 'access' => TRUE,
       
'type' => MENU_DYNAMIC_ITEM);
    }

   

$items[] = array('path' => 'logout', 'title' => t('Log out'),
     
'access' => $user->uid,
     
'callback' => 'user_logout',
     
'weight' => 10);
  }

  else {
   

// Add the CSS for this module. We put this in !$may_cache so it is only
    // added once per request.
   
drupal_add_css(drupal_get_path('module', 'user') .'/user.css', 'module');
    if (
$_GET['q'] == 'user' && $user->uid) {
     
// We want to make the current user's profile accessible without knowing
      // their uid, so just linking to /user is enough.
     
drupal_goto('user/'. $user->uid);
    }

    if (

arg(0) == 'user' && is_numeric(arg(1)) && arg(1) > 0) {
     
$account = user_load(array('uid' => arg(1)));

      if (

$user !== FALSE) {
       
// Always let a user view their own account
       
$view_access |= $user->uid == arg(1);
       
// Only admins can view blocked accounts
       
$view_access &= $account->status || $admin_access;

       

$pic_category = array('name' => "picture" , 'title' => 'Picture', 'weight' => 2 );
       
$categories = _user_categories($account) ;
       
array_push$categories, $pic_category )  ;

        if (

arg(2) == 'edit') {
          if (
$categories && (count($categories) > 1)) {

            foreach (

$categories as $key => $category) {

             

$items[] = array(
               
'path' => 'user/'. arg(1) .'/edit/'. $category['name'],
               
'title' => $category['title'] ,
               
'type' => $category['name'] == 'account' ? MENU_DEFAULT_LOCAL_TASK : MENU_LOCAL_TASK,
               
'weight' => $category['weight'],
               
'access' => ($admin_access || $user->uid == arg(1)));
            }
          }
        }
      }
    }

    

$items[] = array('path' => 'user/'. arg(1) .'/edit/image', 'title' => t('Image'),
       
'callback' => 'user_profile_menu_edit_form', 'callback arguments' => array( 'user_edit'),
'access' => $user->uid == arg(1) , 'type' => MENU_LOCAL_TASK);

  }

  return

$items;
}

/**
* Implementation of hook_user().
*/

function user_profile_menu_user($op, &$edit, &$account, $category = NULL) {

  if (

$op == 'form' ) {

// Picture/avatar:
 
if (variable_get('user_pictures', 0) && !$register) {
   
$form['picture'] = array('#type' => 'fieldset', '#title' => t('Picture'), '#weight' => 1);
   
$picture = theme('user_picture', (object)$edit);
    if (
$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', ''));
  }
 
return
$form;
  }

  if ((

$op == 'update' || $op == 'insert' || $op == 'submit') ) {
return
_user_edit_submit(arg(1), $edit);
  }
}

function

user_profile_menu_edit_form($uid, $edit, $register = FALSE) {
 
$admin = user_access('administer users');

 

// Account information:
 
$form['account'] = array('#type' => 'fieldset',
   
'#title' => t('Account information'),
  );

 

// Picture/avatar:
 
if (variable_get('user_pictures', 0) && !$register) {
   
$form['picture'] = array('#type' => 'fieldset', '#title' => t('Picture'), '#weight' => 1);
   
$picture = theme('user_picture', (object)$edit);
    if (
$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 = _user_forms($edit, $account, $category);
 
$form['_category'] = array('#type' => 'value', '#value' => $category);
 
$form['_account'] = array('#type' => 'value', '#value' => $account);
 
$form['submit'] = array('#type' => 'submit', '#value' => t('Submit'), '#weight' => 30);
  if (
user_access('administer users')) {
   
$form['delete'] = array('#type' => 'submit', '#value' => t('Delete'), '#weight' => 31);
  }
 
$form['#attributes']['enctype'] = 'multipart/form-data';

//  return $form;
 
$output = drupal_render(theme('table', $form['header']['#value'], $rows));
 
$output.= drupal_render($form); // Process any other fields and display them
 
return $output;
}

function

generate_user_picture_form(){

// Picture/avatar:
 
if (variable_get('user_pictures', 0) && !$register) {
   
$form['picture'] = array('#type' => 'fieldset', '#title' => t('Picture'), '#weight' => 1);
   
$picture = theme('user_picture', (object)$edit);
    if (
$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['_category'] = array('#type' => 'value', '#value' => $category);
 
$form['_account'] = array('#type' => 'value', '#value' => $account);
 
$form['submit'] = array('#type' => 'submit', '#value' => t('Submit'), '#weight' => 30);
  if (
user_access('administer users')) {
   
$form['delete'] = array('#type' => 'submit', '#value' => t('Delete'), '#weight' => 31);
  }
 
$form['#attributes']['enctype'] = 'multipart/form-data';

 

$output = drupal_render(theme('table', $form['header']['#value'], $rows));
 
$output.= drupal_render($form); // Process any other fields and display them
 
return $output;

}

?>

Please help, I have been really stuck on this

Comments

yasir farooqui’s picture

Hi

may be not exactly the solution for your problem but may it help you in solving the problem.
if you open 'user.module', there is function 'user_edit_form', all the form fields are defined there. There you can see the code for placing the image stuff. Now you need to create any module with any name

function modulename_form_alter(&$form, $form_state, $form_id){
        if($form_id=='user_profile_form'){
               $queryStringArray=explode('/',$_GET['q']);
               if(isset($queryStringArray[3]) && $queryStringArray[3]=='About me'){
               //place your own logic here to determine that on which tab you want to place image stuff,
              //remember that for each custom profile category tab, the category name is added in the query string so you                    
              //can easily determine it

             //now place the code for picture stuff from user.module
                    $form['picture'] = array('#type' => 'fieldset', '#title' => t('Picture'), '#weight' => 5);
    $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_validate_picture';
               }
               else{
                               unset($form['picture']); //use this if you want to disable picture stuff on some tab
               }
       }
}

Please let me know if I was not able to explain it well. You can proceed using the same login for other tabs and form fields

mariusooms’s picture

Hi...I've tried your code suggestion as it is exactly what I'm trying to do. I had much already figured out. The original form is unset correctly. The part that doesn't work is:

<?php
...
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');
     }
...
?>

Nothing gets displayed. I'm wondering if also hook_user needs to be implemented for the new tab?

Regards,

Marius

mariusooms’s picture

The upload function works as expected. It is only the current_picture and delete_picture that don't show. When I remove the if condition $edit['picture'] they do show, but don't work as expected. I appreciate any help.

Regards,

Marius

mariusooms’s picture

I also found that the upload fails to append the user id in the new picture. So, picture-17.jpg (where 17 is the uid) turns into picture-.jpg. This is bad for obvious reasons, since everyone will have the same user picture :)

With the new form structure it seems like a very tall order to successfully relocate the user picture form. Still if anyone has done this, please shed some light.

Regards,

Marius

Also the correct unset, to make sure you don't hide the form on other tabs as well, would be:

<?php
function mymodule_form_alter(&$form, $form_state, $form_id){
  ...
  else if(
$form_id == 'user_profile_form' && arg(3) == NULL) {
     
// only fire this if it's user/<uid>/edit, not any other category
     
unset($form['picture']);
  }
}
?>
mikewill’s picture

It seems like someone should develop a module (or maybe this should be in the core) to enable a sort of visual template creation, a sort of WYSIWYG of page layouts where you can lay out blocks in sections, custom regions, etc.

loze’s picture

I know this is old, but did you ever figure this out?
Trying to do the same thing with d6

loze’s picture

Well, I figured it out.

with D6, here is what i did, I hope this helps someone.

<?php
// hook_user
function mymodule_user($op, &$edit, &$account, $category = NULL) {

    switch(
$op){
           
            case
'categories':
                if (
variable_get('user_pictures', 0)) {
               
$output[] = array('name' => 'picture',
                                   
'title' => 'Your Picture',
                                   
'weight' => 2,
                                    );
                 return
$output;
                }
                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;
                   
    }
}



// hook_form_alter
function mymodule_form_alter(&$form, $form_state, $form_id) {
    switch (
$form_id){
        case
'user_profile_form':
            if(
arg(3) == NULL) {
          unset(
$form['picture']);
            }
            break;
    }
}
?>
SamSound’s picture

what to do with this?

Ive created a new module. put this in it. it takes the pictures of the user/%/edit page. But doesnt create a new tab, and user page always comes back with access denied

??

drupalina’s picture

I wonder if someone would be kind enough to post a solution to this for 5.x

I've seen many Drupal sites achieving this, but no workable write-ups.

Ultimately, it would be best if there was no core-hacking involved -- just a module that one enables and that's it.

Thanks!