You are here

function globallink_entity_save_translated_entity_with_fields in GlobalLink Connect for Drupal 7.5

Same name and namespace in other branches
  1. 7.7 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 in globallink_entity/globallink_entity.inc
Updates entity.

File

globallink_entity/globallink_entity.inc, line 1097

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) {
    if ($field_info->field_type == 'image' && $field_info->translatable == 1) {
      $source_lang = $node->language;
      if (isset($node->{$field_name}[$source_lang])) {
        $node->{$field_name}[$target_lang] = $node->{$field_name}[$source_lang];
      }
    }
  }
  foreach ($translation_arr as $field => $values) {
    if (!is_array($values)) {
      continue;
    }
    foreach ($values as $target => $arr_value) {
      foreach ($arr_value as $obj) {
        if (!isset($obj->translatedContent)) {
          continue;
        }
        $format = '';
        if (!empty($obj->format)) {
          $format = $obj->format;
        }
        if (isset($node->{$field}[$target])) {
          $node->{$field}[$target][$obj->delta] = array(
            'value' => $obj->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);
  }

  // Set new translation data
  $node->translations->data[$target_lang] = array(
    '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,
  );
  $node->revision = variable_get('globallink_entity_create_revisions', 0);

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