Based on your example, the could should be:
global $user;
$usr_wrapper = entity_metadata_wrapper('user', $user);
// Create the collection entity and set its "host".
$field_collection_item = entity_create('field_collection_item', array('field_name' => 'field_user_location')); // Name of collection field.
$field_collection_item->setHostEntity('node', $usr_wrapper->value());
// Now define the collection parameters.
$collection = entity_metadata_wrapper('field_collection_item', $field_collection_item);
$collection->field_land_phone = "0123"; // array("0123") in case of multi-valued field.
// $collection->field_land_phone->set("0123"); // Version using set(), use: set(array("0123")) for multi-valued field.
// Save.
$collection->save(); // Or: $usr_wrapper->save();
Also check Entity metadata wrappers documentation page (check for Example using field collections).
Here is another example which adds an entry to a node with a field collection named field_activity_log
:
// Load the controller class file.
module_load_include('inc', 'entity', 'includes/entity.controller');
// Use the currently logged in user if a $uid argument is not defined.
global $user;
if (empty($uid)) {
$uid = $user->uid;
}
$now = gmdate('Y-m-d H:i:s'); // Grab the current time.
// Setup the values in the structure expected by the field_collection entity.
$values = array(
'field_name' => 'field_activity_log',
'field_activity_log_date' => array(
LANGUAGE_NONE => array(array('value' => $now)),
),
'field_activity_log_message' => array(
LANGUAGE_NONE => array(array('value' => $message)),
),
'field_activity_log_user' => array(
LANGUAGE_NONE => array(array('uid' => $uid)),
),
);
$entity = entity_create('field_collection_item', $values);
// Attach the field_collection entity to the application node. This has to
// happen separately so that it actually works -- you can't just specify
// this via the $values array.
$entity->setHostEntity('node', $node);
// Save the entity. Since it's attached to the right application node, this
// will both create the field_collection entity and update the application
// node to point to the new field_activity_log record.
$entity->save();
?>
Source: Example: Programmatically adding field collection entries to a node
And this example shows updating (loading/editing/saving) an existing field collection in an Entity Wrapper:
<?php
// Load the node by NID
$raw_node = node_load($nid);
// Wrap it with Entity API
$node = entity_metadata_wrapper('node', $raw_node);
// Get the first item from the muli-value field collection
$raw_collection = $node->field_collection->value();
// Wrap it with Entity API
$collection = entity_metadata_wrapper('field_collection_item', $raw_collection);
//dsm the old value
dsm($collection->field_example->value());
// Set a new value on the field_example textfield.
$collection->field_example = 'New value';
// Save the changes to the entity
$collection->save();
?>
Source: Example: Programmatically create and update field collection with Entity API
Troubleshooting
In case of error when setting the data value with set()
, be be sure it matches the required data type and format. If your entity won't get saved, check for the logs for any wrapper exceptions (drush ws --tail
).
See also: Modifying a field collection programmatically missing hostEntity fields at SO