Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
// $Id$
/**
* @file
* Internationalization (i18n) package. Synchronization of translations
*
* Node synchronization.
*/
/**
* Synchronizes fields for node translation.
*
* There's some specific handling for known fields like:
* - files, for file attachments.
* - iid (CCK node attachments, translations for them will be handled too).
*
* All the rest of the fields will be just copied over.
* The 'revision' field will have the special effect of creating a revision too for the translation.
*
* @param $node
* Source node being edited.
* @param $translations
* Node translations to synchronize, just needs nid property.
* @param $fields
* List of fields to synchronize.
* @param $op
* Node operation (insert|update).
*/
function i18n_sync_node_translation($node, $translations, $fields, $op) {
$count = 0;
// Disable language selection and synchronization temporarily, enable it again later
i18n_select(FALSE);
i18n_sync(FALSE);
foreach ($translations as $translation) {
// If translation is the same node, we cannot synchronize with itself
if ($node->nid == $translation->nid) {
continue;
}
// Load full node, we need all data here.
$translation = node_load($translation->nid, NULL, TRUE);
$field_info = i18n_sync_node_options($node->type);
// Invoke callback for each field, the default is just copy over
foreach ($fields as $field) {
if (!empty($field_info[$field]['callback'])) {
call_user_func($field_info[$field]['callback'], $node, $translation, $field);
}
elseif (!empty($field_info[$field]['field'])) {
i18n_sync_node_translation_field_field($node, $translation, $field, $field_info[$field]['field']);
}
}
// Give a chance to other modules for additional sync
module_invoke_all('i18n_sync_node_translation', $node, $translation, $fields);
node_save($translation);
$count++;
}
i18n_sync(TRUE);
i18n_select(TRUE);
drupal_set_message(format_plural($count, 'One node translation has been synchronized.', 'All @count node translations have been synchronized.'));
}
/**
* Synchronize default field
*/
function i18n_sync_node_translation_default_field($node, $translation, $field) {
switch ($field) {
case 'parent': // Book outlines, translating parent page if exists.
case 'iid': // Attached image nodes.
i18n_sync_node_translation_attached_node($node, $translation, $field);
break;
case 'files':
// Sync existing attached files. This should work for images too
foreach ((array)$node->files as $fid => $file) {
if (isset($translation->files[$fid])) {
// Just update list and weight properties, description can be different
$translation->files[$fid]->list = $file->list;
$translation->files[$fid]->weight = $file->weight;
}
else {
// New file. Clone so we can set the new property just for this translation
$translation->files[$fid] = clone $file;
$translation->files[$fid]->new = TRUE;
}
}
// Drop removed files.
foreach ((array)$translation->files as $fid => $file) {
if (!isset($node->files[$fid])) {
$translation->files[$fid]->remove = TRUE;
}
}
break;
default:
// For fields that don't need special handling.
if (isset($node->$field)) {
$translation->$field = $node->$field;
}
}
}
/**
* Synchronize taxonomy.
*
* Translate translatable terms, just copy over the rest.
*/
function i18n_sync_node_translation_taxonomy(&$node, &$source) {
if (module_exists('i18n_taxonomy') && is_array($source->taxonomy)) {
// Load clean source node taxonomy so we don't need to handle weird form input
if (!isset($source->i18n_taxonomy)) {
$source->i18n_taxonomy = i18n_taxonomy_node_get_terms($source);
}
$node->taxonomy = i18n_taxonomy_translate_terms($source->i18n_taxonomy, $node->language, FALSE);
}
else {
// If not multilingual taxonomy enabled, just copy over.
$node->taxonomy = $source->taxonomy;
}
}
/**
* Node attachments (CCK) that may have translation.
*/
function i18n_sync_node_translation_attached_node(&$node, &$translation, $field) {
if ($attached = node_load($node->$field)) {
$translation->$field = i18n_sync_node_translation_reference_field($attached, $node->$field, $translation->language);
}
}
/**
* Translating a nodereference field (cck).
*/
function i18n_sync_node_translation_nodereference_field(&$node, &$translation, $field) {
$translated_references = array();
foreach ($node->$field as $reference) {
if ($reference_node = node_load($reference['nid'])) {
$translated_references[] = array(
'nid' => i18n_sync_node_translation_reference_field($reference_node, $reference['nid'], $translation->language)
);
}
}
$translation->$field = $translated_references;
}
/**
* Translating an filefield (cck).
*/
function i18n_sync_node_translation_filefield_field(&$node, &$translation, $field) {
if (is_array($node->$field)) {
$translated_images = array();
foreach ($node->$field as $file) {
$found = false;
// Try to find existing translations of the filefield items and reference them.
foreach ($translation->$field as $translation_image) {
if ($file['fid'] == $translation_image['fid']) {
$translated_images[] = $translation_image;
$found = true;
}
}
// If there was no translation found for the filefield item, just copy it.
if (!$found) {
$translated_images[] = $file;
}
}
$translation->$field = $translated_images;
}
}
/**
* Helper function to which translates reference field. We try to use translations for reference, otherwise fallback.
* Example:
* English A references English B and English C.
* English A and B are translated to German A and B, but English C is not.
* The syncronization from English A to German A would it German B and English C.
*/
function i18n_sync_node_translation_reference_field(&$reference_node, $default_value, $langcode) {
if (isset($reference_node->tnid) && translation_supported_type($reference_node->type)) {
// This content type has translations, find the one.
if (($reference_trans = translation_node_get_translations($reference_node->tnid)) && isset($reference_trans[$langcode])) {
return $reference_trans[$langcode]->nid;
}
else {
// No requested language found, just copy the field.
return $default_value;
}
}
else {
// Content type without language, just copy the field.
return $default_value;
}
}
/**
* Synchronize configurable field
*
* @param $field_info
* Field API field information.
*/
function i18n_sync_node_translation_field_field($node, $translation, $field, $field_info) {
switch ($field_info['type']) {
case 'taxonomy_term_reference':
// Do nothing it has already been syncd.
// i18n_sync_node_translation_taxonomy($translation, $node);
break;
default:
// For fields that don't need special handling, just copy it over if defined.
if (isset($node->$field)) {
$translation->$field = $node->$field;
}
}
}