You are here

function globallink_entity_save_translated_entity_with_fields in GlobalLink Connect for Drupal 7.7

Same name and namespace in other branches
  1. 7.5 globallink_entity/globallink_entity.inc \globallink_entity_save_translated_entity_with_fields()
  2. 7.6 globallink_entity/globallink_entity.inc \globallink_entity_save_translated_entity_with_fields()

Saves translated entity with fields.

Parameters

object $node: The entity node.

array $translation_arr: The translation array.

string $target_lang: The target language of the entity.

Return value

bool TRUE if save was successful. FALSE on failure.

1 call to globallink_entity_save_translated_entity_with_fields()
globallink_entity_update_entity in globallink_entity/globallink_entity.inc
Updates entity.

File

globallink_entity/globallink_entity.inc, line 533

Code

function globallink_entity_save_translated_entity_with_fields(&$node, $translation_arr, $target_lang) {
  $config_fields = globallink_get_config_fields($node->type);
  foreach ($config_fields as $field_name => $field_info) {
    $source_lang = $node->language;
    if ($field_info->field_type == 'image' && $field_info->translatable == 1) {
      if (isset($node->{$field_name}[$source_lang])) {
        $node->{$field_name}[$target_lang] = $node->{$field_name}[$source_lang];
      }
    }
    else {
      if ($field_info->field_type == 'entityreference' && $field_info->translatable == 1) {
        if (isset($node->{$field_name}[$source_lang])) {
          $node->{$field_name}[$target_lang] = $node->{$field_name}[$source_lang];
        }
      }
    }
  }

  //Non translatable fields will be updated to source values.
  $nonconfigs = field_info_instances('node', $node->type);
  foreach ($nonconfigs as $key => $value) {
    if (!array_key_exists($value['field_name'], $config_fields)) {
      $source_lang = $node->language;
      if (isset($node->{$value['field_name']}[$source_lang])) {
        $node->{$value['field_name']}[$target_lang] = $node->{$value['field_name']}[$source_lang];
      }
    }
  }
  foreach ($translation_arr as $field => $values) {
    if (!is_array($values)) {
      continue;
    }
    $body_summary = '';
    $field_def = field_info_field($field);
    if ($field_def['type'] == 'text_with_summary' && isset($translation_arr[$field . '@summary'])) {
      $body_summary = $translation_arr[$field . '@summary'][$target_lang];
    }
    foreach ($values as $target => $arr_value) {
      foreach ($arr_value as $obj) {
        if ($field_def['type'] == 'image') {
          if (is_object($obj)) {
            $delta = key($node->{$field}[$target]);
            if (isset($obj->title)) {
              $node->{$field}[$target][$delta]['title'] = $obj->title;
            }
            if (isset($obj->alt)) {
              $node->{$field}[$target][$delta]['alt'] = $obj->alt;
            }
          }
        }
        if (!isset($obj->translatedContent)) {
          continue;
        }
        $format = '';
        if (!empty($obj->format)) {
          $format = $obj->format;
        }
        if (isset($node->{$field}[$target])) {
          if (strpos($field, '@summary') == false) {
            if ($field_def['type'] == 'text_with_summary' && !empty($body_summary)) {
              $node->{$field}[$target][$obj->delta] = array(
                'value' => $obj->translatedContent,
                'summary' => $body_summary[$obj->delta]->translatedContent,
                'format' => $format,
              );
            }
            else {
              $node->{$field}[$target][$obj->delta] = array(
                'value' => $obj->translatedContent,
                'format' => $format,
              );
            }
          }
        }
        else {
          if (strpos($field, '@summary') == false) {
            if ($field_def['type'] == 'text_with_summary' && !empty($body_summary)) {
              $node->{$field}[$target] = array(
                $obj->delta => array(
                  'value' => $obj->translatedContent,
                  'summary' => $body_summary[$obj->delta]->translatedContent,
                  'format' => $format,
                ),
              );
            }
            else {
              $node->{$field}[$target] = array(
                $obj->delta => array(
                  'value' => $obj->translatedContent,
                  'format' => $format,
                ),
              );
            }
          }
        }
      }
    }
  }
  $is_hook_enabled = variable_get('globallink_implementation_type', 0);
  if ($is_hook_enabled == 1) {
    globallink_update_node_hook($node, $node);
  }
  $handler = entity_translation_get_handler('node', $node);

  // Set new translation data
  $translation = array(
    'entity_type' => GLOBALLINK_ENTITY_TYPE_NODE,
    'entity_id' => $node->nid,
    'language' => $target_lang,
    // Target language
    'source' => $node->language,
    // Source language
    'uid' => $node->uid,
    'status' => variable_get('globallink_publish_node', 0),
    // publish translation
    'translate' => 0,
    // Translation out of date
    'created' => $node->created,
    'changed' => $node->changed,
  );
  if (module_exists('field_collection') && isset($translation_arr['field_collection'])) {
    globallink_entity_save_field_collections($node, $translation_arr, $target_lang);
  }
  $node->revision = variable_get('globallink_entity_create_revisions', 0);
  $handler
    ->setTranslation($translation, $node);
  $handler
    ->saveTranslations();

  // Save new translation data
  $success = globallink_entity_save($node);
  return $success;
}