From 47357c304235c3b980217ec225c8f762a14677f2 Mon Sep 17 00:00:00 2001
From: Filip Engberg <filip@itiden.se>
Date: Tue, 15 Mar 2016 09:51:49 +0100
Subject: [PATCH] Applying patch #21 from issue 2494959

---
 .../Field/FieldWidget/InlineEntityFormBase.php     | 11 +++++++++++
 .../Field/FieldWidget/InlineEntityFormComplex.php  | 23 +++++++++++++++++++---
 .../Field/FieldWidget/InlineEntityFormSimple.php   | 15 ++++++++++++++
 3 files changed, 46 insertions(+), 3 deletions(-)

diff --git a/src/Plugin/Field/FieldWidget/InlineEntityFormBase.php b/src/Plugin/Field/FieldWidget/InlineEntityFormBase.php
index 2e875a8..970e9e2 100644
--- a/src/Plugin/Field/FieldWidget/InlineEntityFormBase.php
+++ b/src/Plugin/Field/FieldWidget/InlineEntityFormBase.php
@@ -353,6 +353,17 @@ abstract class InlineEntityFormBase extends WidgetBase implements ContainerFacto
   }
 
   /**
+   * Gets current language code from the form state or item.
+   *
+   * @param \Drupal\Core\Form\FormStateInterface $form_state
+   * @param \Drupal\Core\Field\FieldItemListInterface $items
+   * @return string
+   */
+  protected function getCurrentLangcode(FormStateInterface $form_state, FieldItemListInterface $items) {
+    return $form_state->get('langcode') ?: $items->getEntity()->language()->getId();
+  }
+
+  /**
    * Adds submit callbacks to the inline entity form.
    *
    * @param array $element
diff --git a/src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php b/src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php
index c7ca41b..39c594d 100644
--- a/src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php
+++ b/src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php
@@ -192,6 +192,7 @@ class InlineEntityFormComplex extends InlineEntityFormBase implements ContainerF
 
     // Get the langcode of the parent entity.
     $parent_langcode = $items->getParent()->getValue()->language()->getId();
+    $target_langcode = $form_state->get('langcode') ?: $parent_langcode;
 
     // Determine the wrapper ID for the entire element.
     $wrapper = 'inline-entity-form-' . $this->getIefId();
@@ -232,8 +233,24 @@ class InlineEntityFormComplex extends InlineEntityFormBase implements ContainerF
       $form_state->set(['inline_entity_form', $this->getIefId(), 'entities'], array());
 
       if (count($items)) {
+        $target_langcode = $this->getCurrentLangcode($form_state, $items);
         foreach ($items as $delta => $item) {
           if ($item->entity && is_object($item->entity)) {
+            if ($item->entity->isTranslatable()) {
+              // If target translation is not yet available, populate it with data from the original entity.
+              if ($item->entity->language()->getId() != $target_langcode && !$item->entity->hasTranslation($target_langcode)) {
+                $item->entity = $item->entity->addTranslation($target_langcode, $item->entity->toArray());
+                if ($item->entity->getEntityType()->isRevisionable()) {
+                  $item->entity->setRevisionTranslationAffected(NULL);
+                }
+                $metadata = \Drupal::service('content_translation.manager')->getTranslationMetadata($item->entity);
+                $metadata->setSource($parent_langcode);
+              }
+
+              // Initiate the entity with the correct translation.
+              $item->entity = $item->entity->getTranslation($target_langcode);
+            }
+
             $form_state->set(['inline_entity_form', $this->getIefId(), 'entities', $delta], array(
               'entity' => $item->entity,
               '_weight' => $delta,
@@ -312,7 +329,7 @@ class InlineEntityFormComplex extends InlineEntityFormBase implements ContainerF
             'inline_entity_form' => $this->getInlineEntityForm(
               $value['form'],
               $entity->bundle(),
-              $parent_langcode,
+              $target_langcode,
               $key,
               array_merge($parents,  ['inline_entity_form', 'entities', $key, 'form']),
               $entity
@@ -510,7 +527,7 @@ class InlineEntityFormComplex extends InlineEntityFormBase implements ContainerF
           'inline_entity_form' => $this->getInlineEntityForm(
             'add',
             $this->determineBundle($form_state),
-            $parent_langcode,
+            $target_langcode,
             NULL,
             array_merge($parents, ['inline_entity_form'])
           )
@@ -533,7 +550,7 @@ class InlineEntityFormComplex extends InlineEntityFormBase implements ContainerF
           // Pass the current entity type.
           '#entity_type' => $target_type,
           // Pass the langcode of the parent entity,
-          '#parent_language' => $parent_langcode,
+          '#parent_language' => $target_langcode,
           // Pass the widget specific labels.
           '#ief_labels' => $this->getEntityTypeLabels(),
         );
diff --git a/src/Plugin/Field/FieldWidget/InlineEntityFormSimple.php b/src/Plugin/Field/FieldWidget/InlineEntityFormSimple.php
index e64f664..5201657 100644
--- a/src/Plugin/Field/FieldWidget/InlineEntityFormSimple.php
+++ b/src/Plugin/Field/FieldWidget/InlineEntityFormSimple.php
@@ -36,6 +36,21 @@ class InlineEntityFormSimple extends InlineEntityFormBase {
         $element['warning']['#markup'] = $this->t('Unable to load the referenced entity.');
         return $element;
       }
+
+      if ($entity->isTranslatable()) {
+        $target_langcode = $this->getCurrentLangcode($form_state, $items);
+        // If target translation is not yet available, populate it with data from the original entity.
+        if ($entity->language()->getId() != $target_langcode && !$entity->hasTranslation($target_langcode)) {
+          $entity = $entity->addTranslation($target_langcode, $entity->toArray());
+          if ($entity->getEntityType()->isRevisionable()) {
+            $entity->setRevisionTranslationAffected(NULL);
+          }
+          $metadata = \Drupal::service('content_translation.manager')->getTranslationMetadata($entity);
+          $metadata->setSource($parent_langcode);
+        }
+        // Initiate the entity with the correct translation.
+        $entity = $entity->getTranslation($target_langcode);
+      }
     }
 
     $op = isset($entity) ? 'edit' : 'add';
-- 
2.6.4

