Right Method to Get the Language Code for a Given Drupal 7 Entity

By junaid on Thu, 01/29/2015 - 14:00

While programming for Drupal, it is a very common scenario to get the language code for a given entity. A bad practice being followed by many programmers is hard-coding the value 'und' directly as language code. Some programmers will take 'language' property of the entity object, this only works for entity of type node and entities that have property named 'language' as entity key for language.

If you want to follow the right method or if you are developing a generic module that deals with variety of entity types then you need to use the function entity_language(). It just requires to pass the type of entity and entity object itself as parameters.

KB Article