A recent blog post by Trellon had some examples that are far better than the ones in README.txt

// Create wrapper around the node.
$wrapper = entity_metadata_wrapper('node', $node);

// We can do it also using only $nid.
$wrapper = entity_metadata_wrapper('node', $nid);

// Get the value of field_name of the nodes author's profile.
$wrapper->author->profile->field_name->value();
$wrapper->author->profile->field_name->set('New name');

// Value of the node's summary in german language.
$wrapper->language('de')->body->summary->value();

// Check whether we can edit node's author email address.
$wrapper->author->mail->access('edit') ? TRUE : FALSE;

// Get roles of node's author.
$wrapper->author->roles->optionsList();

// Set description of the node's first file in field field_files.
$wrapper->field_files[0]->description = 'The first file';
$wrapper->save();

// Get node object.
$node = $wrapper->value();

Comments

danielnolde’s picture

There seems to be some error or misunderstanding in one of Yuri's code examples:

// Get roles of node's author.
$wrapper->author->roles->optionsList();

This actually doesn't get the entity's "authors" 's roles, but a list of all values applicable to the roles property (i.e. _all_ roles defined in the system, not given to that user!). The correct statement for the purpose stated in the comment would be:

$wrapper->author->roles->value();

Sadly, this then gets only an array of the role's RIDs, and not an associative array in the form of RID=>rolename as we'd get from a simple $user->roles - sometimes using EntityMetadataWrapper makes life more complex instead of simpler... <;)

prabhatjn’s picture

What could be a good way to read $wrapper.
dsm does not work so good here.

P.

joachim’s picture

$wrapper's properties are protected so dsm() doesn't see them.

You can temporarily hack some of those to be public if you're debugging.

But most of the stuff you might be interested in is lazy loaded, so won't be there anyway.

What's useful to dsm though is $wrapper->getPropertyInfo() which shows you all the properties on the wrapper.

antonio_pantano’s picture

// Value of the node's summary in german language.
$wrapper->language('de')->body->summary->value();

I think it will not give you the translated body text.
As far as I know, Drupal creates 2 versions of the node. The first one is the original, say in English, and has a nod id, say 100. The translated version has its own node id, lets suppose 200 (a distinct node) , so when you load the node with the id 100 you are wrapping the english node, so calling $wrapper->language('de')->body->summary->value() will still result in the original (English) node and no translated version will be available since the german node has the nid 200. To work on it you have to use $wrapper_de = entity_metadata_wrapper('node', [nid of the german node]);

Rahul2386’s picture

Issue summary: View changes
            $i =0;
            $wrapper = entity_metadata_wrapper('node', $node);
            $items = field_get_items('node', $node, 'field_lifecycle_items');
            foreach ($items as $item) 
            {
             
                $entity_list = field_collection_field_get_entity($item);
                $node_id = $entity_list->field_link['und']['0']['url'];
                $node_id_subsrt =drupal_substr($node_id, '5',strlen ($node_id ));
                $child_node = node_load($node_id_subsrt);
                $child_node_id = $child_node->nid;
                $child_trans = translation_node_get_translations($child_node->tnid);
				foreach ($child_trans as $key => $value)
				{
					if($key == $language_code)
					{
						$entity_list->field_link['und']['0']['url'] = 'node/'.$value->nid;
						node_save($value);
						$node_id_trans = node_load($value->nid);
						$revision_uid = $node_id_trans->revision_uid;
						$items[$i]['value'] = $revision_uid;
						$items[$i]['revision_id'] = $revision_uid;
					}
				}
                $i=$i+1;
            }
            $wrapper->field_lifecycle_items= $items;
            $wrapper->save();
	    node_save($node);   

Hello

I am working on above code but I am unable to save $wrapper I am getting exception as below

exception 'Exception' with message 'The host entity cannot be changed.' in /mnt/www/html/konecranesdev/docroot/sites/all/modules/contrib/field_collection/field_collection.module:359 Stack trace: #0 /mnt/www/html/konecranesdev/docroot/sites/all/modules/contrib/field_collection/field_collection.module(1031): FieldCollectionItemEntity->updateHostEntity(Object(stdClass)) #1 /mnt/www/html/konecranesdev/docroot/modules/field/field.attach.inc(209): field_collection_field_update('node', Object(stdClass), Array, Array, 'und', Array, NULL, NULL)

sandboxpl’s picture

What about adding a link to documentation page in README.txt ? A lot of examples can be found here:
https://www.drupal.org/docs/7/api/entity-api/entity-metadata-wrappers