diff --git a/entity_translation.info b/entity_translation.info index e59a3ce..07f3c12 100644 --- a/entity_translation.info +++ b/entity_translation.info @@ -11,6 +11,7 @@ files[] = includes/translation.handler.comment.inc files[] = includes/translation.handler.node.inc files[] = includes/translation.handler.taxonomy_term.inc files[] = includes/translation.handler.user.inc +files[] = includes/translation.migrate.inc files[] = tests/entity_translation.test diff --git a/entity_translation.module b/entity_translation.module index 77abb30..0fa70c5 100644 --- a/entity_translation.module +++ b/entity_translation.module @@ -1984,3 +1984,14 @@ function path_entity_translation_delete($entity_type, $entity, $langcode) { $handler = entity_translation_get_handler($entity_type, $entity); path_delete(array('source' => $handler->getViewPath(), 'language' => $langcode)); } + +/* + * You must implement hook_migrate_api(), setting the API level to 2, for + * your migration classes to be recognized by the Migrate module. + */ +function entity_translation_migrate_api() { + $api = array( + 'api' => 2, + ); + return $api; +} diff --git a/includes/translation.migrate.inc b/includes/translation.migrate.inc new file mode 100644 index 0000000..d40a17b --- /dev/null +++ b/includes/translation.migrate.inc @@ -0,0 +1,110 @@ ++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($entity, stdClass $source_row) { + + $migration = Migration::currentMigration(); + $entity_type = $migration->getDestination()->getEntityType(); + + if (entity_translation_enabled($entity_type) && property_exists($entity, 'nid') && $entity->nid) { + $translation_handler = entity_translation_get_handler($entity_type, $entity); + + // Load translations if necessary + if (!property_exists($entity, 'translations')) { + $entity->translations = $translation_handler->getTranslations(); + } + + // Content based translation + if (entity_translation_node_supported_type($entity->type)) { + + if (!isset($entity->translations->data[$entity->language])) { + $changed = (property_exists($entity, 'changed'))? $entity->changed: time(); + + // Add the new translation and store it + $translation_handler->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->field_language = $entity->language; + $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; + } + } + } + } +}