You are here

function lingotek_normalize_special_field_language in Lingotek Translation 7.7

Same name and namespace in other branches
  1. 7.5 lingotek.module \lingotek_normalize_special_field_language()
  2. 7.6 lingotek.module \lingotek_normalize_special_field_language()
2 calls to lingotek_normalize_special_field_language()
lingotek_entity_load in ./lingotek.module
Implements hook_entity_load().
lingotek_entity_presave in ./lingotek.module
Implements hook_entity_presave().

File

./lingotek.module, line 2261

Code

function lingotek_normalize_special_field_language($entity_type, $entity, $read_only = TRUE) {
  $host_language = lingotek_get_host_language($entity_type, $entity, $read_only);
  $excluded_fields =& drupal_static(__FUNCTION__);
  if (!isset($excluded_fields)) {
    if ($cache = cache_get('lingotek__excluded_fc_fields')) {
      $excluded_fields = $cache->data;
    }
    else {
      $excluded_fields = field_read_fields(array(
        'type' => 'field_collection',
      ));
      cache_set('lingotek__excluded_fc_fields', $excluded_fields, 'cache', CACHE_TEMPORARY);
    }
  }
  foreach ($entity as $key => $value) {

    // for each field in the entity
    // if the given field is a member of the field types then normalize the languages
    if (isset($excluded_fields[$key])) {
      continue;
    }
    if (is_array($value)) {

      // On entity load, LANGUAGE_NONE should be the authority,
      // meaning the most current changes should be stored there.
      if ($read_only) {
        if (array_key_exists(LANGUAGE_NONE, $value)) {
          $entity->{$key}[$host_language] = $value[LANGUAGE_NONE];
        }
      }
      else {
        if (count($value) == 1) {

          // initial creation, so only one language exists
          if (array_key_exists($host_language, $value)) {
            $entity->{$key}[LANGUAGE_NONE] = $value[$host_language];
          }
          elseif (array_key_exists(LANGUAGE_NONE, $value)) {
            $entity->{$key}[$host_language] = $value[LANGUAGE_NONE];
          }
        }
        else {

          // Update by comparing changes with the original entity for the host
          // language and language neutral.  Save the one that changed.
          if (isset($entity->original) && isset($entity->original->{$key})) {
            $original_value = $entity->original->{$key};
            if (array_key_exists($host_language, $value) && (!array_key_exists($host_language, $original_value) || $value[$host_language] != $original_value[$host_language])) {
              $entity->{$key}[LANGUAGE_NONE] = $value[$host_language];
            }
            elseif (array_key_exists(LANGUAGE_NONE, $value) && (!array_key_exists(LANGUAGE_NONE, $original_value) || $value[LANGUAGE_NONE] != $original_value[LANGUAGE_NONE])) {
              $entity->{$key}[$host_language] = $value[LANGUAGE_NONE];
            }
          }
          else {

            // Use the most common way if no original entity exists.
            if (array_key_exists($host_language, $value) && !isset($value[$host_language][0]['safe_value'])) {
              $entity->{$key}[LANGUAGE_NONE] = $value[$host_language];
            }
            elseif (array_key_exists(LANGUAGE_NONE, $value) && !isset($value[LANGUAGE_NONE][0]['safe_value'])) {
              $entity->{$key}[$host_language] = $value[LANGUAGE_NONE];
            }
          }
        }
      }
    }
  }
}