From dbc9dd8f6a72b8855f5cb829a2ac77431fc9a461 Mon Sep 17 00:00:00 2001
From: Kristiaan Van den Eynde <magentix@gmail.com>
Date: Fri, 4 Sep 2015 14:51:21 +0200
Subject: [PATCH] Issue #929402 by brockfanning,kristiaanvandeneynde, et al:
 Add support for migrate module.

---
 entity_translation.info          |  1 +
 entity_translation.migrate.inc   | 17 ++++++++
 includes/translation.migrate.inc | 86 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 104 insertions(+)
 create mode 100644 entity_translation.migrate.inc
 create mode 100644 includes/translation.migrate.inc

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.migrate.inc b/entity_translation.migrate.inc
new file mode 100644
index 0000000..21ae1ad
--- /dev/null
+++ b/entity_translation.migrate.inc
@@ -0,0 +1,17 @@
+<?php
+/**
+ * @file
+ * Implements a Migrate destination handler to automatically add translations
+ * to entities with translatable fields.
+ */
+
+/**
+ * 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..b385306
--- /dev/null
+++ b/includes/translation.migrate.inc
@@ -0,0 +1,86 @@
+<?php
+/**
+ * @file
+ * MigrateTranslationEntityHandler class for entity_translation.
+ */
+
+/**
+ * Destination handler implementing translatable fields.
+ */
+class MigrateTranslationEntityHandler extends MigrateDestinationHandler {
+  /**
+   * Registers all entites as handled by this class.
+   */
+  public function __construct() {
+    $this->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)) ? REQUEST_TIME : $entity->changed,
+            'created' => (empty($entity->created)) ? REQUEST_TIME : $entity->created,
+          ));
+        }
+      }
+    }
+  }
+}
-- 
2.0.1

