Projet

Général

Profil

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

root / drupal7 / sites / all / modules / i18n / i18n_sync / i18n_sync.module @ 76df55b7

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 accross 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
    );
95

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

    
127
/**
128
 * Check whether this node is to be synced
129
 */
130
function i18n_sync_node_check($node) {
131
  return translation_supported_type($node->type) && i18n_object_langcode($node) && i18n_sync();
132
}
133

    
134
/**
135
 * Get node translations if any, excluding the node itself
136
 *
137
 * @param $reset
138
 *   Whether to reset the translation_node_get_translations cache.
139
 */
140
function i18n_sync_node_get_translations($node, $reset = FALSE) {
141
  if (!empty($node->tnid)) {
142
    if ($reset) {
143
      // Clear the static cache of translation_node_get_translations
144
      $translations_cache = &drupal_static('translation_node_get_translations', array());
145
      unset($translations_cache[$node->tnid]);
146
    }
147
    // Maybe translations are already here
148
    if ($translations = translation_node_get_translations($node->tnid)) {
149
      unset($translations[$node->language]);
150
      return $translations;
151
    }
152
  }
153
}
154

    
155
/**
156
 * Implements hook_node_insert().
157
 */
158
function i18n_sync_node_insert($node) {
159
  // When creating a translation, there are some aditional steps, different from update
160
  if (i18n_sync_node_check($node) && !empty($node->translation_source)) {
161
    i18n_sync_node_update($node);
162
  }
163
}
164

    
165
/**
166
 * Implements hook_node_update().
167
 */
168
function i18n_sync_node_update($node) {
169
  // Let's go with field synchronization.
170
  if (i18n_sync_node_check($node) && !empty($node->tnid) && ($fields = i18n_sync_node_fields($node->type)) && ($translations = i18n_sync_node_get_translations($node, TRUE))) {
171
    module_load_include('node.inc', 'i18n_sync');
172
    i18n_sync_node_translation($node, $translations, $fields, 'update');
173
  }
174
}
175

    
176
/**
177
 * Implements hook_node_prepare().
178
 */
179
function i18n_sync_node_prepare($node) {
180
  // If creating a translation, copy over all the fields to be synchronized.
181
  if (empty($node->nid) && !empty($node->translation_source) && ($sync_fields = i18n_sync_node_fields($node->type))) {
182
    $source = $node->translation_source;
183
    $i18n_options = i18n_sync_node_options($node->type);
184
    // Copy over standard node fields. The rest are handled by field_attach_prepare_translation()
185
    foreach ($sync_fields as $field) {
186
      if (!empty($source->$field) && empty($i18n_options[$field]['field_name'])) {
187
        if ($field == 'uid') {
188
          $node->name = $source->name;
189
        }
190
        elseif ($field == 'created') {
191
          $node->date = format_date($source->created, 'custom', 'Y-m-d H:i:s O');
192
        }
193
        else {
194
          $node->$field = $source->$field;
195
        }
196
      }
197
    }
198
  }
199
}
200

    
201
/**
202
 * Returns list of fields to synchronize for a given content type.
203
 *
204
 * @param $type
205
 *   Node type.
206
 * @param $field
207
 *   Optional field name to check whether it is in the list
208
 */
209
function i18n_sync_node_fields($type, $field = NULL) {
210
  $fields = variable_get('i18n_sync_node_type_' . $type, array());
211
  return $field ? in_array($field, $fields) : $fields;
212
}
213

    
214
/**
215
 * Returns list of available fields for given content type.
216
 *
217
 * Fields can also be changed using hook_i18n_sync_fields_alter($fields, $type)
218
 *
219
 * @param $type
220
 *   Node type.
221
 */
222
function i18n_sync_node_options($type) {
223
  return i18n_sync_options('node', $type);
224
}
225

    
226
/**
227
 * Returns list of available fields for given entity / bundle.
228
 *
229
 * Fields can also be changed using hook_i18n_sync_options_alter($fields, $type)
230
 *
231
 * @param $entity_type
232
 *   Entity type.
233
 * @param
234
 */
235
function i18n_sync_options($entity_type, $bundle_name) {
236
  $cache = &drupal_static(__FUNCTION__);
237

    
238
  if (!isset($cache[$entity_type][$bundle_name])) {
239
    module_load_include('modules.inc', 'i18n_sync');
240
    $fields = module_invoke_all('i18n_sync_options', $entity_type, $bundle_name);
241
    // Give a chance to modules to change/remove/add their own fields
242
    drupal_alter('i18n_sync_options', $fields, $entity_type, $bundle_name);
243
    $cache[$entity_type][$bundle_name] = $fields;
244
  }
245

    
246
  return $cache[$entity_type][$bundle_name];
247
}
248

    
249
/**
250
 * Synchronize entity field translation
251
 */
252
function i18n_sync_field_translation_sync($entity_type, $bundle_name, $entity, $langcode, $source_entity, $source_langcode, $field_name) {
253
  $field = field_info_field($field_name);
254
  $instance = field_info_instance($entity_type, $field_name, $bundle_name);
255
  $source_lang = field_language($entity_type, $source_entity, $field_name);
256
  $translation_lang = field_is_translatable($entity_type, $field) ? $entity->language : LANGUAGE_NONE;
257
  if (isset($source_entity->{$field_name}[$source_lang])) {
258
    $items = $source_entity->{$field_name}[$source_lang];
259
    // Determine the function to synchronize this field. If none, just copy over.
260
    $type_info = field_info_field_types($field['type']);
261
    if (isset($type_info['i18n_sync_callback'])) {
262
      $function = $type_info['i18n_sync_callback'];
263
      $function($entity_type, $entity, $field, $instance, $langcode, $items, $source_entity, $source_langcode);
264
    }
265
    $entity->{$field_name}[$translation_lang] = $items;
266
  }
267
  else {
268
    // If source not set, unset translation too
269
    unset($entity->{$field_name}[$translation_lang]);
270
  }
271
}
272

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