You are here

function entity_metadata_field_get_language in Entity API 7

Helper for determining the field language to be used.

Note that we cannot use field_language() as we are not about to display values, but generally read/write values.

Parameters

$fallback: (optional) Whether to fall back to the entity default language, if no value is available for the given language code yet.

Return value

string The language code to use.

4 calls to entity_metadata_field_get_language()
entity_metadata_field_property_get in modules/callbacks.inc
Callback for getting field property values.
entity_metadata_field_property_set in modules/callbacks.inc
Callback for setting field property values.
entity_metadata_field_verbatim_get in modules/callbacks.inc
Callback to verbatim get the data structure of a field. Useful for fields that add metadata for their own data structure.
entity_metadata_field_verbatim_set in modules/callbacks.inc
Writes the passed field items in the object. Useful as field level setter to set the whole data structure at once.

File

modules/callbacks.inc, line 565
Provides various callbacks for the whole core module integration.

Code

function entity_metadata_field_get_language($entity_type, $entity, $field, $langcode = LANGUAGE_NONE, $fallback = FALSE) {

  // Try to figure out the default language used by the entity.
  // With Drupal >= 7.15 we can use entity_language().
  if (function_exists('entity_language')) {
    $default_langcode = entity_language($entity_type, $entity);
  }
  else {
    $default_langcode = !empty($entity->language) ? $entity->language : LANGUAGE_NONE;
  }

  // Determine the right language to use.
  if ($default_langcode != LANGUAGE_NONE && field_is_translatable($entity_type, $field)) {
    $langcode = $langcode != LANGUAGE_NONE ? field_valid_language($langcode, $default_langcode) : $default_langcode;
    if (!isset($entity->{$field['field_name']}[$langcode]) && $fallback) {
      $langcode = $default_langcode;
    }
    return $langcode;
  }
  else {
    return LANGUAGE_NONE;
  }
}