Skip to content

Instantly share code, notes, and snippets.

@guoxiangke
Created October 9, 2012 09:51
Show Gist options
  • Save guoxiangke/3857700 to your computer and use it in GitHub Desktop.
Save guoxiangke/3857700 to your computer and use it in GitHub Desktop.
Entity metadata wrappers
<?php
// 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();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment