Entity metadata wrappers
The entity API provides some wrapper classes you may use to easily deal with entities and to leverage the entity property information modules provided. With the help of the wrappers you can access the property information, loop over known properties, or just get/set the described data values, etc.
These are some simple usage examples as found in the README:
For making use of this information (metadata) the module provides some
wrapper classes which ease getting and setting values. The wrapper supports
chained usage for retrieving wrappers of entity properties, e.g. to get a
node author's mail address one could use:
$wrapper = entity_metadata_wrapper('node', $node);
$wrapper->author->mail->value();
To update the user's mail address one could use
$wrapper->author->mail->set('[email protected]');
or
$wrapper->author->mail = '[email protected]';
The wrappers always return the data as described in the property
information, which may be retrieved directly via entity_get_property_info()
or from the wrapper:
$mail_info = $wrapper->author->mail->info();
In order to force getting a textual value sanitized for output one can use,
e.g.
$wrapper->title->value(array('sanitize' => TRUE));
to get the sanitized node title. When a property is already returned
sanitized by default, like the node body, one possibly wants to get the
not-sanitized data as it would appear in a browser for other use-cases.
To do so one can enable the 'decode' option, which ensures for any sanitized
data the tags are stripped and HTML entities are decoded before the property
is returned:
$wrapper->body->value->value(array('decode' => TRUE));
That way one always gets the data as shown to the user. However if you
really want to get the raw, unprocessed value, even for sanitized textual
data, you can do so via:
$wrapper->body->value->raw();
More examples:
$wrapper->body->set(array('value' => "content"));
$wrapper->field_text[0] = 'the text';
$wrapper->field_text[0]->set(array('value' => "content"));
$wrapper->field_text2->summary = 'the summary';
$wrapper->field_text2->value = 'the text';
$wrapper->save();
$wrapper->delete();
More docs: http://drupal.org/node/1021556