I seem to be developing add-ons to a lot of modules that use Entity API at the moment, and the
entity_metadata_wrapper() function keeps on popping up.
The docs page says this about it:
Returns a property wrapper for the given data.
If an entity is wrapped, the wrapper can be used to retrieve further wrappers for the entitity properties.
Ignoring the wonderfully Freudian spelling of the word 'entity' in there, I don't really understand what the purpose of these wrappers is.
I understand that the function essentially returns a
The wrapper eases applying getter and setter callbacks of entity properties
But what I can't understand is how it makes things easier.
For example, to update the status property of a node I could use this code:
$node = node_load($nid); $node->status = 1; node_save($node);
That's pretty clean. As I understand it (but may be wrong) the equivalent code using
entity_metadata_wrapper() would be more verbose than that.
I'm not sure if it's simply the use of the term 'wrapper' that's tripping me up here, but I've looked through the code in the Entity module too and I'm not really any closer to understanding it.
Is anyone able to explain what the benefits of using this function are, and perhaps provide a simple code example for a common use-case?