Hi,

I have a feature request, which I think is truly appropriate to make this nice module great. Please consider this request. I have a requirement where I want account and profile edit forms on a single page. I have different profile categories which link to different roles so let say there is a role of student, which has a profile category Student and there is another role teacher having profile category Teacher. Now I want a single edit page where they have typical account edit form and after this form another form which obviously Profile form according to the selected category of respective user. so for Student account edit page has Account edit form and after this form profile edit form for category Student. If we have more than one category associated with this user role It can be shown weight wise. Hope I am able to clarify what I am talking about. I am also attaching a jpg to further illustrate. Hope to hear from you soon. Thoughts please.

Regards,

Adnan Haider

Comments

fago’s picture

Status: Needs work » Closed (won't fix)

I'd like to avoid featurities in profile2, so features like that should be implemented in external extension modules.

devtherock’s picture

Hi, I was looking for the same, searched for module which can create a single user edit form, but didn't find. So in hook_form_alter I have added

	if ($form_id == 'user_profile_form'){
		$types = profile2_get_types();
	  if (!empty($types)) {
			foreach ($types as $type) {
				$profile = profile2_load_by_user($form['#user'], $type->type);
				if (!empty($profile)) {
			  	$form_state['profiles'][$profile->type] = $profile;
					profile2_attach_form($form, $form_state);
				}
			}
	  }
	}

and it displayed a complete form.

Thanks.

adnanhader’s picture

thanks... good code snippet

kardave’s picture

Thanks!
If it's that simple, I'd vote for patch :)

shadowdknight’s picture

devtherock,
can you provide a patch ?

Thanks

ram4nd’s picture

Improved #2. If the profile wasn't created, the user is made by facebook module for an example, the form wasn't displayed.

if ($form_id == 'user_profile_form') {
  $types = profile2_get_types();
  if (!empty($types)) {
    foreach ($types as $type) {
      $profile = profile2_by_uid_load($form['#user']->uid, $type->type);
      if (!empty($profile)) {
        $form_state['profiles'][$profile->type] = $profile;
        profile2_attach_form($form, $form_state);
      }
    }
  }
}
sliiiiiide’s picture

When I tried any of the above code snippets, the profile2 form fields appeared on the acount edit page but values were not saved on submission. Debugging indicated that the $form_state['profiles'] were not being passed on to profile2_form_submit_handler function in profile2.module. As a quick fix I added a custom submit handler that seems to do the job for me and may help someone else.

function mymodule_form_alter(&$form, $form_state, $form_id)  {

	switch ($form_id)  {
	
		case 'user_profile_form' :
	
		
		//add the profile 2 fields to the user account edit page
		$types = profile2_get_types();
		  if (!empty($types)){
				foreach ($types as $type) {
					$profile = profile2_load_by_user($form['#user'], $type->type);
					if (!empty($profile)) {
					$form_state['profiles'][$profile->type] = $profile;
					
						profile2_attach_form($form, $form_state);
					}
				}
			}
			
			
			//remove profile2 submit handler and add custom handler
			if(($key = array_search('profile2_form_submit_handler', $form['#submit'])) !== false) {
				unset($form['#submit'][$key]);
			}
			$form['#submit'][] = 'mymodule_profile2_submit_handler';
			
		break;
                }
}

function mymodule_profile2_submit_handler($form, &$form_state){
$types = profile2_get_types();
  if (!empty($types)){
		foreach ($types as $type) {
			$profile = profile2_load_by_user($form['#user'], $type->type);
			if (!empty($profile)) {
				foreach($form_state['values']['profile_' . $profile->type] as $field => $value){
					if($value['und'] != '_none'){
						$profile->$field = $value;
					}
				}
				profile2_save($profile);
			}
		}
	}
}
stevenx’s picture

#7 helped me. thanks for sharing your code

ktrev’s picture

the code in #7 helped me to have all the fields in the edit page.But the tabs are still showing.

dellad’s picture

#7 worked for me too. Lifesaver. Thanks so much!!!!

rakesh.gectcr’s picture

Issue summary: View changes

#7 worked for me . Is there any way to reorder the form ?

Romain Trenet’s picture

#7 worked for me too. Thanks ! I'm now searching how to reorder the form and hide the tabs.

oksana-c’s picture

anyone came up with a solution on reordering the form and hiding tabs?

figured it out.

Hide tabs 1) tab tamer module. or 2) place following code in your own module:

<?php 
function your_module_name_menu_alter(&$item) {
  // Hide the "your_profile_type" tab
  $item['user/%/edit/your_profile_type']['type'] = MENU_CALLBACK;
}
?>

Reorder profile field groups -> Each profile type has two weight values, which can be set on "admin/structure/profiles/manage/your_profile" page.

Bill Choy’s picture

Excellent solution.

Getting the fields ordered properly was a pain, because the Managed Fields UI would reset the weight. Had put things into DIV/Fieldsets to hardcode weights, in the hook_form_alter.

    $form['#groups']['group_user_row1']->weight = -9;
    $form['profile_main']['#groups']['group_user_row2']->weight = 2;
ownage’s picture

#7 did the trick, but now my Profile2 Date field isn't working:

Warning: implode(): Invalid arguments passed in date_limit_format() (line 2262 of /sites/all/modules/date/date_api/date_api.module).

Only local images are allowed.

Does anyone know why this might be happening or have any tips I could try out?

ownage’s picture

Is anyone still using this snippet to combine the elements into a single Edit page? This is a widely requested Profile2 feature and I am not sure if this is the best solution out there anymore so wanted to ask.

I removed the Date field from my custom profile, but one warning & notice remains on the Edit screen:

Notice: Undefined index: profiles in profile2_form_validate_handler() (line 428 of /sites/all/modules/profile2/profile2.module).
Warning: Invalid argument supplied for foreach() in profile2_form_validate_handler() (line 428 of /sites/all/modules/profile2/profile2.module).

Line 428 is in the foreach below:

/**
 * Validation handler for the profile form.
 *
 * @see profile2_attach_form()
 */
function profile2_form_validate_handler(&$form, &$form_state) {
  foreach ($form_state['profiles'] as $type => $profile) {
    if (isset($form_state['values']['profile_' . $profile->type])) {
      // @see entity_form_field_validate()
      $pseudo_entity = (object) $form_state['values']['profile_' . $profile->type];
      $pseudo_entity->type = $type;
      field_attach_form_validate('profile2', $pseudo_entity, $form['profile_' . $profile->type], $form_state);
    }
  }
}

I really wish I knew more to fix this. If anyone has anything they can offer, please do!

Thank you for your time!

ashish.verma85’s picture

hi, that can solve this issue,
https://www.drupal.org/project/account_profile
Thanks