Skip to content

Instantly share code, notes, and snippets.

@olragon
Last active November 25, 2015 16:13
Show Gist options
  • Save olragon/a1298d381e1710cf2346 to your computer and use it in GitHub Desktop.
Save olragon/a1298d381e1710cf2346 to your computer and use it in GitHub Desktop.
Drupal's entity_metadata_wrapper examples
// @see https://www.drupal.org/node/2378611#comment-9360381
$entity = entity_create('node', $values);
$wrapper = entity_metadata_wrapper('node', $entity);
$wrapper->field_my_term_reference->set(123);
$wrapper->save();
$term = new stdClass();
$term->name = 'Drupal';
$term->vocabulary_machine_name = 'cms'; // made wrapper happy
// wrap
$wrap = entity_metadata_wrapper('taxonomy_term', $term);
// made wrapper really save vid
$wrap->vocabulary->set('cms');
// set field value
$wrap->field_version->set('7.16');
// save
$term->save();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment