Hello,

I'm currently trying to load the edit form for the logged-in user's profile2 profile, but I'm getting some unexpected results.

I'd really appreciate any insight into this problem.

My current code sort of works:

    $profile2 = profile2_by_uid_load($uid, 'seeker_profile');
    $entity_form = entity_ui_get_form('profile2', $profile2);
    dsm($entity_form);

I've also tried:

$profile_form = drupal_get_form('profile2_edit_seeker_profile_form', $user);
dsm($profile_form);

Both of these approaches return the same result. This loads the profile2 form, but strangely, the returned array only contains about half of the fields! Worse, the fields aren't populated with the user's profile values.

Now I know that I simply use profile2_by_uid_load() to get the profile object, but that doesn't really suit my purposes. Is there any way to actually load the form the same way that menu router '$items[$path . '/%profile2_by_uid/edit']' does?

I can't figure out what ingredient I'm missing here.

Thank you!

Comments

grasmash’s picture

Ok, well maybe this is a little less strange. I realized that really, the profile isn't being attached at all. It only seemed that some fields were being attached because I was modifying fields with hook_form_profile2_edit_seeker_profile_form_alter(), and those were being built out by Drupal (without values).

Here's the trouble... I can't use profile2_attach_form() because I want to pull up $profile_form as a php variable that is completely separate from the $form and $form_state that are loaded onto the page by default and passed around by reference.

I need to keep $form and $profile_form separate. Really not sure how to do this. Ideas?

grasmash’s picture

Status: Active » Closed (fixed)

Geeeezzz. I figured it out. What's really annoying is that I tried this solution initially but I forgot to clear the caches when testing it, so I didn't realize that it was working.

    module_load_include('inc', 'profile2_page', 'profile2_page');
    $profile2 = profile2_by_uid_load($uid, 'seeker_profile');
    $entity_form = entity_ui_get_form('profile2', $profile2, 'edit');
    dsm($entity_form);

Had to load profile2_page.inc!

Thanks anyway.

garciac1212’s picture

Where does this pecie of snippet go? In the template.php file?
And If I want to theme say the labels or rewrite them, how will that look. With node forms it looks something like this:

<?php $form['group_place']['group_information']['field_hours_of_operation']['und']['0']['value']['#resizable'] = FALSE; 

// dsm($form)
print drupal_render_children($form);
?>

FranciscoLuz’s picture

I had a similar need for creating a custom tab at the user page and loading the user profile2 form in it.

Here is a snap code of how I managed to accomplish just that:

MYMODULE.module https://gist.github.com/4223234
MYMODULE_profile2_MYPROFILE2TYPE.inc https://gist.github.com/4223201

Hope it helps.

wasid’s picture

Issue summary: View changes

This is the code which actually works in basic profile2 form setup though it got some issues with ajax loading!

<?php
  global $user;
  module_load_include('inc', 'profile2_page', 'profile2_page');
  $profile2 = profile2_load_by_user($user->uid, 'profile2_machine_name');

  if (!$profile2) {
    $profile2 = profile2_create(array('type' => 'profile2_machine_name'));
    $profile2->setUser($user);
    $profile2->is_new = TRUE;
    $profile2->save();
    $profile2 = profile2_load_by_user($user->uid, 'profile2_machine_name');
  }

  $data = entity_ui_get_form('profile2',  $profile2, 'edata);

  return $data;
?>
katerega’s picture

change seeker_profile to your profile machine name