With Drupal development, if you are ever working with field values or something of the like, you’ll probably see the value in a nested array like this:
That ‘und’ key is actually the language the field value is in! If you have a multilingual site, or are just setting language options regardless, you’ll find this key will change to represent the language. When I first started developing in Drupal, my way of accessing these values without explicitly stating ‘und’ (or whatever the language was), was to get the value of the language variable set for that node. This worked for a while but I eventually came across a node whose language variable was different from the language key of the field I was trying to access.
After some asking around I learned that I had actually been doing things wrong the whole time. I shouldn’t have been looking for the language variable; I should have been using entity metadata wrappers.
Entity metadata wrappers are provided by the Entity API and allow you to not have to worry about what language the content is currently in when you are trying to values. You can turn this:
Not only does this solve the issue of trying to find out the right language key, it allows you to other things a lot easier like getting the fields and values of a field collection.
$fields = entity_metadata_wrapper('field_collection_item', $fc_item);
This can make things a lot simpler and MAKE your code a lot more readable.
One problem that I found when I first started working with entity metadata wrappers however, was that you can’t use dpm() on the node wrapper. To view the functions you can call off of an entity wrapper you have to use
and then use dpm() on each functions return value to see what it is. This is a minor price to pay for the functionality of the entity wrapper though in my opinion.
All in all, if you are looking to make your code more extensible and readable, making use of entity metadata wrappers is definitely a must.