Projet

Général

Profil

Paste
Télécharger (11,8 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / i18n / i18n_sync / i18n_sync.module @ 9faa5de0

1
<?php
2

    
3
/**
4
 * @file
5
 * Internationalization (i18n) package. Synchronization of translations
6
 *
7
 * Keeps vocabulary terms in sync for translations.
8
 * This is a per-vocabulary option.
9
 *
10
 * Ref: http://drupal.org/node/115463
11
 *
12
 * Notes:
13
 * This module needs to run after taxonomy, i18n, translation. Check module weight.
14
 */
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
/**
31
 * Implements hook_help().
32
 */
33
function i18n_sync_help($path, $arg) {
34
  switch ($path) {
35
    case 'admin/help#i18n_sync' :
36
      $output = '<p>' . t('This module synchronizes content taxonomy and fields across translations:') . '</p>';
37
      $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>';
38
      $output .= '<ul>';
39
      $output .= '<li>' . t('All the node fields selected for synchronization will be set to the same value for all translations.') . '</li>';
40
      $output .= '<li>' . t('For multilingual vocabularies, the terms for all translations will be replaced by the translations of the original node terms.') . '</li>';
41
      $output .= '<li>' . t('For other vocabularies, the terms will be just copied over to all the translations.') . '</li>';
42
      $output .= '</ul>';
43
      $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>';
44
      $output .= '<p>' . t('To enable synchronization check content type options to select which fields to synchronize for each node type.') . '</p>';
45
      $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>';
46
      $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>';
47
      return $output;
48
  }
49
}
50

    
51
/**
52
 * Implements hook_hook_info().
53
 */
54
function i18n_sync_hook_info() {
55
  $hooks['i18n_sync_options'] = array(
56
    'group' => 'i18n',
57
  );
58
  return $hooks;
59
}
60

    
61
/**
62
 * Implements hook_field_info_alter()
63
 */
64
function i18n_sync_field_info_alter(&$fields) {
65
  foreach (array('file', 'image') as $type) {
66
    if (isset($fields[$type])) {
67
      $fields[$type]['i18n_sync_callback'] = 'i18n_sync_field_file_sync';
68
    }
69
  }
70
}
71

    
72
/**
73
 * Implements hook_form_FORM_ID_alter().
74
 */
75
function i18n_sync_form_node_type_form_alter(&$form, &$form_state) {
76
  if (isset($form['type'])) {
77
    $type = $form['#node_type']->type;
78
    $disabled = !translation_supported_type($type);
79
    $form['i18n_sync'] = array(
80
      '#type' => 'fieldset',
81
      '#title' => t('Synchronize translations'),
82
      '#collapsible' => TRUE,
83
      '#collapsed' => TRUE,
84
      '#group' => 'additional_settings',
85
      '#attributes' => array(
86
        'class' => array('i18n-node-type-settings-form'),
87
      ),
88
      '#description' => t('Select which fields to synchronize for all translations of this content type.'),
89
      '#disabled' => $disabled,
90
    );
91

    
92
    $form['i18n_sync']['i18n_sync_node_type'] = array(
93
      '#tree' => TRUE,
94
      '#weight' => 1,
95
    );
96

    
97
    // Each set provides title and options. We build a big checkboxes control for it to be
98
    // saved as an array.
99
    $current = i18n_sync_node_fields($type);
100
    // Group options, group fields by type.
101
    $groups = array(
102
      'node' => t('Standard node fields'),
103
      'fields' => t('Configurable fields'),
104
    );
105
    $fields = array();
106
    foreach (i18n_sync_node_options($type) as $field => $info) {
107
      $group = isset($info['group']) && isset($groups[$info['group']]) ? $info['group'] : 'node';
108
      $fields[$group][$field] = $info;
109
    }
110
    foreach ($fields as $group => $group_fields) {
111
      $form['i18n_sync']['i18n_sync_node_type']['i18n_sync_group_' . $group] = array(
112
        '#prefix' => '<strong>', '#suffix' => '</strong>',
113
        '#markup' => $groups[$group],
114
      );
115
      foreach ($group_fields as $field => $info) {
116
        $form['i18n_sync']['i18n_sync_node_type'][$field] = array(
117
          '#title' => $info['title'],
118
          '#type' => 'checkbox',
119
          '#default_value' => in_array($field, $current),
120
          '#disabled' => $disabled,
121
          '#description' => isset($info['description']) ? $info['description'] : '',
122
        );
123
      }
124
    }
125

    
126
    // Add option to restrict syncing only when editing the translation source.
127
    $form['i18n_sync']['i18n_sync_source_description'] = array(
128
      '#prefix' => '<div>', '#suffix' => '</div>',
129
      '#markup' => t('Restrict synchronization to the translation source.'),
130
      '#weight' => 2,
131
    );
132
    $form['i18n_sync']['i18n_sync_source'] = array(
133
      '#type' => 'checkbox',
134
      '#title' => t('Synchronize translations only when saving the translation source'),
135
      '#default_value' => variable_get('i18n_sync_source_' . $type, FALSE),
136
      '#disabled' => $disabled,
137
      '#weight' => 3,
138
      '#description' => t('If not checked each node will trigger the synchronization, whether it\'s the source or not.'),
139
    );
140
  }
141
}
142

    
143
/**
144
 * Check whether this node is to be synced
145
 */
146
function i18n_sync_node_check($node) {
147
  return translation_supported_type($node->type) && i18n_object_langcode($node) && i18n_sync();
148
}
149

    
150
/**
151
 * Get node translations if any, excluding the node itself
152
 *
153
 * @param $reset
154
 *   Whether to reset the translation_node_get_translations cache.
155
 */
156
function i18n_sync_node_get_translations($node, $reset = FALSE) {
157
  if (!empty($node->tnid)) {
158
    if ($reset) {
159
      // Clear the static cache of translation_node_get_translations
160
      $translations_cache = &drupal_static('translation_node_get_translations', array());
161
      unset($translations_cache[$node->tnid]);
162
    }
163
    // Maybe translations are already here
164
    if ($translations = translation_node_get_translations($node->tnid)) {
165
      unset($translations[$node->language]);
166
      return $translations;
167
    }
168
  }
169
}
170

    
171
/**
172
 * Implements hook_node_insert().
173
 */
174
function i18n_sync_node_insert($node) {
175
  // When creating a translation, there are some aditional steps, different from update
176
  if (i18n_sync_node_check($node) && !empty($node->translation_source)) {
177
    i18n_sync_node_update($node);
178
  }
179
}
180

    
181
/**
182
 * Implements hook_node_update().
183
 */
184
function i18n_sync_node_update($node) {
185
  // Let's go with field synchronization.
186
  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))) {
187
    $do_sync = TRUE;
188
    if (module_exists('entity_translation')) {
189
      if (entity_translation_enabled_bundle('node', $node->type)) {
190
        $do_sync = FALSE;
191
      }
192
    }
193
    if ($do_sync) {
194
      module_load_include('node.inc', 'i18n_sync');
195
      i18n_sync_node_translation($node, $translations, $fields, 'update');
196
    }
197
  }
