Entity API provides wrapper classes that make dealing with the values of an entity's properties and fields easier. This post is to make the information easy to learn by examples.
Load wrapper
Source code viewer
// Load node and wrapper. $nid = 7; $wrapper = entity_metadata_wrapper('node', $nid); // OR if you already have the node object. $node = node_load($nid); $wrapper = entity_metadata_wrapper('node', $node);Programming Language: PHP
New entity
Source code viewer
global $user; $node->uid = $user->uid; $node_wrapper = entity_metadata_wrapper('node', $node); $node_wrapper->title = 'My Test Node'; $node_wrapper->field_autosaved_chapter->set($_POST['nid']); $node_wrapper->field_autosaved_body->value = $_POST['content']; $node_wrapper->save();Programming Language: PHP
General methods
Source code viewer
// Get title or name ($node->title, $user->name, etc.). $wrapper->label(); // Get id ($node->nid, $user->uid, etc.). $wrapper->getIdentifier(); // Get type or bundle ($node->type, $entity->bundle). $wrapper->getBundle(); // Get fields and other properties of the entity. $wrapper->getPropertyInfo(); // IN PROGRESS...Programming Language: PHP
Properties
Source code viewer
// Is "Promoted to front page". $is_promoted = $wrapper->promote->value(); // Is "Sticky at top of lists". $is_sticky = $wrapper->sticky->value(); // IN PROGRESS...Programming Language: PHP
Fields
Seems that multivalue field returns multiple values as an array. Single value field return directly the single value.Avoid fatal errors
Source code viewer
// NB: If your object property does not exist, you will get a fatal error. To avoid this you can control the existence of it. if ($wrapper->__isset('body')) { // ... }Programming Language: PHP
Longtext field / wysiwyg (textarea with text-format-enabled)
Source code viewer
// Array of: value, safe_value, format, and optionally summary + safe_summary. $wrapper->body->value(); // Filtered value. $wrapper->body->value->value(); // Unfiltered value. $wrapper->body->value->raw(); // Text format. $wrapper->body->format->value(); // Set value. $wrapper->body->value = 'new value'; $wrapper->body->set(array('value' => $body['value'], 'summary' => $body['summary'], 'format' => 'full_html'));Programming Language: PHP
Longtext field (textarea with text-format-disabled)
Source code viewer
$wrapper->field_textarea->value(); $wrapper->field_textarea->set('Long text....');Programming Language: PHP
Text field
Source code viewer
$wrapper->field_textfield->value(); $wrapper->field_textfield = 'Short text....';Programming Language: PHP
Taxonomy field
Source code viewer
$wrapper->field_taxonomy->value(); $tid = $wrapper->field_taxonomy->raw();Programming Language: PHP
Checkbox field
Source code viewer
// Returns boolean. $wrapper->field_checkbox->value();Programming Language: PHP
Field Collection field
IN PROGRESS...Source code viewer
foreach ($wrapper->field_authors->value() as $field_collection_item) { // ... } }Programming Language: PHP
Examples
Source code viewer
// Load user from external login system and save mail that is the same as username. $account = user_external_load($email); $wrapper = entity_metadata_wrapper('user', $account); $wrapper->mail->set($wrapper->label()); $wrapper->save();Programming Language: PHP