From 6c01bb31917878cece0d1166a5c033472eb80e84 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Sat, 8 Feb 2025 21:55:36 +0000
Subject: [PATCH] Issue #3096570 by recrit, raman.b, ameymudras,
 ranjith_kumar_k_u, Oscaner, smustgrave, peterwcm, pameeela: Redirect correct
 language page after node save

---
 .../ImageOnTranslatedEntityTest.php           | 18 ++++++-------
 core/modules/node/src/NodeForm.php            |  3 ++-
 .../src/Functional/NodeTranslationUITest.php  | 27 +++++++++++++++++++
 3 files changed, 38 insertions(+), 10 deletions(-)

diff --git a/core/modules/image/tests/src/Functional/ImageOnTranslatedEntityTest.php b/core/modules/image/tests/src/Functional/ImageOnTranslatedEntityTest.php
index 127559229f05..ccbbcfe9a802 100644
--- a/core/modules/image/tests/src/Functional/ImageOnTranslatedEntityTest.php
+++ b/core/modules/image/tests/src/Functional/ImageOnTranslatedEntityTest.php
@@ -119,12 +119,12 @@ public function testSyncedImages(): void {
     $this->submitForm($edit, 'Save (this translation)');
     // This inspects the HTML after the post of the translation, the image
     // should be displayed on the original node.
-    $this->assertSession()->responseContains('alt="Lost in translation image"');
-    $this->assertSession()->responseContains('title="Lost in translation image title"');
-    $second_fid = $this->getLastFileId();
-    // View the translated node.
-    $this->drupalGet('fr/node/' . $default_language_node->id());
     $this->assertSession()->responseContains('alt="Scarlett Johansson image"');
+    $this->assertSession()->responseContains('title="Scarlett Johansson image title"');
+    $second_fid = $this->getLastFileId();
+    // View the untranslated node.
+    $this->drupalGet('node/' . $default_language_node->id());
+    $this->assertSession()->responseContains('alt="Lost in translation image"');
 
     \Drupal::entityTypeManager()->getStorage('file')->resetCache();
 
@@ -159,12 +159,12 @@ public function testSyncedImages(): void {
     $this->assertTrue($file->isPermanent(), 'First file still exists and is permanent.');
     // This inspects the HTML after the post of the translation, the image
     // should be displayed on the original node.
-    $this->assertSession()->responseContains('alt="Lost in translation image"');
-    $this->assertSession()->responseContains('title="Lost in translation image title"');
-    // View the translated node.
-    $this->drupalGet('nl/node/' . $default_language_node->id());
     $this->assertSession()->responseContains('alt="Ada Lovelace image"');
     $this->assertSession()->responseContains('title="Ada Lovelace image title"');
+    // View untranslated node.
+    $this->drupalGet('node/' . $default_language_node->id());
+    $this->assertSession()->responseContains('alt="Lost in translation image"');
+    $this->assertSession()->responseContains('title="Lost in translation image title"');
 
     // Ensure the file status of the second file is permanent.
     $file = File::load($second_fid);
diff --git a/core/modules/node/src/NodeForm.php b/core/modules/node/src/NodeForm.php
index c3893136360e..ab81a4e3f928 100644
--- a/core/modules/node/src/NodeForm.php
+++ b/core/modules/node/src/NodeForm.php
@@ -298,7 +298,8 @@ public function save(array $form, FormStateInterface $form_state) {
       if ($node->access('view')) {
         $form_state->setRedirect(
           'entity.node.canonical',
-          ['node' => $node->id()]
+          ['node' => $node->id()],
+          ['language' => $node->language()]
         );
       }
       else {
diff --git a/core/modules/node/tests/src/Functional/NodeTranslationUITest.php b/core/modules/node/tests/src/Functional/NodeTranslationUITest.php
index d34bcaa6aa5f..2bb252f7c6e1 100644
--- a/core/modules/node/tests/src/Functional/NodeTranslationUITest.php
+++ b/core/modules/node/tests/src/Functional/NodeTranslationUITest.php
@@ -668,4 +668,31 @@ public function testSearchIndexRebuildOnTranslationDeletion(): void {
     $this->assertSession()->pageTextNotContains('First rev fr title');
   }
 
+  /**
+   * Tests redirection after saving translation.
+   */
+  public function testRedirect(): void {
+    $this->drupalLogin($this->administrator);
+
+    $article = $this->drupalCreateNode(['type' => 'article', 'langcode' => $this->langcodes[0]]);
+
+    $edit = [
+      'title[0][value]' => 'English node title',
+    ];
+    $this->drupalGet('node/' . $article->id() . '/edit');
+    $this->submitForm($edit, 'Save');
+
+    $this->assertSession()->pageTextContains('English node title');
+    $this->assertEquals($this->baseUrl . '/node/' . $article->id(), $this->getSession()->getCurrentUrl());
+
+    $this->drupalGet('node/' . $article->id() . '/translations/add/' . $this->langcodes[0] . '/' . $this->langcodes[1]);
+    $edit = [
+      'title[0][value]' => 'Italian node title',
+    ];
+    $this->submitForm($edit, 'Save (this translation)');
+
+    $this->assertSession()->pageTextContains('Italian node title');
+    $this->assertEquals($this->baseUrl . '/' . $this->langcodes[1] . '/node/' . $article->id(), $this->getSession()->getCurrentUrl());
+  }
+
 }
-- 
GitLab