3

I'm trying to add profile2 fields in my custom form. I have created a path in my custom module in hook_menu() and in page arguments inside array, I have passed my custom_form.

In my custom_form I have returned the following code:

function custom_form($form, &$form_state) {

    global $user;
    module_load_include('inc', 'user', 'user.pages');
    return drupal_get_form('user_profile_form', $user); 
}

When I go to the path I do not see the fields which I had created for a particular profile type through manage fields. Instead it renders the default user-profile-form fields.

Where I'm going wrong? Please suggest any ideas.

Thanks In advance

2

Solved it with the following code:

function custom_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;
}

profile2_attach_form($form, &$form_state);

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

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.