1 |
85ad3d82
|
Assos Assos
|
<?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_delete_confirm_alter(&$form, &$form_state) {
|
87 |
|
|
// Intercept form submission so we can handle uploads, replace callback
|
88 |
|
|
$form['#submit'] = array_merge(array('i18n_sync_node_delete_submit'), $form['#submit']);
|
89 |
|
|
}
|
90 |
|
|
|
91 |
|
|
|
92 |
|
|
|
93 |
|
|
/**
|
94 |
|
|
* Implements hook_form_FORM_ID_alter().
|
95 |
|
|
*/
|
96 |
|
|
function i18n_sync_form_node_type_form_alter(&$form, &$form_state) {
|
97 |
|
|
if (isset($form['type'])) {
|
98 |
|
|
$type = $form['#node_type']->type;
|
99 |
|
|
$disabled = !translation_supported_type($type);
|
100 |
|
|
$form['i18n_sync'] = array(
|
101 |
|
|
'#type' => 'fieldset',
|
102 |
|
|
'#title' => t('Synchronize translations'),
|
103 |
|
|
'#collapsible' => TRUE,
|
104 |
|
|
'#collapsed' => TRUE,
|
105 |
|
|
'#group' => 'additional_settings',
|
106 |
|
|
'#attributes' => array(
|
107 |
|
|
'class' => array('i18n-node-type-settings-form'),
|
108 |
|
|
),
|
109 |
|
|
'#description' => t('Select which fields to synchronize for all translations of this content type.'),
|
110 |
|
|
'#disabled' => $disabled,
|
111 |
|
|
);
|
112 |
|
|
|
113 |
|
|
$form['i18n_sync']['i18n_sync_node_type'] = array(
|
114 |
|
|
'#tree' => TRUE,
|
115 |
|
|
);
|
116 |
|
|
|
117 |
|
|
// Each set provides title and options. We build a big checkboxes control for it to be
|
118 |
|
|
// saved as an array.
|
119 |
|
|
$current = i18n_sync_node_fields($type);
|
120 |
|
|
// Group options, group fields by type.
|
121 |
|
|
$groups = array(
|
122 |
|
|
'node' => t('Standard node fields'),
|
123 |
|
|
'fields' => t('Configurable fields'),
|
124 |
|
|
);
|
125 |
|
|
$fields = array();
|
126 |
|
|
foreach (i18n_sync_node_options($type) as $field => $info) {
|
127 |
|
|
$group = isset($info['group']) && isset($groups[$info['group']]) ? $info['group'] : 'node';
|
128 |
|
|
$fields[$group][$field] = $info;
|
129 |
|
|
}
|
130 |
|
|
foreach ($fields as $group => $group_fields) {
|
131 |
|
|
$form['i18n_sync']['i18n_sync_node_type']['i18n_sync_group_' . $group] = array(
|
132 |
|
|
'#prefix' => '<strong>', '#suffix' => '</strong>',
|
133 |
|
|
'#markup' => $groups[$group],
|
134 |
|
|
);
|
135 |
|
|
foreach ($group_fields as $field => $info) {
|
136 |
|
|
$form['i18n_sync']['i18n_sync_node_type'][$field] = array(
|
137 |
|
|
'#title' => $info['title'],
|
138 |
|
|
'#type' => 'checkbox',
|
139 |
|
|
'#default_value' => in_array($field, $current),
|
140 |
|
|
'#disabled' => $disabled,
|
141 |
|
|
'#description' => isset($info['description']) ? $info['description'] : '',
|
142 |
|
|
);
|
143 |
|
|
}
|
144 |
|
|
}
|
145 |
|
|
}
|
146 |
|
|
}
|
147 |
|
|
|
148 |
|
|
/**
|
149 |
|
|
* Submit callback for
|
150 |
|
|
* - node delete confirm
|
151 |
|
|
* - node multiple delete confirm
|
152 |
|
|
*/
|
153 |
|
|
function i18n_sync_node_delete_submit($form, $form_state) {
|
154 |
|
|
if ($form_state['values']['confirm']) {
|
155 |
|
|
if (!empty($form_state['values']['nid'])) {
|
156 |
|
|
// Single node
|
157 |
|
|
i18n_sync_node_delete_prepare($form_state['values']['nid']);
|
158 |
|
|
}
|
159 |
|
|
elseif (!empty($form_state['values']['nodes'])) {
|
160 |
|
|
// Multiple nodes
|
161 |
|
|
foreach ($form_state['values']['nodes'] as $nid => $value) {
|
162 |
|
|
i18n_sync_node_delete_prepare($nid);
|
163 |
|
|
}
|
164 |
|
|
}
|
165 |
|
|
}
|
166 |
|
|
// Then it will go through normal form submission
|
167 |
|
|
}
|
168 |
|
|
|
169 |
|
|
/**
|
170 |
|
|
* Prepare node for deletion, work out synchronization issues
|
171 |
|
|
*/
|
172 |
|
|
function i18n_sync_node_delete_prepare($nid) {
|
173 |
|
|
$node = node_load($nid);
|
174 |
|
|
// Delete file associations when files are shared with existing translations
|
175 |
|
|
// so they are not removed by upload module
|
176 |
|
|
if (!empty($node->tnid) && module_exists('upload')) {
|
177 |
|
|
$result = db_query('SELECT u.* FROM {upload} u WHERE u.nid = %d AND u.fid IN (SELECT t.fid FROM {upload} t WHERE t.fid = u.fid AND t.nid <> u.nid)', $nid);
|
178 |
|
|
while ($up = db_fetch_object($result)) {
|
179 |
|
|
db_query("DELETE FROM {upload} WHERE fid = %d AND vid = %d", $up->fid, $up->vid);
|
180 |
|
|
}
|
181 |
|
|
}
|
182 |
|
|
}
|
183 |
|
|
|
184 |
|
|
/**
|
185 |
|
|
* Check whether this node is to be synced
|
186 |
|
|
*/
|
187 |
|
|
function i18n_sync_node_check($node) {
|
188 |
|
|
return translation_supported_type($node->type) && i18n_object_langcode($node) && i18n_sync();
|
189 |
|
|
}
|
190 |
|
|
|
191 |
|
|
/**
|
192 |
|
|
* Get node translations if any, excluding the node itself
|
193 |
|
|
*
|
194 |
|
|
* @param $reset
|
195 |
|
|
* Whether to reset the translation_node_get_translations cache.
|
196 |
|
|
*/
|
197 |
|
|
function i18n_sync_node_get_translations($node, $reset = FALSE) {
|
198 |
|
|
if (!empty($node->tnid)) {
|
199 |
|
|
if ($reset) {
|
200 |
|
|
// Clear the static cache of translation_node_get_translations
|
201 |
|
|
$translations_cache = &drupal_static('translation_node_get_translations', array());
|
202 |
|
|
unset($translations_cache[$node->tnid]);
|
203 |
|
|
}
|
204 |
|
|
// Maybe translations are already here
|
205 |
|
|
if ($translations = translation_node_get_translations($node->tnid)) {
|
206 |
|
|
unset($translations[$node->language]);
|
207 |
|
|
return $translations;
|
208 |
|
|
}
|
209 |
|
|
}
|
210 |
|
|
}
|
211 |
|
|
|
212 |
|
|
/**
|
213 |
|
|
* Implements hook_node_insert().
|
214 |
|
|
*/
|
215 |
|
|
function i18n_sync_node_insert($node) {
|
216 |
|
|
// When creating a translation, there are some aditional steps, different from update
|
217 |
|
|
if (i18n_sync_node_check($node) && !empty($node->translation_source)) {
|
218 |
|
|
i18n_sync_node_update($node);
|
219 |
|
|
}
|
220 |
|
|
}
|
221 |
|
|
|
222 |
|
|
/**
|
223 |
|
|
* Implements hook_node_update().
|
224 |
|
|
*/
|
225 |
|
|
function i18n_sync_node_update($node) {
|
226 |
|
|
// Let's go with field synchronization.
|
227 |
|
|
if (i18n_sync_node_check($node) && !empty($node->tnid) && ($fields = i18n_sync_node_fields($node->type)) && ($translations = i18n_sync_node_get_translations($node, TRUE))) {
|
228 |
|
|
module_load_include('node.inc', 'i18n_sync');
|
229 |
|
|
i18n_sync_node_translation($node, $translations, $fields, 'update');
|
230 |
|
|
}
|
231 |
|
|
}
|
232 |
|
|
|
233 |
|
|
/**
|
234 |
|
|
* Implements hook_node_prepare().
|
235 |
|
|
*/
|
236 |
|
|
function i18n_sync_node_prepare($node) {
|
237 |
|
|
// If creating a translation, copy over all the fields to be synchronized.
|
238 |
|
|
if (empty($node->nid) && !empty($node->translation_source) && ($sync_fields = i18n_sync_node_fields($node->type))) {
|
239 |
|
|
$source = $node->translation_source;
|
240 |
|
|
$i18n_options = i18n_sync_node_options($node->type);
|
241 |
|
|
// Copy over standard node fields. The rest are handled by field_attach_prepare_translation()
|
242 |
|
|
foreach ($sync_fields as $field) {
|
243 |
|
|
if (!empty($source->$field) && empty($i18n_options[$field]['field_name'])) {
|
244 |
|
|
if ($field == 'uid') {
|
245 |
|
|
$node->name = $source->name;
|
246 |
|
|
}
|
247 |
|
|
elseif ($field == 'created') {
|
248 |
|
|
$node->date = format_date($source->created, 'custom', 'Y-m-d H:i:s O');
|
249 |
|
|
}
|
250 |
|
|
else {
|
251 |
|
|
$node->$field = $source->$field;
|
252 |
|
|
}
|
253 |
|
|
}
|
254 |
|
|
}
|
255 |
|
|
}
|
256 |
|
|
}
|
257 |
|
|
|
258 |
|
|
/**
|
259 |
|
|
* Returns list of fields to synchronize for a given content type.
|
260 |
|
|
*
|
261 |
|
|
* @param $type
|
262 |
|
|
* Node type.
|
263 |
|
|
* @param $field
|
264 |
|
|
* Optional field name to check whether it is in the list
|
265 |
|
|
*/
|
266 |
|
|
function i18n_sync_node_fields($type, $field = NULL) {
|
267 |
|
|
$fields = variable_get('i18n_sync_node_type_' . $type, array());
|
268 |
|
|
return $field ? in_array($field, $fields) : $fields;
|
269 |
|
|
}
|
270 |
|
|
|
271 |
|
|
/**
|
272 |
|
|
* Returns list of available fields for given content type.
|
273 |
|
|
*
|
274 |
|
|
* Fields can also be changed using hook_i18n_sync_fields_alter($fields, $type)
|
275 |
|
|
*
|
276 |
|
|
* @param $type
|
277 |
|
|
* Node type.
|
278 |
|
|
*/
|
279 |
|
|
function i18n_sync_node_options($type) {
|
280 |
|
|
return i18n_sync_options('node', $type);
|
281 |
|
|
}
|
282 |
|
|
|
283 |
|
|
/**
|
284 |
|
|
* Returns list of available fields for given entity / bundle.
|
285 |
|
|
*
|
286 |
|
|
* Fields can also be changed using hook_i18n_sync_options_alter($fields, $type)
|
287 |
|
|
*
|
288 |
|
|
* @param $entity_type
|
289 |
|
|
* Entity type.
|
290 |
|
|
* @param
|
291 |
|
|
*/
|
292 |
|
|
function i18n_sync_options($entity_type, $bundle_name) {
|
293 |
|
|
$cache = &drupal_static(__FUNCTION__);
|
294 |
|
|
|
295 |
|
|
if (!isset($cache[$entity_type][$bundle_name])) {
|
296 |
|
|
module_load_include('modules.inc', 'i18n_sync');
|
297 |
|
|
$fields = module_invoke_all('i18n_sync_options', $entity_type, $bundle_name);
|
298 |
|
|
// Give a chance to modules to change/remove/add their own fields
|
299 |
|
|
drupal_alter('i18n_sync_options', $fields, $entity_type, $bundle_name);
|
300 |
|
|
$cache[$entity_type][$bundle_name] = $fields;
|
301 |
|
|
}
|
302 |
|
|
|
303 |
|
|
return $cache[$entity_type][$bundle_name];
|
304 |
|
|
}
|
305 |
|
|
|
306 |
|
|
/**
|
307 |
|
|
* Synchronize entity field translation
|
308 |
|
|
*/
|
309 |
|
|
function i18n_sync_field_translation_sync($entity_type, $bundle_name, $entity, $langcode, $source_entity, $source_langcode, $field_name) {
|
310 |
|
|
$field = field_info_field($field_name);
|
311 |
|
|
$instance = field_info_instance($entity_type, $field_name, $bundle_name);
|
312 |
|
|
$source_lang = field_language($entity_type, $source_entity, $field_name);
|
313 |
|
|
$translation_lang = field_is_translatable($entity_type, $field) ? $entity->language : LANGUAGE_NONE;
|
314 |
|
|
if (isset($source_entity->{$field_name}[$source_lang])) {
|
315 |
|
|
$items = $source_entity->{$field_name}[$source_lang];
|
316 |
|
|
// Determine the function to synchronize this field. If none, just copy over.
|
317 |
|
|
$type_info = field_info_field_types($field['type']);
|
318 |
|
|
if (isset($type_info['i18n_sync_callback'])) {
|
319 |
|
|
$function = $type_info['i18n_sync_callback'];
|
320 |
|
|
$function($entity_type, $entity, $field, $instance, $langcode, $items, $source_entity, $source_langcode);
|
321 |
|
|
}
|
322 |
|
|
$entity->{$field_name}[$translation_lang] = $items;
|
323 |
|
|
}
|
324 |
|
|
else {
|
325 |
|
|
// If source not set, unset translation too
|
326 |
|
|
unset($entity->{$field_name}[$translation_lang]);
|
327 |
|
|
}
|
328 |
|
|
}
|
329 |
|
|
|
330 |
|
|
/**
|
331 |
|
|
* Sync a file or image field (i18n_sync_callback)
|
332 |
|
|
*
|
333 |
|
|
* - file-id's (fid) are synced
|
334 |
|
|
* - order of fid's is synced
|
335 |
|
|
* - description, alt, title is kept if already existing, copied otherwise
|
336 |
|
|
*/
|
337 |
|
|
function i18n_sync_field_file_sync($entity_type, $entity, $field, $instance, $langcode, &$items, $source_entity, $source_language) {
|
338 |
|
|
$field_name = $instance['field_name'];
|
339 |
|
|
// Build a copy of the existing files in the translation node
|
340 |
|
|
// indexed by fid for easy retrieval in the copy loop below
|
341 |
|
|
$existing_files = array();
|
342 |
|
|
$field_language = field_language($entity_type, $entity, $field_name, $langcode);
|
343 |
|
|
if (isset($entity->{$field_name}[$field_language])) {
|
344 |
|
|
foreach ($entity->{$field_name}[$field_language] as $delta => $file) {
|
345 |
|
|
$existing_files[$file['fid']] = $file;
|
346 |
|
|
}
|
347 |
|
|
}
|
348 |
|
|
// Start creating the translated copy
|
349 |
|
|
foreach ($items as $delta => &$file) {
|
350 |
|
|
// keep alt, title, description if they already exist
|
351 |
|
|
if (isset($existing_files[$file['fid']])) {
|
352 |
|
|
foreach (array('title', 'description', 'alt') as $property) {
|
353 |
|
|
if (!empty($existing_files[$file['fid']][$property])) {
|
354 |
|
|
$file[$property] = $existing_files[$file['fid']][$property];
|
355 |
|
|
}
|
356 |
|
|
}
|
357 |
|
|
}
|
358 |
|
|
}
|
359 |
|
|
} |