i18n_sync.module 12 KB

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