2

I have two profile2 profiles defined - main and customer_profile. Also, I have a node type called Customer.

When creating a new Customer node, I would like to load the custom_profile form. The idea is to create a node and a profile simultaneously.

I know it's definately a hook_form_alter solution but can someone tell me how to programmatically load a profile while creating or editing a Customer node.

sisko
  • 9,604
  • 20
  • 67
  • 139

5 Answers5

4

You can load profile type and data by using these function

$types = profile2_get_types();
profile2_load_by_user($account, $type_name = NULL)

For Example :

$types = profile2_get_types();
    if (!empty($types)) {
        foreach ($types as $type) {
            $profile = profile2_load_by_user($uid, $type->type);
        }
    }
Nitesh Agarwal
  • 213
  • 1
  • 5
0

Even if you are able to load the customer_profile form, you will need to handle the values separately as they are two different nodes.

I would suggest capturing those fields in the customer node form, and then create a customer_profile programmatically from the values.

If you want to get the profile2 form itself then you can use something like

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');

and then add that to the form you want to place it in.

Gokul N K
  • 2,428
  • 2
  • 32
  • 40
0

You can load full profile data using profile2_load_by_user(); params like:-

profile2_load_by_user($account,$type_name)
$account: The user account to load profiles for, or its uid.
$type_name: To load a single profile, pass the type name of the profile to load

So code like bellow

$account->uid = $existingUser->uid;
$type_name = 'user_about';
$profile =  profile2_load_by_user($account, $type_name);
//$profile variable have full data of profile fields
//changing data profile2 fields 
if(isset($_POST['field_user_first_name'])&& !empty($_POST['field_user_first_name'])){
    $profile->field_user_first_name['und'][0]['value'] = $_POST['field_user_first_name'];
}
profile2_save($profile);
Ajay Gadhavana
  • 415
  • 5
  • 5
0

Well When creating a new Profile , Profile2 fields are not visible until a manual save is done.

To Automatically create the profile2 object , We use rules Module
Step
1) Go to Drupal admin/config/workflow/rules
2) create new rule
3) Give a name and Select in react/event "After saving a new user account"
4) Action,>> Add Action >> Execute custom PHP code
5) insert php code $profile = profile_create(array('type' => 'profile2 type machine name', 'uid' => $account->uid)); profile2_save($profile);
6)Save >> Save changes.
This will create profile2 field when a new user is Created.

user3727574
  • 136
  • 1
  • 8
-1

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.

Francisco Luz
  • 2,775
  • 2
  • 25
  • 35