198
}
199

    
200
/**
201
 * Implements hook_node_prepare().
202
 */
203
function i18n_sync_node_prepare($node) {
204
  // If creating a translation, copy over all the fields to be synchronized.
205
  if (empty($node->nid) && !empty($node->translation_source) && ($sync_fields = i18n_sync_node_fields($node->type))) {
206
    $source = $node->translation_source;
207
    $i18n_options = i18n_sync_node_options($node->type);
208
    // Copy over standard node fields. The rest are handled by field_attach_prepare_translation()
209
    foreach ($sync_fields as $field) {
210
      if (!empty($source->$field) && empty($i18n_options[$field]['field_name'])) {
211
        if ($field == 'uid') {
212
          $node->name = $source->name;
213
        }
214
        elseif ($field == 'created') {
215
          $node->date = format_date($source->created, 'custom', 'Y-m-d H:i:s O');
216
        }
217
        else {
218
          $node->$field = $source->$field;
219
        }
220
      }
221
    }
222
  }
223
}
224

    
225
/**
226
 * Returns list of fields to synchronize for a given content type.
227
 *
228
 * @param $type
229
 *   Node type.
230
 * @param $field
231
 *   Optional field name to check whether it is in the list
232
 */
233
function i18n_sync_node_fields($type, $field = NULL) {
234
  $fields = variable_get('i18n_sync_node_type_' . $type, array());
235
  return $field ? in_array($field, $fields) : $fields;
236
}
237

    
238
/**
239
 * Returns list of available fields for given content type.
240
 *
241
 * Fields can also be changed using hook_i18n_sync_fields_alter($fields, $type)
242
 *
243
 * @param $type
244
 *   Node type.
245
 */
