Are you familiar with entity_metadata_wrappers? If you’re not, oh boy, you should be!

Entity Metadata Wrapper is the right way - and, after you get the grip of it, the easiest way - for you to manipulate anything with a field when coding your module. Sure, since the old days of CCK we are used with dealing with our fields in our nodes. Except they are a little messy.

Cleaner code!

Instead of doing this:

<?php
$first_name
= '';
if (!empty(
$node->field_first_name)) {
 
$name = $node->field_first_name[LANGUAGE_NONE][0]['value'];
}
?>

Let’s condense that, shall we?

<?php
$node_wrapper
= entity_metadata_wrapper('node', $node);
$first_name = $node_wrapper->field_first_name->value();
?>

Sure, the name “metadata wrapper” may be a little intimidating, but it does shortens your code and makes it clearer. Oh, and if you have an entity reference field, or a file field, you can just do this:

<?php
$image
= $node_wrapper->field_image->value();
?>

And the $referenced_node is already a loaded file object, not a useless “fid”.

Wrappers for dealing with entity reference: cleaner-er code!

Suppose you have two node types: Employee and Department. There is an Entity reference field from "Employee" to "Department" and on the "Department" node you have a field called "field_dept_phone" that stores the phone number. (for simplicity, I'm assuming that field_employee_dept is required).

If you have the $employee node, how to fetch the phone number?

Hard way:

<?php
$phone
= '';
$department = node_load($employee->field_employee_dept[LANGUAGE_NONE][0]['target_id']);
if (
$department && !empty($department->field_dept_phone[LANGUAGE_NONE][0]['value'])) {
 
$phone = $department->field_dept_phone[LANGUAGE_NONE][0]['value'];
}
?>

And the wrapper way:

<?php
$wrapper
= entity_metadata_wrapper('node', $employee);
$phone = $wrapper->field_employee_dept->field_dept_phone->value();
?>

 

Now what?

Well, this post is not intended to be a full entity metadata wrapper course, so, if I have convinced you, take 15 minutes of your day and do this:

  1. Download Entity API from http://drupal.org/project/entity
  2. Read this, now: https://drupal.org/node/1021556
  3. Your life quality will improve, proportionally to your code quality!

Photo credits: https://www.flickr.com/photos/81564552@N00/3208209972/

I have seen these wrapper things floating around but never bothered to take a closer look. This post has convinced me to do so. :)

Great intro to why and how to use this. Great examples, showing very clearly nice simple examples, but also demonstrating clearly why we should bother.

Always be sure to check the existence of the metadata wrapper property before attempting to read/write from them. If not you will end up in exception WSOD hell. Use if (!empty($node->property)) or if ($wrapper->__isset('property')) first.

Andy hit it. What happens to your website if field_employee_dept doesn't exist on the node?

This happens, for example, when new content types are added, or when a field is removed because it's no longer needed.

Rather than adding issets on every single field, I wrap my wrappers in a try catch:

https://gist.github.com/Greg-Boggs/7128108

Andy and Greg, I created a wrapper system in part to avoid the try/catch isset problem you mention. It allows you to generate per-bundle wrapper classes for your project with Drush. It's not a generic tool like entity_metadata_wrapper. It wraps that and gives you a project-specific interface, including the fields you have configured on your entities. The wrapper classes can be customized, so you can add validation or value format detection, as well. It's on d.o here: https://www.drupal.org/project/wrappers_delight

clear and convincing article. good job.

Hi there, thanks for your introduction, I'd translated this blog into Chinese, and had posted to 'https://drupal.fleeto.us/translation/entity-metathing-what-very-brief-in..., Please let me know if you don't like this,

Read more

Two tips for advanced Entity Metadata Wrapper usage