function i18nsync_nodeapi in Internationalization 6
Same name and namespace in other branches
- 5.3 experimental/i18nsync.module \i18nsync_nodeapi()
- 5 experimental/i18nsync.module \i18nsync_nodeapi()
- 5.2 experimental/i18nsync.module \i18nsync_nodeapi()
Implementation of hook_nodeapi().
File
- i18nsync/
i18nsync.module, line 150 - Internationalization (i18n) package. Synchronization of translations
Code
function i18nsync_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
global $i18nsync;
// This variable will be true when a sync operation is in progress.
// Only for nodes that have language and belong to a translation set.
if (translation_supported_type($node->type) && !empty($node->language) && !$i18nsync) {
switch ($op) {
case 'load':
// Add instance count for cck fields so we can use the information later, see hook_file_references()
if (!empty($node->tnid) && ($sync_fields = i18nsync_node_fields($node->type)) && ($content_fields = _i18nsync_cck_fields($node->type))) {
if ($translations = _i18nsync_node_translations($node, TRUE)) {
$count = count($translations);
foreach ($sync_fields as $field) {
if (isset($content_fields[$field]) && !empty($node->{$field}) && is_array($node->{$field})) {
// The node field should be an array with one or more fields
// Reminder: Use brackets for $node->{$field}[$key] as $node->$field[$key] won't work
foreach (array_keys($node->{$field}) as $key) {
if (is_array($node->{$field}[$key])) {
$node->{$field}[$key]['i18nsync'] = $count;
}
}
}
}
}
}
break;
case 'prepare translation':
// We copy over all the fields to be synchronized.
if ($fields = i18nsync_node_fields($node->type)) {
i18nsync_prepare_translation($node, $node->translation_source, $fields);
}
break;
case 'insert':
// When creating a translation, there are some aditional steps, different from update
if (!empty($node->translation_source)) {
// Set tnid that is not set by translation module
$node->tnid = $node->translation_source->tnid ? $node->translation_source->tnid : $node->translation_source->nid;
// If we have files, we need to save the files that have been inherited
if (!empty($node->files) && i18nsync_node_fields($node->type, 'files')) {
foreach ($node->files as $fid => $file) {
$file = (object) $file;
if (empty($file->remove) && empty($file->new)) {
db_query("INSERT INTO {upload} (fid, nid, vid, list, description, weight) VALUES (%d, %d, %d, %d, '%s', %d)", $file->fid, $node->nid, $node->vid, $file->list, $file->description, $file->weight);
}
}
}
}
// Intentional no break.
case 'update':
// Let's go with field synchronization.
if (!empty($node->tnid) && ($fields = i18nsync_node_fields($node->type)) && ($translations = _i18nsync_node_translations($node, TRUE))) {
$i18nsync = TRUE;
$count = 0;
// If we have fields we need to reload them so we have the full data (fid, etc...)
if (!empty($node->files) && in_array('files', $fields)) {
$node->files = upload_load($node);
}
// Disable language selection temporarily, enable it again later
i18n_selection_mode('off');
foreach ($translations as $trnode) {
if ($node->nid != $trnode->nid) {
i18nsync_node_translation($node, $trnode, $fields, $op);
$count++;
}
}
i18n_selection_mode('reset');
$i18nsync = FALSE;
drupal_set_message(format_plural($count, 'One node translation has been synchronized.', 'All @count node translations have been synchronized.'));
}
break;
}
}
}