From 2317a3968d427c4864c33d37ad26bf3339dfbab3 Mon Sep 17 00:00:00 2001
From: Nathaniel Catchpole <catch@35733.no-reply.drupal.org>
Date: Thu, 15 Sep 2016 09:24:42 +0100
Subject: [PATCH] Issue #2773333 by Berdir, Juterpillar, Wim Leers, Fabianx,
 renukakulkarni: Form validation errors, status messages on form submission
 are shown after page refresh when form is rendered in block

---
 core/core.services.yml                                |  5 ++++-
 .../lib/Drupal/Core/Render/Element/StatusMessages.php |  7 +++++--
 core/modules/user/src/Tests/UserBlocksTest.php        | 11 +++++++++++
 3 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/core/core.services.yml b/core/core.services.yml
index 6310c211b55e..0b18c3b19357 100644
--- a/core/core.services.yml
+++ b/core/core.services.yml
@@ -1591,10 +1591,13 @@ services:
     tags:
       - { name: mime_type_guesser }
     lazy: true
+  # Currently needs to be public as it is called by
+  # \Drupal\Core\Render\Element\StatusMessages.
+  # @todo Consider making this service private again after
+  #   https://www.drupal.org/node/2367555 lands.
   render_placeholder_generator:
     class: Drupal\Core\Render\PlaceholderGenerator
     arguments: ['%renderer.config%']
-    public: false
   render_cache:
     class: Drupal\Core\Render\PlaceholderingRenderCache
     arguments: ['@request_stack', '@cache_factory', '@cache_contexts_manager', '@render_placeholder_generator']
diff --git a/core/lib/Drupal/Core/Render/Element/StatusMessages.php b/core/lib/Drupal/Core/Render/Element/StatusMessages.php
index f026aa5c58a9..d16c245a1d81 100644
--- a/core/lib/Drupal/Core/Render/Element/StatusMessages.php
+++ b/core/lib/Drupal/Core/Render/Element/StatusMessages.php
@@ -45,12 +45,15 @@ public function getInfo() {
    *   The updated renderable array containing the placeholder.
    */
   public static function generatePlaceholder(array $element) {
-    $element['messages_placeholder'] = [
+    $element = [
       '#lazy_builder' => [get_class() . '::renderMessages', [$element['#display']]],
       '#create_placeholder' => TRUE,
     ];
 
-    return $element;
+    // Directly create a placeholder as we need this to be placeholdered
+    // regardless if this is a POST or GET request.
+    // @todo remove this when https://www.drupal.org/node/2367555 lands.
+    return \Drupal::service('render_placeholder_generator')->createPlaceholder($element);
   }
 
   /**
diff --git a/core/modules/user/src/Tests/UserBlocksTest.php b/core/modules/user/src/Tests/UserBlocksTest.php
index f66007e2d8ad..9216a2765d1f 100644
--- a/core/modules/user/src/Tests/UserBlocksTest.php
+++ b/core/modules/user/src/Tests/UserBlocksTest.php
@@ -87,6 +87,17 @@ function testUserLoginBlock() {
     $this->drupalPostForm('http://example.com/', $edit, t('Log in'), array('external' => FALSE));
     // Check that we remain on the site after login.
     $this->assertUrl($user->url('canonical', ['absolute' => TRUE]), [], 'Redirected to user profile page after login from the frontpage');
+
+    // Verify that form validation errors are displayed immediately for forms
+    // in blocks and not on subsequent page requests.
+    $this->drupalLogout();
+    $edit = array();
+    $edit['name'] = 'foo';
+    $edit['pass'] = 'invalid password';
+    $this->drupalPostForm('filter/tips', $edit, t('Log in'));
+    $this->assertText(t('Unrecognized username or password. Forgot your password?'));
+    $this->drupalGet('filter/tips');
+    $this->assertNoText(t('Unrecognized username or password. Forgot your password?'));
   }
 
   /**
-- 
GitLab