Projet

Général

Profil

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

root / drupal7 / sites / all / modules / i18n / i18n_sync / i18n_sync.module @ 6331c987

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
 * @ TODO Test with CCK when possible, api may have changed.
16
 */
17

    
18
/**
19
 * Global switch to enable / disable syncing and check whether we are synching at the moment
20
 *
21
 * @return boolean
22
 *   TRUE if we need to run sync operations. FALSE during syncing so we don't have recursion.
23
 */
24
function i18n_sync($status = NULL) {
25
  static $current = TRUE;
26
  if (isset($status)) {
27
    $current = $status;
28
  }
29
  return $current;
30
}
31

    
32
/**
33
 * Implements hook_help().
34
 */
35
function i18n_sync_help($path, $arg) {
36
  switch ($path) {
37
    case 'admin/help#i18n_sync' :
38
      $output = '<p>' . t('This module synchronizes content taxonomy and fields accross translations:') . '</p>';
39
      $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>';
40
      $output .= '<ul>';
41
      $output .= '<li>' . t('All the node fields selected for synchronization will be set to the same value for all translations.') . '</li>';
42
      $output .= '<li>' . t('For multilingual vocabularies, the terms for all translations will be replaced by the translations of the original node terms.') . '</li>';
43
      $output .= '<li>' . t('For other vocabularies, the terms will be just copied over to all the translations.') . '</li>';
44
      $output .= '</ul>';
45
      $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>';
46
      $output .= '<p>' . t('To enable synchronization check content type options to select which fields to synchronize for each node type.') . '</p>';
47
      $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>';
48
      $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>';
49
      return $output;
50
  }
51
}
52

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

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

    
74
/**
75
 * Implements hook_form_FORM_ID_alter().
76
 */
77
function i18n_sync_form_node_admin_content_alter(&$form, &$form_state) {
78
  if (!empty($form['operation']) && $form['operation']['#value'] == 'delete') {
79
    $form['#submit'] = array_merge(array('i18n_sync_node_delete_submit'), $form['#submit']);
80
  }
81
}
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

    
103
    $form['i18n_sync']['i18n_sync_node_type'] = array(
104
      '#tree' => TRUE,
105
    );
106

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

    
138
/**
139
 * Check whether this node is to be synced
140
 */
141
function i18n_sync_node_check($node) {
142
  return translation_supported_type($node->type) && i18n_object_langcode($node) && i18n_sync();
143
}
144

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

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

    
176
/**
177
 * Implements hook_node_update().
178
 */
179
function i18n_sync_node_update($node) {
180
  // Let's go with field synchronization.
181
  if (i18n_sync_node_check($node) && !empty($node->tnid) && ($fields = i18n_sync_node_fields($node->type)) && ($translations = i18n_sync_node_get_translations($node, TRUE))) {
182
    module_load_include('node.inc', 'i18n_sync');
183
    i18n_sync_node_translation($node, $translations, $fields, 'update');
184
  }
185
}
186

    
187
/**
188
 * Implements hook_node_prepare().
189
 */
190
function i18n_sync_node_prepare($node) {
191
  // If creating a translation, copy over all the fields to be synchronized.
192
  if (empty($node->nid) && !empty($node->translation_source) && ($sync_fields = i18n_sync_node_fields($node->type))) {
193
    $source = $node->translation_source;
194
    $i18n_options = i18n_sync_node_options($node->type);
195
    // Copy over standard node fields. The rest are handled by field_attach_prepare_translation()
196
    foreach ($sync_fields as $field) {
197
      if (!empty($source->$field) && empty($i18n_options[$field]['field_name'])) {
198
        if ($field == 'uid') {
199
          $node->name = $source->name;
200
        }
201
        elseif ($field == 'created') {
202
          $node->date = format_date($source->created, 'custom', 'Y-m-d H:i:s O');
203
        }
204
        else {
205
          $node->$field = $source->$field;
206
        }
207
      }
208
    }
209
  }
210
}
211

    
212
/**
213
 * Returns list of fields to synchronize for a given content type.
214
 *
215
 * @param $type
216
 *   Node type.
217
 * @param $field
218
 *   Optional field name to check whether it is in the list
219
 */
