Set field values using entity metadata wrapper in Drupal7

entity metadata wrapper

This entity metadata wrapper class is provided by the Entity API module. By using entity metadata wrapper class we can set new fields data dynamically to node and also we can get node fields data by using entity metadata wrapper class

Retrieve values from a node using entity metadata programmatically.

To retrieve field values from node first you need to pass node id in entity metadata wrapper then wrapper calls can store all fields data in the object. By using this object get all fields data

Set Single value field using entity metadata wrapper programmatically

Set values code programmatically example and code

$wrapper_node = entity_metadata_wrapper('node', $node);
$wrapper_node->field_name->set('New Value')

Set multiple values or unlimited values field using entity metadata wrapper programmatically

Set values code programmatically example and code

$wrapper_node = entity_metadata_wrapper('node', $node);
$nids_set = array(201, 754);
$wrapper_node->field_name->($nids_set);

for set multiple(unlimited) values to the node must pass an array in a set function

 syntax: $wrapper_node->field_name->set(array(12,45,78)); 
start a project with us