You are here

function lingotek_get_translated_node in Lingotek Translation 7.5

Same name and namespace in other branches
  1. 7.4 lingotek.api.inc \lingotek_get_translated_node()
1 call to lingotek_get_translated_node()
lingotek_entity_download in ./lingotek.module

File

./lingotek.remote.inc, line 18

Code

function lingotek_get_translated_node($node, $drupal_language_code) {
  $tset = translation_node_get_translations($node->nid);
  if ($node->tnid == 0) {
    $node->tnid = $node->nid;
    db_update('{node}')
      ->fields(array(
      'tnid' => $node->nid,
    ))
      ->condition('nid', $node->nid)
      ->execute();
  }
  if (isset($tset[$drupal_language_code])) {
    $localized_node = node_load($tset[$drupal_language_code]->nid);
  }
  else {
    $localized_node = new stdClass();
    $localized_node->type = $node->type;
    node_object_prepare($localized_node);

    // Grandfather the lingotek settings (ie. profile, etc.) on the new node.
    if (!empty($node->lingotek)) {
      $localized_node->lingotek = $node->lingotek;
    }
    $localized_node->title = $node->title . ' (' . $drupal_language_code . ')';
    $localized_node->tnid = $node->tnid;
    $localized_node->language = $drupal_language_code;
    $localized_node->uid = $node->uid;
    $localized_node->name = $node->name;
    $localized_node->comment = $node->comment;
    $localized_node->promote = $node->promote;
    $localized_node->sticky = $node->sticky;
    $localized_node->status = $node->status;
    $localized_node->create_lingotek_document = FALSE;

    // Grandfather field settings/values from source node to target.
    $source_fields = field_info_instances('node', $node->type);
    foreach (array_keys($source_fields) as $key) {
      $copied_field = $node->{$key};
      if (!empty($copied_field[$node->language])) {
        $copied_field[$localized_node->language] = $copied_field[$node->language];
        unset($copied_field[$node->language]);
      }
      $localized_node->{$key} = $copied_field;
    }
    $lingotek_fields = variable_get('lingotek_enabled_fields');
    foreach ($lingotek_fields['node'][$localized_node->type] as $field_name) {
      $field = $node->{$field_name};
      $f = array();
      if (isset($field[$node->language])) {
        foreach ($field[$node->language] as $key => $value) {
          if (isset($value['format'])) {
            $f[$drupal_language_code][$key]['format'] = $value['format'];
          }
        }
      }
      $localized_node->{$field_name} = $f;
    }
    node_save($localized_node);
    lingotek_keystore('node', $localized_node->nid, 'node_sync_status', 'TARGET');

    // Child node should keep its parent node's profile, for rules integration.
    $parent_profile = lingotek_keystore('node', $node->nid, 'profile');
    if ($parent_profile !== FALSE) {
      lingotek_keystore('node', $localized_node->nid, 'profile', $parent_profile);
    }
  }
  return $localized_node;
}