You are here

3 February 2015
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
  1. // Load node and wrapper.
  2. $nid = 7;
  3. $wrapper = entity_metadata_wrapper('node', $nid);
  4. // OR if you already have the node object.
  5. $node = node_load($nid);
  6. $wrapper = entity_metadata_wrapper('node', $node);
Programming Language: PHP

New entity

Source code viewer
  1. global $user;
  2.  
  3. $node = entity_create('node', array('type' => $content_type));
  4. $node->uid = $user->uid;
  5. $node_wrapper = entity_metadata_wrapper('node', $node);
  6. $node_wrapper->title = 'My Test Node';
  7. $node_wrapper->field_autosaved_chapter->set($_POST['nid']);
  8. $node_wrapper->field_autosaved_body->value = $_POST['content'];
  9. $node_wrapper->save();
Programming Language: PHP

General methods

Source code viewer
  1. // Get title or name ($node->title, $user->name, etc.).
  2. $wrapper->label();
  3.  
  4. // Get id ($node->nid, $user->uid, etc.).
  5. $wrapper->getIdentifier();
  6.  
  7. // Get type or bundle ($node->type, $entity->bundle).
  8. $wrapper->getBundle();
  9.  
  10. // Get fields and other properties of the entity.
  11. $wrapper->getPropertyInfo();
  12.  
  13. // IN PROGRESS...
Programming Language: PHP

Properties

Source code viewer
  1. // Is "Promoted to front page".
  2. $is_promoted = $wrapper->promote->value();
  3.  
  4. // Is "Sticky at top of lists".
  5. $is_sticky = $wrapper->sticky->value();
  6.  
  7. // 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
  1. // NB: If your object property does not exist, you will get a fatal error. To avoid this you can control the existence of it.
  2. $wrapper_properties = $entity_metadata_wrapper->getPropertyInfo();
  3. if (isset($wrapper_properties['body'])) {
  4. // ...
  5. }
Programming Language: PHP

Longtext field / wysiwyg (textarea with text-format-enabled)

Source code viewer
  1. // Array of: value, safe_value, format, and optionally summary + safe_summary.
  2. $wrapper->body->value();
  3. // Filtered value.
  4. $wrapper->body->value->value();
  5. // Unfiltered value.
  6. $wrapper->body->value->raw();
  7. // Text format.
  8. $wrapper->body->format->value();
  9. // Set value.
  10. $wrapper->body->value = 'new value';
  11. $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
  1. $wrapper->field_textarea->value();
  2. $wrapper->field_textarea->set('Long text....');
Programming Language: PHP

Text field

Source code viewer
  1. $wrapper->field_textfield->value();
  2. $wrapper->field_textfield = 'Short text....';
Programming Language: PHP

Taxonomy field

Source code viewer
  1. $wrapper->field_taxonomy->value();
  2. $tid = $wrapper->field_taxonomy->raw();
Programming Language: PHP

Field Collection field

Source code viewer
  1. if (isset($wrapper_properties['field_authors'])) {
  2. foreach ($wrapper->field_authors->value() as $field_collection_item) {
  3. // ...
  4. }
  5. }
Programming Language: PHP
IN PROGRESS...

Examples

Source code viewer
  1. // Load user from external login system and save mail that is the same as username.
  2. $account = user_external_load($email);
  3. $wrapper = entity_metadata_wrapper('user', $account);
  4. $wrapper->mail->set($wrapper->label());
  5. $wrapper->save();
Programming Language: PHP