Entity Metathing what?
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:
<span style="color: #000000"><span style="color: #0000BB"><?php<br>$first_name </span><span style="color: #007700">= </span><span style="color: #DD0000">''</span><span style="color: #007700">;<br>if (!empty(</span><span style="color: #0000BB">$node</span><span style="color: #007700">-></span><span style="color: #0000BB">field_first_name</span><span style="color: #007700">)) {<br> </span><span style="color: #0000BB">$name </span><span style="color: #007700">= </span><span style="color: #0000BB">$node</span><span style="color: #007700">-></span><span style="color: #0000BB">field_first_name</span><span style="color: #007700">[</span><span style="color: #0000BB">LANGUAGE_NONE</span><span style="color: #007700">][</span><span style="color: #0000BB">0</span><span style="color: #007700">][</span><span style="color: #DD0000">'value'</span><span style="color: #007700">];<br>}<br></span><span style="color: #0000BB">?></span></span>
Let’s condense that, shall we?
<span style="color: #000000"><span style="color: #0000BB"><?php<br>$node_wrapper </span><span style="color: #007700">= </span><span style="color: #0000BB">entity_metadata_wrapper</span><span style="color: #007700">(</span><span style="color: #DD0000">'node'</span><span style="color: #007700">, </span><span style="color: #0000BB">$node</span><span style="color: #007700">);<br></span><span style="color: #0000BB">$first_name </span><span style="color: #007700">= </span><span style="color: #0000BB">$node_wrapper</span><span style="color: #007700">-></span><span style="color: #0000BB">field_first_name</span><span style="color: #007700">-></span><span style="color: #0000BB">value</span><span style="color: #007700">();<br></span><span style="color: #0000BB">?></span></span>
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:
<span style="color: #000000"><span style="color: #0000BB"><?php<br>$image </span><span style="color: #007700">= </span><span style="color: #0000BB">$node_wrapper</span><span style="color: #007700">-></span><span style="color: #0000BB">field_image</span><span style="color: #007700">-></span><span style="color: #0000BB">value</span><span style="color: #007700">();<br></span><span style="color: #0000BB">?></span></span>
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:
<span style="color: #000000"><span style="color: #0000BB"><?php<br>$phone </span><span style="color: #007700">= </span><span style="color: #DD0000">''</span><span style="color: #007700">;<br></span><span style="color: #0000BB">$department </span><span style="color: #007700">= </span><span style="color: #0000BB">node_load</span><span style="color: #007700">(</span><span style="color: #0000BB">$employee</span><span style="color: #007700">-></span><span style="color: #0000BB">field_employee_dept</span><span style="color: #007700">[</span><span style="color: #0000BB">LANGUAGE_NONE</span><span style="color: #007700">][</span><span style="color: #0000BB">0</span><span style="color: #007700">][</span><span style="color: #DD0000">'target_id'</span><span style="color: #007700">]);<br>if (</span><span style="color: #0000BB">$department </span><span style="color: #007700">&& !empty(</span><span style="color: #0000BB">$department</span><span style="color: #007700">-></span><span style="color: #0000BB">field_dept_phone</span><span style="color: #007700">[</span><span style="color: #0000BB">LANGUAGE_NONE</span><span style="color: #007700">][</span><span style="color: #0000BB">0</span><span style="color: #007700">][</span><span style="color: #DD0000">'value'</span><span style="color: #007700">])) {<br> </span><span style="color: #0000BB">$phone </span><span style="color: #007700">= </span><span style="color: #0000BB">$department</span><span style="color: #007700">-></span><span style="color: #0000BB">field_dept_phone</span><span style="color: #007700">[</span><span style="color: #0000BB">LANGUAGE_NONE</span><span style="color: #007700">][</span><span style="color: #0000BB">0</span><span style="color: #007700">][</span><span style="color: #DD0000">'value'</span><span style="color: #007700">];<br>}<br></span><span style="color: #0000BB">?></span></span>
And the wrapper way:
<span style="color: #000000"><span style="color: #0000BB"><?php<br>$wrapper </span><span style="color: #007700">= </span><span style="color: #0000BB">entity_metadata_wrapper</span><span style="color: #007700">(</span><span style="color: #DD0000">'node'</span><span style="color: #007700">, </span><span style="color: #0000BB">$employee</span><span style="color: #007700">);<br></span><span style="color: #0000BB">$phone </span><span style="color: #007700">= </span><span style="color: #0000BB">$wrapper</span><span style="color: #007700">-></span><span style="color: #0000BB">field_employee_dept</span><span style="color: #007700">-></span><span style="color: #0000BB">field_dept_phone</span><span style="color: #007700">-></span><span style="color: #0000BB">value</span><span style="color: #007700">();<br></span><span style="color: #0000BB">?></span></span>
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:
- Download Entity API from http://drupal.org/project/entity
- Read this, now: https://drupal.org/node/1021556
- Your life quality will improve, proportionally to your code quality!
Photo credits: https://www.flickr.com/photos/81564552@N00/3208209972/
Subtitle: A very brief introduction on Entity Metadata Wrappers