function field_language_fallback

Applies language fallback rules to the fields attached to the given entity.

Core language fallback rules simply check if fields have a field translation for the requested language code. If so, the requested language is returned, otherwise all the fallback candidates are inspected to see if there is a field translation available in another language. By default this is called by field_field_language_alter(), but this behavior can be disabled by setting the 'field.settings.language_fallback' variable to FALSE.

Parameters

$field_langcodes: A reference to an array of language codes keyed by field name.

\Drupal\Core\Entity\EntityInterface $entity: The entity to be displayed.

$langcode: The language code $entity has to be displayed in.

Related topics

4 calls to field_language_fallback()
field_language in drupal/core/modules/field/field.multilingual.inc
Returns the display language code for the fields attached to the given entity.
field_test_field_language_alter in drupal/core/modules/field/tests/modules/field_test/field_test.module
Implements hook_field_language_alter().
hook_field_language_alter in drupal/core/modules/field/field.api.php
Perform alterations on field_language() values.
translation_entity_field_language_alter in drupal/core/modules/translation_entity/translation_entity.module
Implements hook_field_language_alter().
1 string reference to 'field_language_fallback'
field_update_8004 in drupal/core/modules/field/field.install
Moves field_storage_default and field_language_fallback to config.

File

drupal/core/modules/field/field.module, line 350
Attach custom data fields to Drupal entities.

Code

function field_language_fallback(&$field_langcodes, EntityInterface $entity, $langcode) {

  // Lazily init fallback candidates to avoid unnecessary calls.
  $fallback_candidates = NULL;
  foreach ($field_langcodes as $field_name => $field_langcode) {

    // If the requested language is defined for the current field use it,
    // otherwise search for a fallback value among the fallback candidates.
    if (isset($entity->{$field_name}[$langcode])) {
      $field_langcodes[$field_name] = $langcode;
    }
    elseif (!empty($entity->{$field_name})) {
      if (!isset($fallback_candidates)) {
        require_once DRUPAL_ROOT . '/core/includes/language.inc';
        $fallback_candidates = language_fallback_get_candidates();
      }
      foreach ($fallback_candidates as $fallback_langcode) {
        if (isset($entity->{$field_name}[$fallback_langcode])) {
          $field_langcodes[$field_name] = $fallback_langcode;
          break;
        }
      }
    }
  }
}