Various EntityMetadataWrapper Examples

Public

Get the entity object back out of the wrapper.

Get raw version
php
  1. $wrapper = entity_metadata_wrapper('node', $nid);
  2. $node = $wrapper->value();

Working with Text field data.

Get raw version
php
  1. $old_value = $wrapper->field_my_text->value();
  2. $wrapper->field_my_text = 'new value';

Working with Longtext field data.

Get raw version
php
  1. $wrapper->body->value(); // Array of: value, safe_value, format, and summary + safe_summary.
  2. $wrapper->body->value->value(); // Filtered value.
  3. $wrapper->body->value->raw(); // Unfiltered value.
  4. $wrapper->body->format->value(); // The selected text format.
  5. $wrapper->body->value = 'new value';

Working with Link field data.

Get raw version
php
  1. $wrapper->field_my_link->value(); // Array of: url, title, attributes.
  2. $wrapper->field_my_link->url->value(); // Hyperlink destination.
  3. $wrapper->field_my_link->title->value(); // Hyperlink text.
  4. $wrapper->field_my_link->attributes->value(); // Array of: target, title, etc.
  5. $wrapper->field_my_link->url = 'http://mediacurrent.com';
  6. $wrapper->field_my_link->title = 'Do Drupal Right';

Deleting a value.

Get raw version
php
  1. $wrapper->field_foobar = NULL;

Comments

Ehsan's picture

Thank you!