### Eclipse Workspace Patch 1.0 #P translation Index: includes/translation.migrate.inc =================================================================== RCS file: includes/translation.migrate.inc diff -N includes/translation.migrate.inc --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ includes/translation.migrate.inc 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,106 @@ +language field to the appropriate language code. + * + * @TODO Would be nice to move some parts of this into migrate itself + */ +class MigrateTranslationEntityHandler extends MigrateDestinationHandler { + + /** + * registers all entites as handled by this class + */ + public function __construct() { + $this->registerTypes(array('Entity')); + } + + /** + * Handles Entity Translations + * + * @TODO Add support for node based translations + * + * @param Migration $migration + * @param stdClass $entity + * @param stdClass $sourceRow + */ + public function prepare(Migration $migration, stdClass $entity, stdClass $source_row) { + $entity_type = $migration->getDestination()->getEntityType(); + + if (translation_enabled($entity_type) && property_exists($entity, 'nid') && $entity->nid) { + $translationHandler = translation_get_handler($entity_type, $entity); + + // Load translations if necessary + if (!property_exists($entity, 'translations')) { + $entity->translations = $translationHandler->getTranslations(); + } + + // Content based translation + if (translation_supported_type($entity->type)) { + // Translation needed? + if (!isset($entity->translations->data[$entity->language])) { + + $changed = (property_exists($entity, 'changed'))? $entity->changed: time(); + + // Add the new translation and store it + $translationHandler->setTranslation(array( + 'translate' => 0, + 'status' => $entity->status, + 'language' => $entity->language, + 'source' => $entity->translations->original, + 'uid' => $entity->uid, + 'created' => $entity->created, + 'changed' => $changed, + ) + ); + } + // Preserve original language setting + $entity->language = $entity->translations->original; + } + // Node based translation + else { + // Load old node to make sure it's the right id - mapping of migrate is not language sensitive + $current_node = node_load($entity->nid); + + // "Fix" mapping "issue" - should be solved withing migrate itself + if ($current_node->language != $entity->language) { + + // Check if there was a translation before - if not save the tnid + $tnid = $current_node->tnid; + if (!$tnid) { + $current_node->tnid = $current_node->nid; + node_save($current_node); + } + + // Load the translation of the node if there is one + $nodes = node_load_multiple(array(), array('tnid' => $current_node->tnid, 'language' => $entity->language)); + + // If there was a translation - map these information in the current entity + if (count($nodes)) { + $matching_node = reset($nodes); + // Change Id's to the matching node + $entity->nid = $matching_node->nid; + $entity->vid = $matching_node->vid; + $entity->translations = $matching_node->translations; + } + else { + // Tehere was no node with this language - create a new one + unset( + $entity->nid, + $entity->translations, + $entity->vid + ); + } + // Make sure the translation nodes are grouped properly + $entity->tnid = $current_node->tnid; + } + } + } + } +} Index: translation.info =================================================================== RCS file: /cvs/drupal/contributions/modules/translation/translation.info,v retrieving revision 1.3 diff -u -r1.3 translation.info --- translation.info 23 Sep 2010 11:58:28 -0000 1.3 +++ translation.info 2 Oct 2010 12:10:49 -0000 @@ -12,3 +12,4 @@ files[] = includes/translation.handler.inc files[] = includes/translation.handler.node.inc files[] = includes/translation.node.inc +files[] = includes/translation.migrate.inc