1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* Internationalization (i18n) package. Synchronization of translations
|
6 |
|
|
*
|
7 |
|
|
* Node synchronization.
|
8 |
|
|
*/
|
9 |
|
|
|
10 |
|
|
/**
|
11 |
|
|
* Synchronizes fields for node translation.
|
12 |
|
|
*
|
13 |
|
|
* There's some specific handling for known fields like:
|
14 |
|
|
* - files, for file attachments.
|
15 |
|
|
* - iid (CCK node attachments, translations for them will be handled too).
|
16 |
|
|
*
|
17 |
|
|
* All the rest of the fields will be just copied over.
|
18 |
|
|
* The 'revision' field will have the special effect of creating a revision too for the translation.
|
19 |
|
|
*
|
20 |
|
|
* @param $node
|
21 |
|
|
* Source node being edited.
|
22 |
|
|
* @param $translations
|
23 |
|
|
* Node translations to synchronize, just needs nid property.
|
24 |
|
|
* @param $fields
|
25 |
|
|
* List of fields to synchronize.
|
26 |
|
|
* @param $op
|
27 |
|
|
* Node operation (insert|update).
|
28 |
|
|
*/
|
29 |
|
|
function i18n_sync_node_translation($node, $translations, $field_names, $op) {
|
30 |
|
|
$total = count($translations);
|
31 |
|
|
$count = 0;
|
32 |
|
|
// Disable language selection and synchronization temporarily, enable it again later
|
33 |
|
|
$i18n_select = i18n_select(FALSE);
|
34 |
|
|
i18n_sync(FALSE);
|
35 |
|
|
foreach ($translations as $translation) {
|
36 |
|
|
// If translation is the same node, we cannot synchronize with itself
|
37 |
|
|
if ($node->nid == $translation->nid) {
|
38 |
|
|
$total--;
|
39 |
|
|
continue;
|
40 |
|
|
}
|
41 |
|
|
// Load full node, we need all data here.
|
42 |
|
|
$translation = node_load($translation->nid);
|
43 |
|
|
$i18n_options = i18n_sync_node_options($node->type);
|
44 |
|
|
// Invoke callback for each field, the default is just copy over
|
45 |
|
|
foreach ($field_names as $field) {
|
46 |
|
|
if (!empty($i18n_options[$field]['field_name'])) {
|
47 |
|
|
i18n_sync_field_translation_sync('node', $node->type, $translation, $translation->language, $node, $node->language, $i18n_options[$field]['field_name']);
|
48 |
|
|
}
|
49 |
|
|
elseif (isset($node->$field)) {
|
50 |
|
|
// Standard node field, just copy over.
|
51 |
|
|
$translation->$field = $node->$field;
|
52 |
|
|
}
|
53 |
|
|
}
|
54 |
|
|
// Give a chance to other modules for additional sync
|
55 |
|
|
module_invoke_all('i18n_sync_translation', 'node', $translation, $translation->language, $node, $node->language, $field_names);
|
56 |
|
|
node_save($translation);
|
57 |
|
|
$count++;
|
58 |
|
|
// Flush each entity from the load cache after processing, to
|
59 |
|
|
// avoid exceeding PHP memory limits. It should be safe to keep
|
60 |
|
|
// at least one, however; so we retain the final translation in
|
61 |
|
|
// the cache after saving it.
|
62 |
|
|
if ($count < $total) {
|
63 |
|
|
entity_get_controller('node')->resetCache(array($translation->nid));
|
64 |
|
|
}
|
65 |
|
|
}
|
66 |
|
|
i18n_sync(TRUE);
|
67 |
|
|
i18n_select($i18n_select);
|
68 |
|
|
drupal_set_message(format_plural($count, 'One node translation has been synchronized.', 'All @count node translations have been synchronized.'));
|
69 |
|
|
}
|
70 |
|
|
|
71 |
|
|
/**
|
72 |
|
|
* Node attachments (CCK) that may have translation.
|
73 |
|
|
*/
|
74 |
|
|
function i18n_sync_node_translation_attached_node(&$node, &$translation, $field) {
|
75 |
|
|
if ($attached = node_load($node->$field)) {
|
76 |
|
|
$translation->$field = i18n_sync_node_translation_reference_field($attached, $node->$field, $translation->language);
|
77 |
|
|
}
|
78 |
|
|
}
|
79 |
|
|
|
80 |
|
|
/**
|
81 |
|
|
* Translating a nodereference field (cck).
|
82 |
|
|
*/
|
83 |
|
|
function i18n_sync_node_translation_nodereference_field(&$node, &$translation, $field) {
|
84 |
|
|
$translated_references = array();
|
85 |
|
|
foreach ($node->$field as $reference) {
|
86 |
|
|
if ($reference_node = node_load($reference['nid'])) {
|
87 |
|
|
$translated_references[] = array(
|
88 |
|
|
'nid' => i18n_sync_node_translation_reference_field($reference_node, $reference['nid'], $translation->language)
|
89 |
|
|
);
|
90 |
|
|
}
|
91 |
|
|
}
|
92 |
|
|
$translation->$field = $translated_references;
|
93 |
|
|
}
|
94 |
|
|
|
95 |
|
|
/**
|
96 |
|
|
* Helper function to which translates reference field. We try to use translations for reference, otherwise fallback.
|
97 |
|
|
* Example:
|
98 |
|
|
* English A references English B and English C.
|
99 |
|
|
* English A and B are translated to German A and B, but English C is not.
|
100 |
|
|
* The syncronization from English A to German A would it German B and English C.
|
101 |
|
|
*/
|
102 |
|
|
function i18n_sync_node_translation_reference_field(&$reference_node, $default_value, $langcode) {
|
103 |
|
|
if (isset($reference_node->tnid) && translation_supported_type($reference_node->type)) {
|
104 |
|
|
// This content type has translations, find the one.
|
105 |
|
|
if (($reference_trans = translation_node_get_translations($reference_node->tnid)) && isset($reference_trans[$langcode])) {
|
106 |
|
|
return $reference_trans[$langcode]->nid;
|
107 |
|
|
}
|
108 |
|
|
else {
|
109 |
|
|
// No requested language found, just copy the field.
|
110 |
|
|
return $default_value;
|
111 |
|
|
}
|
112 |
|
|
}
|
113 |
|
|
else {
|
114 |
|
|
// Content type without language, just copy the field.
|
115 |
|
|
return $default_value;
|
116 |
|
|
}
|
117 |
|
|
}
|
118 |
|
|
|
119 |
|
|
/**
|
120 |
|
|
* Synchronize configurable field
|
121 |
|
|
*
|
122 |
|
|
* @param $field_info
|
123 |
|
|
* Field API field information.
|
124 |
|
|
*/
|
125 |
|
|
function i18n_sync_node_translation_default_field($node, $translation, $field, $field_info) {
|
126 |
|
|
switch ($field_info['field']['type']) {
|
127 |
|
|
case 'file':
|
128 |
|
|
case 'image':
|
129 |
|
|
i18n_sync_node_translation_file_field($node, $translation, $field);
|
130 |
|
|
break;
|
131 |
|
|
default:
|
132 |
|
|
// For fields that don't need special handling, just copy it over if defined.
|
133 |
|
|
// Field languages are completely unconsistent, for not to say broken
|
134 |
|
|
// both in Drupal core and entity translation. Let's hope this works.
|
135 |
|
|
$source_lang = field_language('node', $node, $field);
|
136 |
|
|
$translation_lang = field_language('node', $translation, $field);
|
137 |
|
|
if (isset($node->{$field}[$source_lang])) {
|
138 |
|
|
$translation->{$field}[$translation_lang] = $node->{$field}[$source_lang];
|
139 |
|
|
}
|
140 |
|
|
break;
|
141 |
|
|
}
|
142 |
|
|
}
|
143 |
|
|
|
144 |
|
|
/**
|
145 |
|
|
* Sync a file or image field:
|
146 |
|
|
* - file-id's (fid) are synced
|
147 |
|
|
* - order of fid's is synced
|
148 |
|
|
* - description, alt, title is kept if already existing, copied otherwise
|
149 |
|
|
*
|
150 |
|
|
* @param object $node the node whose changes are to be synced
|
151 |
|
|
* @param object $translation a node to which the changes need to be synced
|
152 |
|
|
* @param string $field field name
|
153 |
|
|
*/
|
154 |
|
|
function i18n_sync_node_translation_file_field($node, $translation, $field) {
|
155 |
|
|
if (isset($node->{$field}[$node->language])) {
|
156 |
|
|
// Build a copy of the existing files in the translation node
|
157 |
|
|
// indexed by fid for easy retrieval in the copy loop below
|
158 |
|
|
$existing_files = array();
|
159 |
|
|
if (isset($translation->{$field}[$translation->language])) {
|
160 |
|
|
foreach ($translation->{$field}[$translation->language] as $delta => $file) {
|
161 |
|
|
$existing_files[$file['fid']] = $file;
|
162 |
|
|
}
|
163 |
|
|
}
|
164 |
|
|
|
165 |
|
|
// Start creating the translated copy
|
166 |
|
|
$translated_files = $node->{$field}[$node->language];
|
167 |
|
|
foreach ($translated_files as $delta => &$file) {
|
168 |
|
|
// keep alt, title, description if they already exist
|
169 |
|
|
if (array_key_exists($file['fid'], $existing_files)) {
|
170 |
|
|
foreach (array('title', 'description', 'alt') as $property) {
|
171 |
|
|
if (!empty($existing_files[$file['fid']][$property])) {
|
172 |
|
|
$file[$property] = $existing_files[$file['fid']][$property];
|
173 |
|
|
}
|
174 |
|
|
}
|
175 |
|
|
}
|
176 |
|
|
}
|
177 |
|
|
$translation->{$field}[$translation->language] = $translated_files;
|
178 |
|
|
}
|
179 |
|
|
} |