tnid) { // Already part of a set, grab that set. $tnid = $node->tnid; $translations = translation_node_get_translations($node->tnid); } else { // We have no translation source nid, this could be a new set, emulate that. $tnid = $node->nid; $translations = array($node->language => $node); } $header = array(t('Language'), t('Title'), t('Status'), t('Operations')); foreach (language_list() as $language) { $options = array(); $language_name = $language->name; // We may need to switch interface language for translations $params = variable_get('i18n_translation_switch', 0) ? array('language' => $language) : array(); if (isset($translations[$language->language])) { // Existing translation in the translation set: display status. // We load the full node to check whether the user can edit it. $translation_node = node_load($translations[$language->language]->nid); $title = l($translation_node->title, 'node/'. $translation_node->nid, $params); if (node_access('update', $translation_node)) { $options[] = l(t('edit'), "node/$translation_node->nid/edit", $params); } $status = $translation_node->status ? t('Published') : t('Not published'); $status .= $translation_node->translate ? ' - '. t('outdated') .'' : ''; if ($translation_node->nid == $tnid) { $language_name = t('@language_name (source)', array('@language_name' => $language_name)); } } else { // No such translation in the set yet: help user to create it. $title = t('n/a'); if (node_access('create', $node)) { $options[] = l(t('add translation'), 'node/add/'. str_replace('_', '-', $node->type), array('query' => "translation=$node->nid&language=$language->language") + $params); } $status = t('Not translated'); } $rows[] = array($language_name, $title, $status, implode(" | ", $options)); } drupal_set_title(t('Translations of %title', array('%title' => $node->title))); $output = theme('table', $header, $rows); if (user_access('administer translations')) { $output .= drupal_get_form('i18n_node_select_translation', $node, $translations); } return $output; } /** * Form to select existing nodes as translation * * This one uses autocomplete fields for all languages */ function i18n_node_select_translation($form_state, $node, $translations) { $form['node'] = array('#type' => 'value', '#value' => $node); $form['translations'] = array( '#type' => 'fieldset', '#title' => t('Select translations for %title', array('%title' => $node->title)), '#tree' => TRUE, '#theme' => 'i18n_node_select_translation', '#description' => t("Alternatively, you can select existing nodes as translations of this one or remove nodes from this translation set. Only nodes that have the right language and don't belong to other translation set will be available here.") ); foreach (language_list() as $language) { if ($language->language != $node->language) { $trans_nid = isset($translations[$language->language]) ? $translations[$language->language]->nid : 0; $form['translations']['nid'][$language->language] = array('#type' => 'value', '#value' => $trans_nid); $form['translations']['language'][$language->language] = array('#value' => $language->name); $form['translations']['node'][$language->language] = array( '#type' => 'textfield', '#autocomplete_path' => 'i18n/node/autocomplete/' . $node->type . '/' . $language->language, '#default_value' => $trans_nid ? i18n_node_nid2autocomplete($trans_nid) : '', ); } } $form['buttons']['update'] = array('#type' => 'submit', '#value' => t('Update translations')); //$form['buttons']['clean'] = array('#type' => 'submit', '#value' => t('Delete translation set')); return $form; } /** * Form validation */ function i18n_node_select_translation_validate($form, &$form_state) { foreach ($form_state['values']['translations']['node'] as $lang => $title) { if (!$title) { $nid = 0; } else { $nid = i18n_node_autocomplete2nid($title, "translations][node][$lang", array($node->type), array($lang)); } $form_state['values']['translations']['nid'][$lang] = $nid; } } /** * Form submission: update / delete the translation set */ function i18n_node_select_translation_submit($form, &$form_state) { $op = isset($form_state['values']['op']) ? $form_state['values']['op'] : NULL; $node = $form_state['values']['node']; $translations = $node->tnid ? translation_node_get_translations($node->tnid) : array($node->language => $node); foreach ($translations as $trans) { $current[$trans->language] = $trans->nid; } $update = array($node->language => $node->nid) + array_filter($form_state['values']['translations']['nid']); // Compute the difference to see which are the new translations and which ones to remove $new = array_diff_assoc($update, $current); $remove = array_diff_assoc($current, $update); // The tricky part: If the existing source is not in the new set, we need to create a new tnid if ($node->tnid && in_array($node->tnid, $update)) { $tnid = $node->tnid; $add = $new; } else { // Create new tnid, which is the source node $tnid = $node->nid; $add = $update; } // Now update values for all nodes if ($add) { $args = array('' => $tnid) + $add; db_query('UPDATE {node} SET tnid = %d WHERE nid IN (' . db_placeholders($add) . ')', $args); if (count($new)) { drupal_set_message(format_plural(count($new), 'Added a node to the translation set.', 'Added @count nodes to the translation set.')); } } if ($remove) { db_query('UPDATE {node} SET tnid = 0 WHERE nid IN (' . db_placeholders($remove) . ')', $remove); drupal_set_message(format_plural(count($remove), 'Removed a node from the translation set.', 'Removed @count nodes from the translation set.')); } } /** * Node title autocomplete callback */ function i18n_node_autocomplete($type, $language, $string = '') { $params = array('type' => $type, 'language' => $language, 'tnid' => 0); $matches = array(); foreach (_i18n_node_references($string, 'contains', $params) as $id => $row) { // Add a class wrapper for a few required CSS overrides. $matches[$row['title'] ." [nid:$id]"] = '