You are here

function globallink_save_translated_node_with_fields in GlobalLink Connect for Drupal 7.6

Same name and namespace in other branches
  1. 7.7 globallink_node.inc \globallink_save_translated_node_with_fields()
  2. 7.5 globallink_node.inc \globallink_save_translated_node_with_fields()

Saves translated node with fields.

Parameters

object $node: The original node.

object $tnode: The translated node.

array $t_arr: The translated array.

Return value

True if save is successful.

1 call to globallink_save_translated_node_with_fields()
globallink_update_node in ./globallink_node.inc
Updates node.

File

./globallink_node.inc, line 1350

Code

function globallink_save_translated_node_with_fields($node, &$tnode, $t_arr) {
  $field_arr = field_info_instances('node', $node->type);
  $keys = array_keys($field_arr);
  $non_translate_field_arr = globallink_get_non_translatable_config_fields($node->type);
  foreach ($keys as $field) {
    $items = field_get_items('node', $node, $field);
    if (!$items || array_key_exists($field, $non_translate_field_arr)) {
      continue;
    }
    $field_def = field_read_field($field);
    switch ($field_def['type']) {
      case 'list_boolean':
      case 'file':
      case 'taxonomy_term_reference':
      case 'field_collection':
        continue 2;
      default:
        if (!isset($t_arr[$field])) {
          continue 2;
        }
        $format = '';
        $t_field_lang = LANGUAGE_NONE;
        $t_target_lang = LANGUAGE_NONE;
        if (key($t_arr[$field]) !== LANGUAGE_NONE) {
          $t_field_lang = key($t_arr[$field]);
          $t_target_lang = $tnode->language;
        }
        $t_field_arr = $t_arr[$field][$t_field_lang];
        $body_summary = '';
        if ($field_def['type'] == 'text_with_summary' && isset($t_arr[$field . '@summary'])) {
          $body_summary = $t_arr[$field . '@summary'][$t_field_lang];
        }
        $arr = $tnode->{$field};
        if (empty($arr[$t_target_lang]) && $t_target_lang != LANGUAGE_NONE) {
          $arr[$t_target_lang] = $arr[$t_field_lang];
        }
        $updated = FALSE;
        if ($field_def['type'] == 'image') {
          $source_lang = field_language('node', $node, $field);
          if (!isset($arr[$t_target_lang])) {
            $arr[$t_target_lang] = $arr[$source_lang];
          }
        }
        foreach ($arr[$t_target_lang] as $delta => $n_field) {
          if (isset($t_field_arr[$delta])) {
            $f_arr = array();
            $gl_obj = $t_field_arr[$delta];
            if ($field_def['type'] == 'image') {
              if (is_object($gl_obj)) {
                if (isset($gl_obj->title)) {
                  $n_field['title'] = $gl_obj->title;
                  $arr[$t_target_lang][$delta] = $n_field;
                }
                if (isset($gl_obj->alt)) {
                  $n_field['alt'] = $gl_obj->alt;
                  $arr[$t_target_lang][$delta] = $n_field;
                }
              }
            }
            else {
              if (is_object($gl_obj)) {
                $translated_content = $gl_obj->translatedContent;
              }
              else {
                $translated_content = $gl_obj;
              }
              if (isset($n_field['format'])) {
                $format = $n_field['format'];
              }
              if ($format != '') {
                $f_arr['format'] = $format;
              }
              if ($field_def['type'] == 'link_field') {
                $n_field['title'] = $translated_content;
                $arr[$t_target_lang][$delta] = $n_field;
              }
              else {
                $f_arr['value'] = $translated_content;
                if ($field_def['type'] == 'text_with_summary' && !empty($body_summary)) {
                  $f_arr['summary'] = $body_summary[$delta]->translatedContent;
                }
                $arr[$t_target_lang][$delta] = $f_arr;
              }
            }
            $updated = TRUE;
          }
        }
        if ($updated) {
          $tnode->{$field} = $arr;
        }
    }
  }
  $is_hook_enabled = variable_get('globallink_implementation_type', 0);
  if ($is_hook_enabled == 1) {
    globallink_update_node_hook($node, $tnode);
  }
  $success = globallink_node_save($tnode);
  if ($success) {
    if (module_exists('field_collection') && isset($t_arr['field_collection'])) {
      globallink_save_field_collections($node, $tnode, $t_arr, $t_target_lang);
    }
  }
  return $success;
}