2

This is what I use to create the user:

    $new_user = array(
        'name' => $form_state['values']['email'],
        'status' => 1,
        'access' => REQUEST_TIME,
        'roles' => array(),
    );

   user_save('', $new_user);

Now I want to add values to the field collection items. I have a field collection called field_personal_details and a field collection item called field_firstname, but I have no idea how to access it. Can someone help me get started?

2

I think following code should give you an idea.

    //Create a field collection entity
    $field_collection_item = entity_create('field_collection_item', array('field_name' => 'field_personal_details'));
    $field_collection_item->field_firstname[LANGUAGE_NONE][0]['value'] = "Yassin Tahtah";        
    //$field_collection_item->field_firstname['und'][0]['value'] = "Yassin Tahtah";
    $field_collection_item->setHostEntity('user', $user);
    // Save the entity.
    $field_collection_item->save(TRUE);
2

You can see this example:

$new_user = array(
    'name' => $form_state['values']['email'],
    'status' => 1,
    'access' => REQUEST_TIME,
    'roles' => array(),
);

 //making the field collection
 $values = array();
 $values['field_name'] = 'field_personal_details';
 $values['field_firstname'][LANGUAGE_NONE][0]['value'] = 'Yassin';

 //saving the collection
 $entity = entity_create('field_collection_item', $values);
 $entity->setHostEntity('user', $user);
 $entity->save();    

 //saving the user
 user_save('', $new_user);

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.