220
function i18n_sync_node_fields($type, $field = NULL) {
221
  $fields = variable_get('i18n_sync_node_type_' . $type, array());
222
  return $field ? in_array($field, $fields) : $fields;
223
}
224

    
225
/**
226
 * Returns list of available fields for given content type.
227
 *
228
 * Fields can also be changed using hook_i18n_sync_fields_alter($fields, $type)
229
 *
230
 * @param $type
231
 *   Node type.
232
 */
233
function i18n_sync_node_options($type) {
234
  return i18n_sync_options('node', $type);
235
}
236

    
237
/**
238
 * Returns list of available fields for given entity / bundle.
239
 *
240
 * Fields can also be changed using hook_i18n_sync_options_alter($fields, $type)
241
 *
242
 * @param $entity_type
243
 *   Entity type.
244
 * @param
245
 */
246
function i18n_sync_options($entity_type, $bundle_name) {
247
  $cache = &drupal_static(__FUNCTION__);
248

    
249
  if (!isset($cache[$entity_type][$bundle_name])) {
250
    module_load_include('modules.inc', 'i18n_sync');
251
    $fields = module_invoke_all('i18n_sync_options', $entity_type, $bundle_name);
252
    // Give a chance to modules to change/remove/add their own fields
253
    drupal_alter('i18n_sync_options', $fields, $entity_type, $bundle_name);
254
    $cache[$entity_type][$bundle_name] = $fields;
255
  }
256

    
257
  return $cache[$entity_type][$bundle_name];
258
}
259

    
260
/**
261
 * Synchronize entity field translation
262
 */
263
function i18n_sync_field_translation_sync($entity_type, $bundle_name, $entity, $langcode, $source_entity, $source_langcode, $field_name) {
264
  $field = field_info_field($field_name);
265
  $instance = field_info_instance($entity_type, $field_name, $bundle_name);
266
  $source_lang = field_language($entity_type, $source_entity, $field_name);
267
  $translation_lang = field_is_translatable($entity_type, $field) ? $entity->language : LANGUAGE_NONE;
268
  if (isset($source_entity->{$field_name}[$source_lang])) {
269
    $items = $source_entity->{$field_name}[$source_lang];
270
    // Determine the function to synchronize this field. If none, just copy over.
271
    $type_info = field_info_field_types($field['type']);
272
    if (isset($type_info['i18n_sync_callback'])) {
273
      $function = $type_info['i18n_sync_callback'];
274
      $function($entity_type, $entity, $field, $instance, $langcode, $items, $source_entity, $source_langcode);
275
    }
276
    $entity->{$field_name}[$translation_lang] = $items;
277
  }
278
  else {
279
    // If source not set, unset translation too
280
    unset($entity->{$field_name}[$translation_lang]);
281
  }
282
}
283

    
284
/**
285
 * Sync a file or image field (i18n_sync_callback)
286
 *
287
 * - file-id's (fid) are synced
288
 * - order of fid's is synced
289
 * - description, alt, title is kept if already existing, copied otherwise
290
 */
291
function i18n_sync_field_file_sync($entity_type, $entity, $field, $instance, $langcode, &$items, $source_entity, $source_language) {
292
  $field_name = $instance['field_name'];
293
  // Build a copy of the existing files in the translation node
294
  // indexed by fid for easy retrieval in the copy loop below
295
  $existing_files = array();
296
  $field_language = field_language($entity_type, $entity, $field_name, $langcode);
297
  if (isset($entity->{$field_name}[$field_language])) {
298
    foreach ($entity->{$field_name}[$field_language] as $delta => $file) {
299
      $existing_files[$file['fid']] = $file;
300
    }
301
  }
302
  // Start creating the translated copy
303
  foreach ($items as $delta => &$file) {
304
    // keep alt, title, description if they already exist
305
    if (isset($existing_files[$file['fid']])) {
306
      foreach (array('title', 'description', 'alt') as $property) {
307
        if (!empty($existing_files[$file['fid']][$property])) {
308
          $file[$property] = $existing_files[$file['fid']][$property];
309
        }
310
      }
311
    }
312
  }
313
}