The function entity_metadata_field_get_language returns the entity language instead of the field language. There is probably a good reason for that, but I can't figure out why. So here is a patch that first looks for the field language then falls back on the entity language.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

StoraH’s picture

Title: Get the field language instead of entity language from entity_metadata_field_get_language » Get field language instead of entity language from entity_metadata_field_get_language
emattias’s picture

Status: Active » Needs review
joelpittet’s picture

Version: 7.x-1.5 » 7.x-1.x-dev
Status: Needs review » Reviewed & tested by the community

Thanks you for the patch @StoraH, this fixed my issue that looked like #2342991: Get the translated product reference field to display the products with the correct language. for commerce product reference field translation.

logaritmisk’s picture

If I understand correctly, this is because if we want to read/write to the fields, we want to use the language the entity uses, for example.

$wrapper = entity_metadata_wrapper('node', $node);
$wrapper->field_body = "My body should be saved using the node language and not the fields language. We might have mixed languages for different fields.";

But if you want to display the values for the user, you will have to set the language to use.

$wrapper = entity_metadata_wrapper('node', $node);
$wrapper->language($language_content->language);

print $wrapper->field_body->value();

It's annoying. I love entity_metadata_wrapper and I use it, like, everywhere and Slate heavily depends on it, but I can understand why it works like it do. Do get a round this, you would have to implement another method to use to display the value for a field.

Maybe something like $wrapper->field_body->display();

fago’s picture

Status: Reviewed & tested by the community » Closed (won't fix)
+++ b/modules/callbacks.inc
@@ -539,9 +539,6 @@ function entity_metadata_field_verbatim_set($entity, $name, $items, $langcode, $
- * Note that we cannot use field_language() as we are not about to display
- * values, but generally read/write values.

That's the reason - field_language() has language fallback for display, but we are in a general read/write operation.