i18n_sync.module 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 accross 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. );
  88. // Each set provides title and options. We build a big checkboxes control for it to be
  89. // saved as an array.
  90. $current = i18n_sync_node_fields($type);
  91. // Group options, group fields by type.
  92. $groups = array(
  93. 'node' => t('Standard node fields'),
  94. 'fields' => t('Configurable fields'),
  95. );
  96. $fields = array();
  97. foreach (i18n_sync_node_options($type) as $field => $info) {
  98. $group = isset($info['group']) && isset($groups[$info['group']]) ? $info['group'] : 'node';
  99. $fields[$group][$field] = $info;
  100. }
  101. foreach ($fields as $group => $group_fields) {
  102. $form['i18n_sync']['i18n_sync_node_type']['i18n_sync_group_' . $group] = array(
  103. '#prefix' => '<strong>', '#suffix' => '</strong>',
  104. '#markup' => $groups[$group],
  105. );
  106. foreach ($group_fields as $field => $info) {
  107. $form['i18n_sync']['i18n_sync_node_type'][$field] = array(
  108. '#title' => $info['title'],
  109. '#type' => 'checkbox',
  110. '#default_value' => in_array($field, $current),
  111. '#disabled' => $disabled,
  112. '#description' => isset($info['description']) ? $info['description'] : '',
  113. );
  114. }
  115. }
  116. }
  117. }
  118. /**
  119. * Check whether this node is to be synced
  120. */
  121. function i18n_sync_node_check($node) {
  122. return translation_supported_type($node->type) && i18n_object_langcode($node) && i18n_sync();
  123. }
  124. /**
  125. * Get node translations if any, excluding the node itself
  126. *
  127. * @param $reset
  128. * Whether to reset the translation_node_get_translations cache.
  129. */
  130. function i18n_sync_node_get_translations($node, $reset = FALSE) {
  131. if (!empty($node->tnid)) {
  132. if ($reset) {
  133. // Clear the static cache of translation_node_get_translations
  134. $translations_cache = &drupal_static('translation_node_get_translations', array());
  135. unset($translations_cache[$node->tnid]);
  136. }
  137. // Maybe translations are already here
  138. if ($translations = translation_node_get_translations($node->tnid)) {
  139. unset($translations[$node->language]);
  140. return $translations;
  141. }
  142. }
  143. }
  144. /**
  145. * Implements hook_node_insert().
  146. */
  147. function i18n_sync_node_insert($node) {
  148. // When creating a translation, there are some aditional steps, different from update
  149. if (i18n_sync_node_check($node) && !empty($node->translation_source)) {
  150. i18n_sync_node_update($node);
  151. }
  152. }
  153. /**
  154. * Implements hook_node_update().
  155. */
  156. function i18n_sync_node_update($node) {
  157. // Let's go with field synchronization.
  158. if (i18n_sync_node_check($node) && !empty($node->tnid) && ($fields = i18n_sync_node_fields($node->type)) && ($translations = i18n_sync_node_get_translations($node, TRUE))) {
  159. module_load_include('node.inc', 'i18n_sync');
  160. i18n_sync_node_translation($node, $translations, $fields, 'update');
  161. }
  162. }
  163. /**
  164. * Implements hook_node_prepare().
  165. */
  166. function i18n_sync_node_prepare($node) {
  167. // If creating a translation, copy over all the fields to be synchronized.
  168. if (empty($node->nid) && !empty($node->translation_source) && ($sync_fields = i18n_sync_node_fields($node->type))) {
  169. $source = $node->translation_source;
  170. $i18n_options = i18n_sync_node_options($node->type);
  171. // Copy over standard node fields. The rest are handled by field_attach_prepare_translation()
  172. foreach ($sync_fields as $field) {
  173. if (!empty($source->$field) && empty($i18n_options[$field]['field_name'])) {
  174. if ($field == 'uid') {
  175. $node->name = $source->name;
  176. }
  177. elseif ($field == 'created') {
  178. $node->date = format_date($source->created, 'custom', 'Y-m-d H:i:s O');
  179. }
  180. else {
  181. $node->$field = $source->$field;
  182. }
  183. }
  184. }
  185. }
  186. }
  187. /**
  188. * Returns list of fields to synchronize for a given content type.
  189. *
  190. * @param $type
  191. * Node type.
  192. * @param $field
  193. * Optional field name to check whether it is in the list
  194. */
  195. function i18n_sync_node_fields($type, $field = NULL) {
  196. $fields = variable_get('i18n_sync_node_type_' . $type, array());
  197. return $field ? in_array($field, $fields) : $fields;
  198. }
  199. /**
  200. * Returns list of available fields for given content type.
  201. *
  202. * Fields can also be changed using hook_i18n_sync_fields_alter($fields, $type)
  203. *
  204. * @param $type
  205. * Node type.
  206. */
  207. function i18n_sync_node_options($type) {
  208. return i18n_sync_options('node', $type);
  209. }
  210. /**
  211. * Returns list of available fields for given entity / bundle.
  212. *
  213. * Fields can also be changed using hook_i18n_sync_options_alter($fields, $type)
  214. *
  215. * @param $entity_type
  216. * Entity type.
  217. * @param
  218. */
  219. function i18n_sync_options($entity_type, $bundle_name) {
  220. $cache = &drupal_static(__FUNCTION__);
  221. if (!isset($cache[$entity_type][$bundle_name])) {
  222. module_load_include('modules.inc', 'i18n_sync');
  223. $fields = module_invoke_all('i18n_sync_options', $entity_type, $bundle_name);
  224. // Give a chance to modules to change/remove/add their own fields
  225. drupal_alter('i18n_sync_options', $fields, $entity_type, $bundle_name);
  226. $cache[$entity_type][$bundle_name] = $fields;
  227. }
  228. return $cache[$entity_type][$bundle_name];
  229. }
  230. /**
  231. * Synchronize entity field translation
  232. */
  233. function i18n_sync_field_translation_sync($entity_type, $bundle_name, $entity, $langcode, $source_entity, $source_langcode, $field_name) {
  234. $field = field_info_field($field_name);
  235. $instance = field_info_instance($entity_type, $field_name, $bundle_name);
  236. $source_lang = field_language($entity_type, $source_entity, $field_name);
  237. $translation_lang = field_is_translatable($entity_type, $field) ? $entity->language : LANGUAGE_NONE;
  238. if (isset($source_entity->{$field_name}[$source_lang])) {
  239. $items = $source_entity->{$field_name}[$source_lang];
  240. // Determine the function to synchronize this field. If none, just copy over.
  241. $type_info = field_info_field_types($field['type']);
  242. if (isset($type_info['i18n_sync_callback'])) {
  243. $function = $type_info['i18n_sync_callback'];
  244. $function($entity_type, $entity, $field, $instance, $langcode, $items, $source_entity, $source_langcode);
  245. }
  246. $entity->{$field_name}[$translation_lang] = $items;
  247. }
  248. else {
  249. // If source not set, unset translation too
  250. unset($entity->{$field_name}[$translation_lang]);
  251. }
  252. }
  253. /**
  254. * Sync a file or image field (i18n_sync_callback)
  255. *
  256. * - file-id's (fid) are synced
  257. * - order of fid's is synced
  258. * - description, alt, title is kept if already existing, copied otherwise
  259. */
  260. function i18n_sync_field_file_sync($entity_type, $entity, $field, $instance, $langcode, &$items, $source_entity, $source_language) {
  261. $field_name = $instance['field_name'];
  262. // Build a copy of the existing files in the translation node
  263. // indexed by fid for easy retrieval in the copy loop below
  264. $existing_files = array();
  265. $field_language = field_language($entity_type, $entity, $field_name, $langcode);
  266. if (isset($entity->{$field_name}[$field_language])) {
  267. foreach ($entity->{$field_name}[$field_language] as $delta => $file) {
  268. $existing_files[$file['fid']] = $file;
  269. }
  270. }
  271. // Start creating the translated copy
  272. foreach ($items as $delta => &$file) {
  273. // keep alt, title, description if they already exist
  274. if (isset($existing_files[$file['fid']])) {
  275. foreach (array('title', 'description', 'alt') as $property) {
  276. if (!empty($existing_files[$file['fid']][$property])) {
  277. $file[$property] = $existing_files[$file['fid']][$property];
  278. }
  279. }
  280. }
  281. }
  282. }