Contributions API

i18nsync_nodeapi

Definition

i18nsync_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL)
contributions/i18n/i18nsync/i18nsync.module, line 101

Description

Implementation of hook_nodeapi().

Code

<?php
function i18nsync_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  global $i18nsync; // This variable will be true when a sync operation is in progress

  // Only for nodes that have language and belong to a translation set.
  if (translation_supported_type($node->type) && !empty($node->language)  && !$i18nsync) {
    switch ($op) {
      case 'prepare translation':
        // We copy over all the fields to be synchronized
        if ($fields = i18nsync_node_fields($node->type)) {
          i18nsync_prepare_translation($node, $node->translation_source, $fields);
        }
        break;
      case 'insert':
        // When creating a translation we need to save the files that have been inherited
        if (!empty($node->translation_source) && !empty($node->files)) {
          foreach ($node->files as $fid => $file) {
            $file = (object)$file;
            if (empty($file->remove) && !isset($_SESSION['upload_files'][$fid])) {
              db_query("INSERT INTO {upload} (fid, nid, vid, list, description, weight) VALUES (%d, %d, %d, %d, '%s', %d)", $file->fid, $node->nid, $node->vid, $file->list, $file->description, $file->weight);
            }
          }
        }
        // Intentional no break
      case 'update':
        // Let's go with field synchronization.
        if (!empty($node->tnid) && ($fields = i18nsync_node_fields($node->type)) && ($translations = translation_node_get_translations($node->tnid)) && count($translations) > 1) {
  
          // We want to work with a fresh copy of this node, so we load it again bypassing cache.
          // This is to make sure all the other modules have done their stuff and the fields are right.
          // But we have to copy the revision field over to the new copy.
          $revision = isset($node->revision) && $node->revision;
          $source = node_load(array('nid' => $node->nid));
          $source->revision = $revision;
          $i18nsync = TRUE;

          foreach ($translations as $trnode) {
            if ($source->nid != $trnode->nid) {
              i18nsync_node_translation($source, $trnode, $fields, $op);
            }
          }
          $i18nsync = FALSE;
          drupal_set_message(t('All %count node translations have been synchronized', array('%count' => count($translations) - 1)));
        }
        break;
    }
  }
}
?>