Projet

Général

Profil

Paste
Télécharger (6,71 ko) Statistiques
| Branche: | Révision:

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

1
<?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
    entity_get_controller('node')->resetCache(array($translation->nid));
43
    $translation = node_load($translation->nid);
44
    $i18n_options = i18n_sync_node_options($node->type);
45
    // Invoke callback for each field, the default is just copy over
46
    foreach ($field_names as $field) {
47
      if (!empty($i18n_options[$field]['field_name'])) {
48
        i18n_sync_field_translation_sync('node', $node->type, $translation, $translation->language, $node, $node->language, $i18n_options[$field]['field_name']);
49
      }
50
      elseif (isset($node->$field)) {
51
        // Standard node field, just copy over.
52
        $translation->$field = $node->$field;
53
      }
54
    }
55
    // Give a chance to other modules for additional sync
56
    module_invoke_all('i18n_sync_translation', 'node', $translation, $translation->language, $node, $node->language, $field_names);
57
    node_save($translation);
58
    $count++;
59
    // Flush each entity from the load cache after processing, to
60
    // avoid exceeding PHP memory limits. It should be safe to keep
61
    // at least one, however; so we retain the final translation in
62
    // the cache after saving it.
63
    if ($count < $total) {
64
      entity_get_controller('node')->resetCache(array($translation->nid));
65
    }
66
  }
67
  i18n_sync(TRUE);
68
  i18n_select($i18n_select);
69
  drupal_set_message(format_plural($count, 'One node translation has been synchronized.', 'All @count node translations have been synchronized.'));
70
}
71

    
72
/**
73
 * Node attachments (CCK) that may have translation.
74
 */
75
function i18n_sync_node_translation_attached_node(&$node, &$translation, $field) {
76
  if ($attached = node_load($node->$field)) {
77
    $translation->$field = i18n_sync_node_translation_reference_field($attached, $node->$field, $translation->language);
78
  }
79
}
80

    
81
/**
82
 * Translating a nodereference field (cck).
83
 */
84
function i18n_sync_node_translation_nodereference_field(&$node, &$translation, $field) {
85
  $translated_references = array();
86
  foreach ($node->$field as $reference) {
87
    if ($reference_node = node_load($reference['nid'])) {
88
      $translated_references[] = array(
89
        'nid' => i18n_sync_node_translation_reference_field($reference_node, $reference['nid'], $translation->language)
90
      );
91
    }
92
  }
93
  $translation->$field = $translated_references;
94
}
95

    
96
/**
97
 * Helper function to which translates reference field. We try to use translations for reference, otherwise fallback.
98
 * Example:
99
 *   English A references English B and English C.
100
 *   English A and B are translated to German A and B, but English C is not.
101
 *   The syncronization from English A to German A would it German B and English C.
102
 */
103
function i18n_sync_node_translation_reference_field(&$reference_node, $default_value, $langcode) {
104
  if (isset($reference_node->tnid) && translation_supported_type($reference_node->type)) {
105
    // This content type has translations, find the one.
106
    if (($reference_trans = translation_node_get_translations($reference_node->tnid)) && isset($reference_trans[$langcode])) {
107
      return $reference_trans[$langcode]->nid;
108
    }
109
    else {
110
      // No requested language found, just copy the field.
111
      return $default_value;
112
    }
113
  }
114
  else {
115
    // Content type without language, just copy the field.
116
    return $default_value;
117
  }
118
}
119

    
120
/**
121
 * Synchronize configurable field
122
 *
123
 * @param $field_info
124
 *   Field API field information.
125
 */
126
function i18n_sync_node_translation_default_field($node, $translation, $field, $field_info) {
127
  switch ($field_info['field']['type']) {
128
    case 'file':
129
    case 'image':
130
      i18n_sync_node_translation_file_field($node, $translation, $field);
131
      break;
132
    default:
133
      // For fields that don't need special handling, just copy it over if defined.
134
      // Field languages are completely unconsistent, for not to say broken
135
      // both in Drupal core and entity translation. Let's hope this works.
136
      $source_lang = field_language('node', $node, $field);
137
      $translation_lang = field_language('node', $translation, $field);
138
      if (isset($node->{$field}[$source_lang])) {
139
        $translation->{$field}[$translation_lang] = $node->{$field}[$source_lang];
140
      }
141
      break;
142
  }
143
}
144

    
145
/**
146
 * Sync a file or image field:
147
 * - file-id's (fid) are synced
148
 * - order of fid's is synced
149
 * - description, alt, title is kept if already existing, copied otherwise
150
 *
151
 * @param object $node the node whose changes are to be synced
152
 * @param object $translation a node to which the changes need to be synced
153
 * @param string $field field name
154
 */
155
function i18n_sync_node_translation_file_field($node, $translation, $field) {
156
  if (isset($node->{$field}[$node->language])) {
157
    // Build a copy of the existing files in the translation node
158
    // indexed by fid for easy retrieval in the copy loop below
159
    $existing_files = array();
160
    if (isset($translation->{$field}[$translation->language])) {
161
      foreach ($translation->{$field}[$translation->language] as $delta => $file) {
162
        $existing_files[$file['fid']] = $file;
163
      }
164
    }
165

    
166
    // Start creating the translated copy
167
    $translated_files = $node->{$field}[$node->language];
168
    foreach ($translated_files as $delta => &$file) {
169
      // keep alt, title, description if they already exist
170
      if (array_key_exists($file['fid'], $existing_files)) {
171
        foreach (array('title', 'description', 'alt') as $property) {
172
          if (!empty($existing_files[$file['fid']][$property])) {
173
            $file[$property] = $existing_files[$file['fid']][$property];
174
          }
175
        }
176
      }
177
    }
178
    $translation->{$field}[$translation->language] = $translated_files;
179
  }
180
}