You are here

function i18n_sync_node_translation in Internationalization 7

Synchronizes fields for node translation.

There's some specific handling for known fields like:

  • files, for file attachments.
  • iid (CCK node attachments, translations for them will be handled too).

All the rest of the fields will be just copied over. The 'revision' field will have the special effect of creating a revision too for the translation.

Parameters

$node: Source node being edited.

$translations: Node translations to synchronize, just needs nid property.

$fields: List of fields to synchronize.

$op: Node operation (insert|update).

1 call to i18n_sync_node_translation()
i18n_sync_node_update in i18n_sync/i18n_sync.module
Implements hook_node_update().

File

i18n_sync/i18n_sync.node.inc, line 29
Internationalization (i18n) package. Synchronization of translations

Code

function i18n_sync_node_translation($node, $translations, $field_names, $op) {
  $total = count($translations);
  $count = 0;

  // Disable language selection and synchronization temporarily, enable it again later
  $i18n_select = i18n_select(FALSE);
  i18n_sync(FALSE);
  foreach ($translations as $translation) {

    // If translation is the same node, we cannot synchronize with itself
    if ($node->nid == $translation->nid) {
      $total--;
      continue;
    }

    // Load full node, we need all data here.
    entity_get_controller('node')
      ->resetCache(array(
      $translation->nid,
    ));
    $translation = node_load($translation->nid);
    $i18n_options = i18n_sync_node_options($node->type);

    // Invoke callback for each field, the default is just copy over
    foreach ($field_names as $field) {
      if (!empty($i18n_options[$field]['field_name'])) {
        i18n_sync_field_translation_sync('node', $node->type, $translation, $translation->language, $node, $node->language, $i18n_options[$field]['field_name']);
      }
      elseif (isset($node->{$field})) {

        // Standard node field, just copy over.
        $translation->{$field} = $node->{$field};
      }
    }

    // Give a chance to other modules for additional sync
    module_invoke_all('i18n_sync_translation', 'node', $translation, $translation->language, $node, $node->language, $field_names);
    node_save($translation);
    $count++;

    // Flush each entity from the load cache after processing, to
    // avoid exceeding PHP memory limits. It should be safe to keep
    // at least one, however; so we retain the final translation in
    // the cache after saving it.
    if ($count < $total) {
      entity_get_controller('node')
        ->resetCache(array(
        $translation->nid,
      ));
    }
  }
  i18n_sync(TRUE);
  i18n_select($i18n_select);
  drupal_set_message(format_plural($count, 'One node translation has been synchronized.', 'All @count node translations have been synchronized.'));
}