diff --git a/drupal/sites/all/modules/contrib/entity_translation/entity_translation.info b/drupal/sites/all/modules/contrib/entity_translation/entity_translation.info index c1cd0f8..8af4451 100644 --- a/drupal/sites/all/modules/contrib/entity_translation/entity_translation.info +++ b/drupal/sites/all/modules/contrib/entity_translation/entity_translation.info @@ -6,6 +6,7 @@ configure = admin/config/regional/entity_translation dependencies[] = locale files[] = includes/translation.handler.inc files[] = includes/translation.handler.node.inc +files[] = includes/translation.migrate.inc files[] = tests/entity_translation.test ; Information added by drupal.org packaging script on 2011-09-07 diff --git a/drupal/sites/all/modules/contrib/entity_translation/entity_translation.module b/drupal/sites/all/modules/contrib/entity_translation/entity_translation.module index fcf7908..a575899 100644 --- a/drupal/sites/all/modules/contrib/entity_translation/entity_translation.module +++ b/drupal/sites/all/modules/contrib/entity_translation/entity_translation.module @@ -545,3 +545,14 @@ function entity_translation_edit_form_info($form) { function entity_translation_access($entity_type, $translation) { return $translation['status'] || user_access('translate any entity') || user_access("translate $entity_type entities"); } + +/* + * 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/drupal/sites/all/modules/contrib/entity_translation/includes/translation.migrate.inc b/drupal/sites/all/modules/contrib/entity_translation/includes/translation.migrate.inc new file mode 100644 index 0000000..39b7236 --- /dev/null +++ b/drupal/sites/all/modules/contrib/entity_translation/includes/translation.migrate.inc @@ -0,0 +1,112 @@ +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(stdClass $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); + var_dump($translation_handler); + + // Load translations if necessary + if (!property_exists($entity, 'translations')) { + $entity->translations = $translation_handler->getTranslations(); + } + + // Content based translation + if (in_array($entity->type, variable_get('entity_translation_entity_types', array()))) { + var_dump('good types'); + // Translation needed? + if (!isset($entity->translations->data[$entity->language])) { + var_dump('translation needed'); + + $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->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; + } + } + } + } +}