246
function i18n_sync_node_options($type) {
247
  return i18n_sync_options('node', $type);
248
}
249

    
250
/**
251
 * Returns list of available fields for given entity / bundle.
252
 *
253
 * Fields can also be changed using hook_i18n_sync_options_alter($fields, $type)
254
 *
255
 * @param $entity_type
256
 *   Entity type.
257
 * @param
258
 */
259
function i18n_sync_options($entity_type, $bundle_name) {
260
  $cache = &drupal_static(__FUNCTION__);
261

    
262
  if (!isset($cache[$entity_type][$bundle_name])) {
263
    module_load_include('modules.inc', 'i18n_sync');
264
    $fields = module_invoke_all('i18n_sync_options', $entity_type, $bundle_name);
265
    // Give a chance to modules to change/remove/add their own fields
266
    drupal_alter('i18n_sync_options', $fields, $entity_type, $bundle_name);
267
    $cache[$entity_type][$bundle_name] = $fields;
268
  }
269

    
270
  return $cache[$entity_type][$bundle_name];
271
}
272

    
273
/**
274
 * Synchronize entity field translation
275
 */
276
function i18n_sync_field_translation_sync($entity_type, $bundle_name, $entity, $langcode, $source_entity, $source_langcode, $field_name) {
277
  $field = field_info_field($field_name);
278
  $instance = field_info_instance($entity_type, $field_name, $bundle_name);
279
  $source_lang = field_language($entity_type, $source_entity, $field_name);
280
  $translation_lang = field_is_translatable($entity_type, $field) ? $entity->language : LANGUAGE_NONE;
281
  if (isset($source_entity->{$field_name}[$source_lang])) {
282
    $items = $source_entity->{$field_name}[$source_lang];
283
    // Determine the function to synchronize this field. If none, just copy over.
284
    $type_info = field_info_field_types($field['type']);
285
    if (isset($type_info['i18n_sync_callback'])) {
286
      $function = $type_info['i18n_sync_callback'];
287
      $function($entity_type, $entity, $field, $instance, $langcode, $items, $source_entity, $source_langcode);
288
    }
289
    $entity->{$field_name}[$translation_lang] = $items;
290
  }
291
  else {
292
    // If source not set, unset translation too
293
    unset($entity->{$field_name}[$translation_lang]);
294
  }
295
}
296

    
297
/**
298
 * Sync a file or image field (i18n_sync_callback)
299
 *
300
 * - file-id's (fid) are synced
301
 * - order of fid's is synced
302
 * - description, alt, title is kept if already existing, copied otherwise
303
 */
304
function i18n_sync_field_file_sync($entity_type, $entity, $field, $instance, $langcode, &$items, $source_entity, $source_language) {
305
  $field_name = $instance['field_name'];
306
  // Build a copy of the existing files in the translation node
307
  // indexed by fid for easy retrieval in the copy loop below
308
  $existing_files = array();
309
  $field_language = field_language($entity_type, $entity, $field_name, $langcode);
310
  if (isset($entity->{$field_name}[$field_language])) {
311
    foreach ($entity->{$field_name}[$field_language] as $delta => $file) {
312
      $existing_files[$file['fid']] = $file;
313
    }
314
  }
315
  // Start creating the translated copy
316
  foreach ($items as $delta => &$file) {
317
    // keep alt, title, description if they already exist
318
    if (isset($existing_files[$file['fid']])) {
319
      foreach (array('title', 'description', 'alt') as $property) {
320
        if (!empty($existing_files[$file['fid']][$property])) {
321
          $file[$property] = $existing_files[$file['fid']][$property];
322
        }
323
      }
324
    }
325
  }
326
}