i18n_sync.module 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <?php
  2. /**
  3. * @file
  4. * Internationalization (i18n) package. Synchronization of translations
  5. *
  6. * Keeps vocabulary terms in sync for translations.
  7. * This is a per-vocabulary option.
  8. *
  9. * Ref: http://drupal.org/node/115463
  10. *
  11. * Notes:
  12. * This module needs to run after taxonomy, i18n, translation. Check module weight.
  13. *
  14. * @ TODO Test with CCK when possible, api may have changed.
  15. */
  16. /**
  17. * Global switch to enable / disable syncing and check whether we are synching at the moment
  18. *
  19. * @return boolean
  20. * TRUE if we need to run sync operations. FALSE during syncing so we don't have recursion.
  21. */
  22. function i18n_sync($status = NULL) {
  23. static $current = TRUE;
  24. if (isset($status)) {
  25. $current = $status;
  26. }
  27. return $current;
  28. }
  29. /**
  30. * Implements hook_help().
  31. */
  32. function i18n_sync_help($path, $arg) {
  33. switch ($path) {
  34. case 'admin/help#i18n_sync' :
  35. $output = '<p>' . t('This module synchronizes content taxonomy and fields accross translations:') . '</p>';
  36. $output .= '<p>' . 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:') . '</p>';
  37. $output .= '<ul>';
  38. $output .= '<li>' . t('All the node fields selected for synchronization will be set to the same value for all translations.') . '</li>';
  39. $output .= '<li>' . t('For multilingual vocabularies, the terms for all translations will be replaced by the translations of the original node terms.') . '</li>';
  40. $output .= '<li>' . t('For other vocabularies, the terms will be just copied over to all the translations.') . '</li>';
  41. $output .= '</ul>';
  42. $output .= '<p><strong>' . 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.') . '</strong></p>';
  43. $output .= '<p>' . t('To enable synchronization check content type options to select which fields to synchronize for each node type.') . '</p>';
  44. $output .= '<p>' . 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.') . '</p>';
  45. $output .= '<p>' . t('For more information, see the online handbook entry for <a href="@i18n">Internationalization module</a>.', array('@i18n' => 'http://drupal.org/node/133977')) . '</p>';
  46. return $output;
  47. }
  48. }
  49. /**
  50. * Implements hook_hook_info().
  51. */
  52. function i18n_sync_hook_info() {
  53. $hooks['i18n_sync_options'] = array(
  54. 'group' => 'i18n',
  55. );
  56. return $hooks;
  57. }
  58. /**
  59. * Implements hook_field_info_alter()
  60. */
  61. function i18n_sync_field_info_alter(&$fields) {
  62. foreach (array('file', 'image') as $type) {
  63. if (isset($fields[$type])) {
  64. $fields[$type]['i18n_sync_callback'] = 'i18n_sync_field_file_sync';
  65. }
  66. }
  67. }
  68. /**
  69. * Implements hook_form_FORM_ID_alter().
  70. */
  71. function i18n_sync_form_node_admin_content_alter(&$form, &$form_state) {
  72. if (!empty($form['operation']) && $form['operation']['#value'] == 'delete') {
  73. $form['#submit'] = array_merge(array('i18n_sync_node_delete_submit'), $form['#submit']);
  74. }
  75. }
  76. /**
  77. * Implements hook_form_FORM_ID_alter().
  78. */
  79. function i18n_sync_form_node_delete_confirm_alter(&$form, &$form_state) {
  80. // Intercept form submission so we can handle uploads, replace callback
  81. $form['#submit'] = array_merge(array('i18n_sync_node_delete_submit'), $form['#submit']);
  82. }
  83. /**
  84. * Implements hook_form_FORM_ID_alter().
  85. */
  86. function i18n_sync_form_node_type_form_alter(&$form, &$form_state) {
  87. if (isset($form['type'])) {
  88. $type = $form['#node_type']->type;
  89. $disabled = !translation_supported_type($type);
  90. $form['i18n_sync'] = array(
  91. '#type' => 'fieldset',
  92. '#title' => t('Synchronize translations'),
  93. '#collapsible' => TRUE,
  94. '#collapsed' => TRUE,
  95. '#group' => 'additional_settings',
  96. '#attributes' => array(
  97. 'class' => array('i18n-node-type-settings-form'),
  98. ),
  99. '#description' => t('Select which fields to synchronize for all translations of this content type.'),
  100. '#disabled' => $disabled,
  101. );
  102. $form['i18n_sync']['i18n_sync_node_type'] = array(
  103. '#tree' => TRUE,
  104. );
  105. // Each set provides title and options. We build a big checkboxes control for it to be
  106. // saved as an array.
  107. $current = i18n_sync_node_fields($type);
  108. // Group options, group fields by type.
  109. $groups = array(
  110. 'node' => t('Standard node fields'),
  111. 'fields' => t('Configurable fields'),
  112. );
  113. $fields = array();
  114. foreach (i18n_sync_node_options($type) as $field => $info) {
  115. $group = isset($info['group']) && isset($groups[$info['group']]) ? $info['group'] : 'node';
  116. $fields[$group][$field] = $info;
  117. }
  118. foreach ($fields as $group => $group_fields) {
  119. $form['i18n_sync']['i18n_sync_node_type']['i18n_sync_group_' . $group] = array(
  120. '#prefix' => '<strong>', '#suffix' => '</strong>',
  121. '#markup' => $groups[$group],
  122. );
  123. foreach ($group_fields as $field => $info) {
  124. $form['i18n_sync']['i18n_sync_node_type'][$field] = array(
  125. '#title' => $info['title'],
  126. '#type' => 'checkbox',
  127. '#default_value' => in_array($field, $current),
  128. '#disabled' => $disabled,
  129. '#description' => isset($info['description']) ? $info['description'] : '',
  130. );
  131. }
  132. }
  133. }
  134. }
  135. /**
  136. * Submit callback for
  137. * - node delete confirm
  138. * - node multiple delete confirm
  139. */
  140. function i18n_sync_node_delete_submit($form, $form_state) {
  141. if ($form_state['values']['confirm']) {
  142. if (!empty($form_state['values']['nid'])) {
  143. // Single node
  144. i18n_sync_node_delete_prepare($form_state['values']['nid']);
  145. }
  146. elseif (!empty($form_state['values']['nodes'])) {
  147. // Multiple nodes
  148. foreach ($form_state['values']['nodes'] as $nid => $value) {
  149. i18n_sync_node_delete_prepare($nid);
  150. }
  151. }
  152. }
  153. // Then it will go through normal form submission
  154. }
  155. /**
  156. * Prepare node for deletion, work out synchronization issues
  157. */
  158. function i18n_sync_node_delete_prepare($nid) {
  159. $node = node_load($nid);
  160. // Delete file associations when files are shared with existing translations
  161. // so they are not removed by upload module
  162. if (!empty($node->tnid) && module_exists('upload')) {
  163. $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);
  164. while ($up = db_fetch_object($result)) {
  165. db_query("DELETE FROM {upload} WHERE fid = %d AND vid = %d", $up->fid, $up->vid);
  166. }
  167. }
  168. }
  169. /**
  170. * Check whether this node is to be synced
  171. */
  172. function i18n_sync_node_check($node) {
  173. return translation_supported_type($node->type) && i18n_object_langcode($node) && i18n_sync();
  174. }
  175. /**
  176. * Get node translations if any, excluding the node itself
  177. *
  178. * @param $reset
  179. * Whether to reset the translation_node_get_translations cache.
  180. */
  181. function i18n_sync_node_get_translations($node, $reset = FALSE) {
  182. if (!empty($node->tnid)) {
  183. if ($reset) {
  184. // Clear the static cache of translation_node_get_translations
  185. $translations_cache = &drupal_static('translation_node_get_translations', array());
  186. unset($translations_cache[$node->tnid]);
  187. }
  188. // Maybe translations are already here
  189. if ($translations = translation_node_get_translations($node->tnid)) {
  190. unset($translations[$node->language]);
  191. return $translations;
  192. }
  193. }
  194. }
  195. /**
  196. * Implements hook_node_insert().
  197. */
  198. function i18n_sync_node_insert($node) {
  199. // When creating a translation, there are some aditional steps, different from update
  200. if (i18n_sync_node_check($node) && !empty($node->translation_source)) {
  201. i18n_sync_node_update($node);
  202. }
  203. }
  204. /**
  205. * Implements hook_node_update().
  206. */
  207. function i18n_sync_node_update($node) {
  208. // Let's go with field synchronization.
  209. if (i18n_sync_node_check($node) && !empty($node->tnid) && ($fields = i18n_sync_node_fields($node->type)) && ($translations = i18n_sync_node_get_translations($node, TRUE))) {
  210. module_load_include('node.inc', 'i18n_sync');
  211. i18n_sync_node_translation($node, $translations, $fields, 'update');
  212. }
  213. }
  214. /**
  215. * Implements hook_node_prepare().
  216. */
  217. function i18n_sync_node_prepare($node) {
  218. // If creating a translation, copy over all the fields to be synchronized.
  219. if (empty($node->nid) && !empty($node->translation_source) && ($sync_fields = i18n_sync_node_fields($node->type))) {
  220. $source = $node->translation_source;
  221. $i18n_options = i18n_sync_node_options($node->type);
  222. // Copy over standard node fields. The rest are handled by field_attach_prepare_translation()
  223. foreach ($sync_fields as $field) {
  224. if (!empty($source->$field) && empty($i18n_options[$field]['field_name'])) {
  225. if ($field == 'uid') {
  226. $node->name = $source->name;
  227. }
  228. elseif ($field == 'created') {
  229. $node->date = format_date($source->created, 'custom', 'Y-m-d H:i:s O');
  230. }
  231. else {
  232. $node->$field = $source->$field;
  233. }
  234. }
  235. }
  236. }
  237. }
  238. /**
  239. * Returns list of fields to synchronize for a given content type.
  240. *
  241. * @param $type
  242. * Node type.
  243. * @param $field
  244. * Optional field name to check whether it is in the list
  245. */
  246. function i18n_sync_node_fields($type, $field = NULL) {
  247. $fields = variable_get('i18n_sync_node_type_' . $type, array());
  248. return $field ? in_array($field, $fields) : $fields;
  249. }
  250. /**
  251. * Returns list of available fields for given content type.
  252. *
  253. * Fields can also be changed using hook_i18n_sync_fields_alter($fields, $type)
  254. *
  255. * @param $type
  256. * Node type.
  257. */
  258. function i18n_sync_node_options($type) {
  259. return i18n_sync_options('node', $type);
  260. }
  261. /**
  262. * Returns list of available fields for given entity / bundle.
  263. *
  264. * Fields can also be changed using hook_i18n_sync_options_alter($fields, $type)
  265. *
  266. * @param $entity_type
  267. * Entity type.
  268. * @param
  269. */
  270. function i18n_sync_options($entity_type, $bundle_name) {
  271. $cache = &drupal_static(__FUNCTION__);
  272. if (!isset($cache[$entity_type][$bundle_name])) {
  273. module_load_include('modules.inc', 'i18n_sync');
  274. $fields = module_invoke_all('i18n_sync_options', $entity_type, $bundle_name);
  275. // Give a chance to modules to change/remove/add their own fields
  276. drupal_alter('i18n_sync_options', $fields, $entity_type, $bundle_name);
  277. $cache[$entity_type][$bundle_name] = $fields;
  278. }
  279. return $cache[$entity_type][$bundle_name];
  280. }
  281. /**
  282. * Synchronize entity field translation
  283. */
  284. function i18n_sync_field_translation_sync($entity_type, $bundle_name, $entity, $langcode, $source_entity, $source_langcode, $field_name) {
  285. $field = field_info_field($field_name);
  286. $instance = field_info_instance($entity_type, $field_name, $bundle_name);
  287. $source_lang = field_language($entity_type, $source_entity, $field_name);
  288. $translation_lang = field_is_translatable($entity_type, $field) ? $entity->language : LANGUAGE_NONE;
  289. if (isset($source_entity->{$field_name}[$source_lang])) {
  290. $items = $source_entity->{$field_name}[$source_lang];
  291. // Determine the function to synchronize this field. If none, just copy over.
  292. $type_info = field_info_field_types($field['type']);
  293. if (isset($type_info['i18n_sync_callback'])) {
  294. $function = $type_info['i18n_sync_callback'];
  295. $function($entity_type, $entity, $field, $instance, $langcode, $items, $source_entity, $source_langcode);
  296. }
  297. $entity->{$field_name}[$translation_lang] = $items;
  298. }
  299. else {
  300. // If source not set, unset translation too
  301. unset($entity->{$field_name}[$translation_lang]);
  302. }
  303. }
  304. /**
  305. * Sync a file or image field (i18n_sync_callback)
  306. *
  307. * - file-id's (fid) are synced
  308. * - order of fid's is synced
  309. * - description, alt, title is kept if already existing, copied otherwise
  310. */
  311. function i18n_sync_field_file_sync($entity_type, $entity, $field, $instance, $langcode, &$items, $source_entity, $source_language) {
  312. $field_name = $instance['field_name'];
  313. // Build a copy of the existing files in the translation node
  314. // indexed by fid for easy retrieval in the copy loop below
  315. $existing_files = array();
  316. $field_language = field_language($entity_type, $entity, $field_name, $langcode);
  317. if (isset($entity->{$field_name}[$field_language])) {
  318. foreach ($entity->{$field_name}[$field_language] as $delta => $file) {
  319. $existing_files[$file['fid']] = $file;
  320. }
  321. }
  322. // Start creating the translated copy
  323. foreach ($items as $delta => &$file) {
  324. // keep alt, title, description if they already exist
  325. if (isset($existing_files[$file['fid']])) {
  326. foreach (array('title', 'description', 'alt') as $property) {
  327. if (!empty($existing_files[$file['fid']][$property])) {
  328. $file[$property] = $existing_files[$file['fid']][$property];
  329. }
  330. }
  331. }
  332. }
  333. }