11

Profile2 is a module that uses entities to create profiles that are separate from the user account.

From within my module, I want to be able to display a form to edit the profile entities. Possibly using drupal_get_form or maybe Profile2's API or any other method.

What is the best way to go about this?

20
+50

I did something like this recently. Since profile uses fields, this make things pretty simple. For the form you could do something like this:

function my_profile_form($form, &$form_state) {
  global $user;
  if (!isset($form_state['profiles'])) {
    $profile = profile2_load_by_user($user, 'profile_machine_name');
    if (!$profile) {
      $profile = profile_create(array(
        'type' => 'profile_machine_name',
        'uid' => $user->uid
      ));
    }
    $form_state['profiles'][$profile->type] = $profile;
  }

  // Use field attach form and handle the fields yourself:
  field_attach_form('profile2', $profile, $form, $form_state);
  // Or use profile2 API which is simpler
  profile2_attach_form($form, $form_state);

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  return $form;
}

Since all the profile form only is the fields that are attached to the profile, you can simply attach the fields yourself to the form, using Drupal core APIs:

  • field_attach_form adds the fields to the form.
  • field_attach_validate handles validation.
  • field_attach_submit to handles adding the values to the entity (profile).
  • Lastly you would need to save the profile entity using profile2_save.

After browsing the profile2 module code, I found that it provides a wrapper function to attach the fields to the form and save the form. This is simpler, but in doing so, you loose a little control. To use this you need to use the profile2_attach_form. Doing this will also handle the validation and saving of data.

To use the above code you should be able to c/p it, rename the form and replace profile_machine_name with the actual machine name of the profile you want to display the form for.

  • I don't understand what's happening here. I just need to display the Profile2 form. Is this possible? – Carlos Muñiz Jun 23 '11 at 23:06
  • when I use your code in my module it gives me this error: Fatal error: Unsupported operand types in C:\wamp\www\working\modules\field\field.attach.inc on line 550 – Carlos Muñiz Jun 24 '11 at 2:47
  • @Charlie I've update my answer with more details and provided an alternative and simpler solution using profile2 API. – googletorp Jun 24 '11 at 5:58
  • this is it googletorp!!! your answer worked with a minor edit: at the end of the function I added: return $form that was the only way I could get it to display by calling it with drupal_get_form(). If there was an alternative way to display it without the return $form then please explain, otherwise thank you very much! – Carlos Muñiz Jun 24 '11 at 10:43
  • I have to wait 12 hours to award the bounty :) – Carlos Muñiz Jun 24 '11 at 10:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.