diff --git a/entity_translation.info b/entity_translation.info index 56a51b3..6d83e19 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 dd728f5..e7b0430 100644 --- a/entity_translation.module +++ b/entity_translation.module @@ -2001,3 +2001,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)); } + +/** + * Implements hook_migrate_api(). + */ +function entity_translation_migrate_api() { + $api = array( + 'api' => 2, + 'destination handlers' => array('MigrateTranslationEntityHandler'), + ); + return $api; +} diff --git a/includes/translation.migrate.inc b/includes/translation.migrate.inc new file mode 100644 index 0000000..ceee43d --- /dev/null +++ b/includes/translation.migrate.inc @@ -0,0 +1,96 @@ +registerTypes(array('entity')); + } + + /** + * Handles Entity Translations. + * + * @param stdClass $entity + * @param stdClass $sourceRow + */ + public function prepare($entity, stdClass $row) { + + $migration = Migration::currentMigration(); + $entity_type = $migration->getDestination()->getEntityType(); + + // Only continue if the entity type + bundle combination is enabled for + // entity_translation. + if (entity_translation_enabled($entity_type, $entity)) { + + // Get the entity_translation handler and load any existing translations. + $handler = entity_translation_get_handler($entity_type, $entity); + $handler->loadTranslations(); + $original_language = $handler->getLanguage(); + + // Get the bundle of the entity. + list(, , $bundle) = entity_extract_ids($entity_type, $entity); + + // We need to get all of the possible translations to create. So we look + // for any translatable fields. + $translatable_fields = array(); + foreach (field_info_instances($entity_type, $bundle) as $instance) { + + $field_name = $instance['field_name']; + $field = field_info_field($field_name); + + if ($field['translatable']) { + + $translatable_fields[] = $field_name; + } + } + + // In those fields we look for translated values to build out an array + // of languages we need to set translations for. + $translate_languages = array(); + foreach ($translatable_fields as $translatable_field) { + + if (!empty($entity->{$translatable_field})) { + + $field_languages = array_keys($entity->{$translatable_field}); + foreach ($field_languages as $field_language) { + + $translate_languages[$field_language] = $field_language; + } + } + } + + // Remove the LANGUAGE_NONE results. + unset($translate_languages[LANGUAGE_NONE]); + + // Remove the original language. + unset($translate_languages[$original_language]); + + // Anything we're left with is a translation that should be set. + foreach ($translate_languages as $translate_language) { + + if (!isset($entity->translations->data[$translate_language])) { + + // Add the new translation and store it. + $handler->setTranslation(array( + 'translate' => 0, + 'status' => 1, + 'language' => $translate_language, + 'source' => $original_language, + 'uid' => (empty($entity->uid)) ? 0 : $entity->uid, + 'changed' => (empty($entity->changed)) ? time() : $entity->changed, + 'created' => (empty($entity->created)) ? time() : $entity->created, + )); + } + } + } + } +} \ No newline at end of file