'. t('This module synchronizes content taxonomy and fields accross translations:') .'

'; $output .= '

'. t('First you need to select which fields should be synchronized. Then, after a node has been updated, all enabled vocabularies and fields will be synchronized as follows:') .'

'; $output .= ''; $output .= '

'. t('Note that permissions are not checked for each node. So if someone can edit a node and it is set to synchronize, all the translations will be synchronized anyway.') .'

'; $output .= '

'. t('To enable synchronization check content type options to select which fields to synchronize for each node type.') .'

'; $output .= '

'. t('The list of available fields for synchronization will include some standard node fields and all CCK fields. You can add more fields to the list in a configuration variable. See README.txt for how to do it.') .'

'; $output .= '

'. t('For more information, see the online handbook entry for Internationalization module.', array('@i18n' => 'http://drupal.org/node/133977')) .'

'; return $output; } } /** * Implements hook_field_attach_prepare_translation_alter() */ function i18n_sync_field_attach_prepare_translation_alter(&$entity, $context) { } /** * Implements hook_form_FORM_ID_alter(). */ function i18n_sync_form_node_admin_content_alter(&$form, &$form_state) { if (!empty($form['operation']) && $form['operation']['#value'] == 'delete') { $form['#submit'] = array_merge(array('i18n_sync_node_delete_submit'), $form['#submit']); } } /** * Implements hook_form_FORM_ID_alter(). */ function i18n_sync_form_node_delete_confirm_alter(&$form, &$form_state) { // Intercept form submission so we can handle uploads, replace callback $form['#submit'] = array_merge(array('i18n_sync_node_delete_submit'), $form['#submit']); } /** * Implements hook_form_FORM_ID_alter(). */ function i18n_sync_form_node_type_form_alter(&$form, &$form_state) { if (isset($form['type'])) { $type = $form['#node_type']->type; $disabled = !translation_supported_type($type); $form['i18n_sync'] = array( '#type' => 'fieldset', '#title' => t('Synchronize translations'), '#collapsible' => TRUE, '#collapsed' => TRUE, '#group' => 'additional_settings', '#attributes' => array( 'class' => array('i18n-node-type-settings-form'), ), '#description' => t('Select which fields to synchronize for all translations of this content type.'), '#disabled' => $disabled, ); $form['i18n_sync']['i18n_sync_node_type'] = array( '#tree' => TRUE, ); // Each set provides title and options. We build a big checkboxes control for it to be // saved as an array. $current = i18n_sync_node_fields($type); // Group options, group fields by type. $groups = array( 'node' => t('Standard node fields'), 'fields' => t('Configurable fields'), ); $fields = array(); foreach (i18n_sync_node_options($type) as $field => $info) { $group = isset($info['group']) && isset($groups[$info['group']]) ? $info['group'] : 'node'; $fields[$group][$field] = $info; } foreach ($fields as $group => $group_fields) { $form['i18n_sync']['i18n_sync_node_type']['i18n_sync_group_' . $group] = array( '#prefix' => '', '#suffix' => '', '#markup' => $groups[$group], ); foreach ($group_fields as $field => $info) { $form['i18n_sync']['i18n_sync_node_type'][$field] = array( '#title' => $info['title'], '#type' => 'checkbox', '#default_value' => in_array($field, $current), '#disabled' => $disabled, '#description' => isset($info['description']) ? $info['description'] : '', ); } } } } /** * Submit callback for * - node delete confirm * - node multiple delete confirm */ function i18n_sync_node_delete_submit($form, $form_state) { if ($form_state['values']['confirm']) { if (!empty($form_state['values']['nid'])) { // Single node i18n_sync_node_delete_prepare($form_state['values']['nid']); } elseif (!empty($form_state['values']['nodes'])) { // Multiple nodes foreach ($form_state['values']['nodes'] as $nid => $value) { i18n_sync_node_delete_prepare($nid); } } } // Then it will go through normal form submission } /** * Prepare node for deletion, work out synchronization issues */ function i18n_sync_node_delete_prepare($nid) { $node = node_load($nid); // Delete file associations when files are shared with existing translations // so they are not removed by upload module if (!empty($node->tnid) && module_exists('upload')) { $result = db_query('SELECT u.* FROM {upload} u WHERE u.nid = %d AND u.fid IN (SELECT t.fid FROM {upload} t WHERE t.fid = u.fid AND t.nid <> u.nid)', $nid); while ($up = db_fetch_object($result)) { db_query("DELETE FROM {upload} WHERE fid = %d AND vid = %d", $up->fid, $up->vid); } } } /** * Check whether this node is to be synced */ function i18n_sync_node_check($node) { return translation_supported_type($node->type) && i18n_object_langcode($node) && i18n_sync(); } /** * Get node translations if any, excluding the node itself */ function i18n_sync_node_get_translations($node) { // Maybe translations are already here if (!empty($node->tnid) && ($translations = translation_node_get_translations($node->tnid))) { unset($translations[$node->language]); return $translations; } } /** * Implements hook_node_insert(). */ function i18n_sync_node_insert($node) { // When creating a translation, there are some aditional steps, different from update if (i18n_sync_node_check($node) && !empty($node->translation_source)) { i18n_sync_node_update($node); } } /** * Implements hook_node_update(). */ function i18n_sync_node_update($node) { // Let's go with field synchronization. if (i18n_sync_node_check($node) && !empty($node->tnid) && ($fields = i18n_sync_node_fields($node->type)) && ($translations = i18n_sync_node_get_translations($node))) { module_load_include('node.inc', 'i18n_sync'); i18n_sync_node_translation($node, $translations, $fields, 'update'); } } /** * Implements hook_node_prepare(). */ function i18n_sync_node_prepare($node) { // If creating a translation, copy over all the fields to be synchronized. if (empty($node->nid) && !empty($node->translation_source) && ($sync_fields = i18n_sync_node_fields($node->type))) { foreach ($sync_fields as $field) { if (empty($node->translation_source->$field)) continue; switch ($field) { case 'taxonomy': // Do nothing, this is handled by the i18n_taxonomy module break; default: $node->$field = $node->translation_source->$field; break; } } } } /** * Returns list of fields to synchronize for a given content type. * * @param $type * Node type. * @param $field * Optional field name to check whether it is in the list */ function i18n_sync_node_fields($type, $field = NULL) { $fields = variable_get('i18n_sync_node_type_'. $type, array()); return $field ? in_array($field, $fields) : $fields; } /** * Returns list of available fields for given content type. * * Fields can also be changed using hook_i18n_sync_fields_alter($fields, $type) * * @param $type * Node type. */ function i18n_sync_node_options($type) { return i18n_sync_options('node', $type); } /** * Returns list of available fields for given entity / bundle. * * Fields can also be changed using hook_i18n_sync_options_alter($fields, $type) * * @param $entity_type * Entity type. * @param */ function i18n_sync_options($entity_type, $bundle_name) { $cache = &drupal_static(__FUNCTION__); if (!isset($cache[$entity_type][$bundle_name])) { module_load_include('modules.inc', 'i18n_sync'); $fields = module_invoke_all('i18n_sync_options', $entity_type, $bundle_name); // Give a chance to modules to change/remove/add their own fields drupal_alter('i18n_sync_options', $fields, $entity_type, $bundle_name); $cache[$entity_type][$bundle_name] = $fields; } return $cache[$entity_type][$bundle_name]; }