From 3cb5c72db96223411487b227fcf508a9eb15ab0a Mon Sep 17 00:00:00 2001
From: Nathaniel Catchpole <catch@35733.no-reply.drupal.org>
Date: Tue, 16 May 2017 14:57:08 +0100
Subject: [PATCH] Issue #2848120 by kristiaanvandeneynde, tim.plunkett, Pavan B
 S, tstoeckler: Short term fix: Make ContentTranslationController recognize
 'add' and 'edit' form handlers

---
 .../content_translation/content_translation.module        | 6 ++++--
 .../src/Controller/ContentTranslationController.php       | 8 ++++++--
 2 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/core/modules/content_translation/content_translation.module b/core/modules/content_translation/content_translation.module
index 7c72b84cb2fb..8dbd1702af86 100644
--- a/core/modules/content_translation/content_translation.module
+++ b/core/modules/content_translation/content_translation.module
@@ -296,8 +296,10 @@ function content_translation_form_alter(array &$form, FormStateInterface $form_s
   $entity = $form_object->getEntity();
   $op = $form_object->getOperation();
 
-  // Let the content translation handler alter the content entity edit form.
-  if ($entity instanceof ContentEntityInterface && $entity->isTranslatable() && count($entity->getTranslationLanguages()) > 1 && ($op == 'edit' || $op == 'default')) {
+  // Let the content translation handler alter the content entity form. This can
+  // be the 'add' or 'edit' form. It also tries a 'default' form in case neither
+  // of the aforementioned forms are defined.
+  if ($entity instanceof ContentEntityInterface && $entity->isTranslatable() && count($entity->getTranslationLanguages()) > 1 && in_array($op, ['edit', 'add', 'default'], TRUE)) {
     $controller = \Drupal::entityManager()->getHandler($entity->getEntityTypeId(), 'translation');
     $controller->entityFormAlter($form, $form_state, $entity);
 
diff --git a/core/modules/content_translation/src/Controller/ContentTranslationController.php b/core/modules/content_translation/src/Controller/ContentTranslationController.php
index c97f185a63f3..c18eab395a09 100644
--- a/core/modules/content_translation/src/Controller/ContentTranslationController.php
+++ b/core/modules/content_translation/src/Controller/ContentTranslationController.php
@@ -337,7 +337,9 @@ public function add(LanguageInterface $source, LanguageInterface $target, RouteM
     // @todo Provide a way to figure out the default form operation. Maybe like
     //   $operation = isset($info['default_operation']) ? $info['default_operation'] : 'default';
     //   See https://www.drupal.org/node/2006348.
-    $operation = 'default';
+
+    // Use the add form handler, if available, otherwise default.
+    $operation = $entity->getEntityType()->hasHandlerClass('form', 'add') ? 'add' : 'default';
 
     $form_state_additions = [];
     $form_state_additions['langcode'] = $target->getId();
@@ -368,7 +370,9 @@ public function edit(LanguageInterface $language, RouteMatchInterface $route_mat
     // @todo Provide a way to figure out the default form operation. Maybe like
     //   $operation = isset($info['default_operation']) ? $info['default_operation'] : 'default';
     //   See https://www.drupal.org/node/2006348.
-    $operation = 'default';
+
+    // Use the edit form handler, if available, otherwise default.
+    $operation = $entity->getEntityType()->hasHandlerClass('form', 'edit') ? 'edit' : 'default';
 
     $form_state_additions = [];
     $form_state_additions['langcode'] = $language->getId();
-- 
GitLab