You are here

function i18nsync_nodeapi in Internationalization 5.2

Same name and namespace in other branches
  1. 5.3 experimental/i18nsync.module \i18nsync_nodeapi()
  2. 5 experimental/i18nsync.module \i18nsync_nodeapi()
  3. 6 i18nsync/i18nsync.module \i18nsync_nodeapi()

Implementation of hook_nodeapi().

Note that we avoid getting node parameter by reference

File

experimental/i18nsync.module, line 92
Internationalization (i18n) package. Synchronization of translations

Code

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 (variable_get("i18n_node_{$node->type}", 0) && $node->language && $node->trid && !$i18nsync) {
    switch ($op) {
      case 'insert':
      case 'update':

        // Taxonomy synchronization
        if ($sync = variable_get('i18n_vocabulary_nodesync', array())) {

          // Get vocabularies synchronized for this node type
          $result = db_query("SELECT v.* FROM {vocabulary} v INNER JOIN {vocabulary_node_types} n ON v.vid = n.vid WHERE n.type = '%s' AND v.vid IN (%s)", $node->type, implode(',', array_keys($sync)));
          $count = 0;
          while ($vocabulary = db_fetch_object($result)) {
            i18nsync_node_taxonomy($node, $vocabulary);
            $count++;
          }
          if ($count) {
            drupal_set_message(t('Node taxonomy has been synchronized.'));
          }
        }

        // No need to refresh cache. It will be refreshed after insert/update anyway
        // Let's go with field synchronization
        if (($fields = i18nsync_node_fields($node->type)) && ($translations = translation_node_get_translations(array(
          'nid' => $node->nid,
        ), FALSE))) {

          // 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;
          $node = node_load(array(
            'nid' => $node->nid,
          ));
          $node->revision = $revision;
          $i18nsync = TRUE;
          foreach ($translations as $trnode) {
            i18nsync_node_translation($node, $trnode, $fields);
          }
          $i18nsync = FALSE;
          drupal_set_message(t('All %count node translations have been synchronized', array(
            '%count' => count($translations),
          )));
        }
        break;
    }
  }
}