Implementation of hook_nodeapi().

Manages translation information for nodes.

File

modules/translation/translation.module, line 183
Manages content translations.

Code

function translation_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {

  // Only act if we are dealing with a content type supporting translations.
  if (!translation_supported_type($node->type)) {
    return;
  }
  switch ($op) {
    case 'prepare':
      if (empty($node->nid) && user_access('translate content') && isset($_GET['translation']) && isset($_GET['language']) && is_numeric($_GET['translation'])) {
        $translation_source = node_load($_GET['translation']);
        if (empty($translation_source) || !node_access('view', $translation_source)) {

          // Source node not found or no access to view. We should not check
          // for edit access, since the translator might not have permissions
          // to edit the source node but should still be able to translate.
          return;
        }
        $language_list = language_list();
        if (!isset($language_list[$_GET['language']]) || $translation_source->language == $_GET['language']) {

          // If not supported language, or same language as source node, break.
          return;
        }

        // Populate fields based on source node.
        $node->language = $_GET['language'];
        $node->translation_source = $translation_source;
        $node->title = $translation_source->title;

        // If user has no access to the filter used for the body, Drupal core
        // does not let the edit form to appear, so we should avoid exposing
        // the source text here too.
        $node->body = filter_access($translation_source->format) ? $translation_source->body : '';

        // Let every module add custom translated fields.
        node_invoke_nodeapi($node, 'prepare translation');
      }
      break;
    case 'insert':
      if (!empty($node->translation_source)) {
        if ($node->translation_source->tnid) {

          // Add node to existing translation set.
          $tnid = $node->translation_source->tnid;
        }
        else {

          // Create new translation set, using nid from the source node.
          $tnid = $node->translation_source->nid;
          db_query("UPDATE {node} SET tnid = %d, translate = %d WHERE nid = %d", $tnid, 0, $node->translation_source->nid);
        }
        db_query("UPDATE {node} SET tnid = %d, translate = %d WHERE nid = %d", $tnid, 0, $node->nid);
        $node->tnid = $tnid;
      }
      break;
    case 'update':
      if (isset($node->translation) && $node->translation && !empty($node->language) && $node->tnid) {

        // Update translation information.
        db_query("UPDATE {node} SET tnid = %d, translate = %d WHERE nid = %d", $node->tnid, $node->translation['status'], $node->nid);
        if (!empty($node->translation['retranslate'])) {

          // This is the source node, asking to mark all translations outdated.
          db_query("UPDATE {node} SET translate = 1 WHERE tnid = %d AND nid != %d", $node->tnid, $node->nid);
        }
      }
      break;
    case 'delete':
      translation_remove_from_set($node);
      break;
  }
}