From c4e5c497da11ac77ef257fa5c057fee81bc135d4 Mon Sep 17 00:00:00 2001
From: catch <catch@35733.no-reply.drupal.org>
Date: Tue, 17 Sep 2019 10:20:21 +0100
Subject: [PATCH] Issue #2835869 by hchonov, gease, tstoeckler: Prepare content
 translation for nested inline entity forms

---
 .../src/ContentTranslationHandler.php           | 17 ++++++-----------
 .../Controller/ContentTranslationController.php |  4 +++-
 2 files changed, 9 insertions(+), 12 deletions(-)

diff --git a/core/modules/content_translation/src/ContentTranslationHandler.php b/core/modules/content_translation/src/ContentTranslationHandler.php
index 7e9e980a1a87..450a6550956a 100644
--- a/core/modules/content_translation/src/ContentTranslationHandler.php
+++ b/core/modules/content_translation/src/ContentTranslationHandler.php
@@ -314,18 +314,18 @@ public function getSourceLangcode(FormStateInterface $form_state) {
   public function entityFormAlter(array &$form, FormStateInterface $form_state, EntityInterface $entity) {
     /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
 
+    $metadata = $this->manager->getTranslationMetadata($entity);
     $form_object = $form_state->getFormObject();
     $form_langcode = $form_object->getFormLangcode($form_state);
     $entity_langcode = $entity->getUntranslated()->language()->getId();
-    $source_langcode = $this->getSourceLangcode($form_state);
 
-    $new_translation = !empty($source_langcode);
+    $new_translation = $entity->isNewTranslation();
     $translations = $entity->getTranslationLanguages();
     if ($new_translation) {
       // Make sure a new translation does not appear as existing yet.
       unset($translations[$form_langcode]);
     }
-    $is_translation = !$form_object->isDefaultFormLangcode($form_state);
+    $is_translation = $new_translation || ($entity->language()->getId() != $entity_langcode);
     $has_translations = count($translations) > 1;
 
     // Adjust page title to specify the current language being edited, if we
@@ -336,7 +336,7 @@ public function entityFormAlter(array &$form, FormStateInterface $form_state, En
       // When editing the original values display just the entity label.
       if ($is_translation) {
         $t_args = ['%language' => $languages[$form_langcode]->getName(), '%title' => $entity->label(), '@title' => $title];
-        $title = empty($source_langcode) ? t('@title [%language translation]', $t_args) : t('Create %language translation of %title', $t_args);
+        $title = $new_translation ? t('Create %language translation of %title', $t_args) : t('@title [%language translation]', $t_args);
       }
       $form['#title'] = $title;
     }
@@ -344,6 +344,7 @@ public function entityFormAlter(array &$form, FormStateInterface $form_state, En
     // Display source language selector only if we are creating a new
     // translation and there are at least two translations available.
     if ($has_translations && $new_translation) {
+      $source_langcode = $metadata->getSource();
       $form['source_langcode'] = [
         '#type' => 'details',
         '#title' => t('Source language: @language', ['@language' => $languages[$source_langcode]->getName()]),
@@ -435,7 +436,7 @@ public function entityFormAlter(array &$form, FormStateInterface $form_state, En
         '#title' => t('Translation'),
         '#tree' => TRUE,
         '#weight' => 10,
-        '#access' => $this->getTranslationAccess($entity, $source_langcode ? 'create' : 'update')->isAllowed(),
+        '#access' => $this->getTranslationAccess($entity, $new_translation ? 'create' : 'update')->isAllowed(),
         '#multilingual' => TRUE,
       ];
 
@@ -450,7 +451,6 @@ public function entityFormAlter(array &$form, FormStateInterface $form_state, En
       }
 
       // A new translation is enabled by default.
-      $metadata = $this->manager->getTranslationMetadata($entity);
       $status = $new_translation || $metadata->isPublished();
       // If there is only one published translation we cannot unpublish it,
       // since there would be nothing left to display.
@@ -680,11 +680,6 @@ public function entityFormEntityBuild($entity_type, EntityInterface $entity, arr
     $metadata->setPublished(!empty($values['status']));
     $metadata->setCreatedTime(!empty($values['created']) ? strtotime($values['created']) : REQUEST_TIME);
 
-    $source_langcode = $this->getSourceLangcode($form_state);
-    if ($source_langcode) {
-      $metadata->setSource($source_langcode);
-    }
-
     $metadata->setOutdated(!empty($values['outdated']));
     if (!empty($values['retranslate'])) {
       $this->retranslate($entity, $form_langcode);
diff --git a/core/modules/content_translation/src/Controller/ContentTranslationController.php b/core/modules/content_translation/src/Controller/ContentTranslationController.php
index d7bdff90a95a..5d48cc05335c 100644
--- a/core/modules/content_translation/src/Controller/ContentTranslationController.php
+++ b/core/modules/content_translation/src/Controller/ContentTranslationController.php
@@ -71,8 +71,9 @@ public static function create(ContainerInterface $container) {
    *   The language to be used as target.
    */
   public function prepareTranslation(ContentEntityInterface $entity, LanguageInterface $source, LanguageInterface $target) {
+    $source_langcode = $source->getId();
     /* @var \Drupal\Core\Entity\ContentEntityInterface $source_translation */
-    $source_translation = $entity->getTranslation($source->getId());
+    $source_translation = $entity->getTranslation($source_langcode);
     $target_translation = $entity->addTranslation($target->getId(), $source_translation->toArray());
 
     // Make sure we do not inherit the affected status from the source values.
@@ -88,6 +89,7 @@ public function prepareTranslation(ContentEntityInterface $entity, LanguageInter
     // creation time.
     $metadata->setAuthor($user);
     $metadata->setCreatedTime(REQUEST_TIME);
+    $metadata->setSource($source_langcode);
   }
 
   /**
-- 
GitLab