core/lib/Drupal/Core/Render/Renderer.php | 23 ++++++---------- core/modules/big_pipe/src/Render/BigPipe.php | 39 +++++++++++++++------------- 2 files changed, 29 insertions(+), 33 deletions(-) diff --git a/core/lib/Drupal/Core/Render/Renderer.php b/core/lib/Drupal/Core/Render/Renderer.php index a624c0f..14d9713 100644 --- a/core/lib/Drupal/Core/Render/Renderer.php +++ b/core/lib/Drupal/Core/Render/Renderer.php @@ -665,25 +665,18 @@ protected function replacePlaceholders(array &$elements) { // to this one special case, with this hard-coded solution. // @see \Drupal\Core\Render\Element\StatusMessages // @see https://www.drupal.org/node/2712935#comment-11368923 - $possible_message_placeholders = [ - '', - '', - '', - '', - ]; - - // We hardcoded the placeholders above to avoid doing this on every request, - // but we ensure those hardcoded strings are accurate via assertions. - assert('$possible_message_placeholders[0] === (string) $this->placeholderGenerator->createPlaceholder(["#lazy_builder" => ["Drupal\\Core\\Render\\Element\\StatusMessages::renderMessages", [NULL]], "#create_placeholder" => TRUE])["#markup"]'); - assert('$possible_message_placeholders[1] === (string) $this->placeholderGenerator->createPlaceholder(["#lazy_builder" => ["Drupal\\Core\\Render\\Element\\StatusMessages::renderMessages", ["status"]], "#create_placeholder" => TRUE])["#markup"]'); - assert('$possible_message_placeholders[2] === (string) $this->placeholderGenerator->createPlaceholder(["#lazy_builder" => ["Drupal\\Core\\Render\\Element\\StatusMessages::renderMessages", ["warning"]], "#create_placeholder" => TRUE])["#markup"]'); - assert('$possible_message_placeholders[3] === (string) $this->placeholderGenerator->createPlaceholder(["#lazy_builder" => ["Drupal\\Core\\Render\\Element\\StatusMessages::renderMessages", ["error"]], "#create_placeholder" => TRUE])["#markup"]'); + $message_placeholders = []; + foreach ($elements['#attached']['placeholders'] as $placeholder => $placeholder_element) { + if (isset($placeholder_element['#lazy_builder']) && $placeholder_element['#lazy_builder'][0] === 'Drupal\Core\Render\Element\StatusMessages::renderMessages') { + $message_placeholders[] = $placeholder; + } + } // Render placeholders in the appropriate order. $placeholders = array_keys($elements['#attached']['placeholders']); $ordered_placeholders = array_merge( - array_diff($placeholders, $possible_message_placeholders), - array_intersect($placeholders, $possible_message_placeholders) + array_diff($placeholders, $message_placeholders), + array_intersect($placeholders, $message_placeholders) ); foreach ($ordered_placeholders as $placeholder) { $elements = $this->renderPlaceholder($placeholder, $elements); diff --git a/core/modules/big_pipe/src/Render/BigPipe.php b/core/modules/big_pipe/src/Render/BigPipe.php index 00ec4a2..a965068 100644 --- a/core/modules/big_pipe/src/Render/BigPipe.php +++ b/core/modules/big_pipe/src/Render/BigPipe.php @@ -128,7 +128,7 @@ public function sendContent($content, array $attachments) { list($pre_body, $post_body) = explode('', $content, 2); $this->sendPreBody($pre_body, $nojs_placeholders, $cumulative_assets); - $this->sendPlaceholders($placeholders, $this->getPlaceholderOrder($pre_body), $cumulative_assets); + $this->sendPlaceholders($placeholders, $this->getPlaceholderOrder($pre_body, $placeholders), $cumulative_assets); $this->sendPostBody($post_body); // Close the session again. @@ -528,6 +528,9 @@ protected function renderPlaceholder($placeholder, array $placeholder_render_arr * * @param string $html * HTML markup. + * @param array $placeholders + * Associative array; the BigPipe placeholders. Keys are the BigPipe + * placeholder IDs. * * @return array * Indexed array; the order in which the BigPipe placeholders must be sent. @@ -535,17 +538,17 @@ protected function renderPlaceholder($placeholder, array $placeholder_render_arr * placeholders are kept: if the same placeholder occurs multiple times, we * only keep the first occurrence. */ - protected function getPlaceholderOrder($html) { + protected function getPlaceholderOrder($html, $placeholders) { $fragments = explode('
', $fragment, 2); - $placeholder = $t[0]; - $placeholders[] = $placeholder; + $placeholder_id = $t[0]; + $placeholder_ids[] = $placeholder_id; } - $placeholders = array_unique($placeholders); + $placeholder_ids = array_unique($placeholder_ids); // The 'status messages' placeholder needs to be special cased, because it // depends on global state that can be modified when other placeholders are @@ -560,20 +563,20 @@ protected function getPlaceholderOrder($html) { // @see \Drupal\Core\Render\Element\StatusMessages // @see \Drupal\Core\Render\Renderer::replacePlaceholders() // @see https://www.drupal.org/node/2712935#comment-11368923 - $possible_message_placeholders = [ - 'callback=Drupal%5CCore%5CRender%5CElement%5CStatusMessages%3A%3ArenderMessages&args[0]&token=a8c34b5e', - 'callback=Drupal%5CCore%5CRender%5CElement%5CStatusMessages%3A%3ArenderMessages&args[0]=status&token=a26f536e', - 'callback=Drupal%5CCore%5CRender%5CElement%5CStatusMessages%3A%3ArenderMessages&args[0]=warning&token=67320bbd', - 'callback=Drupal%5CCore%5CRender%5CElement%5CStatusMessages%3A%3ArenderMessages&args[0]=error&token=35b549cb', - ]; + $message_placeholder_ids = []; + foreach ($placeholders as $placeholder_id => $placeholder_element) { + if (isset($placeholder_element['#lazy_builder']) && $placeholder_element['#lazy_builder'][0] === 'Drupal\Core\Render\Element\StatusMessages::renderMessages') { + $message_placeholder_ids[] = $placeholder_id; + } + } - // Return placeholder IDs in DOM order, but with the 'status messages' at - // the end, if it exists. - $ordered_placeholders = array_merge( - array_diff($placeholders, $possible_message_placeholders), - array_intersect($placeholders, $possible_message_placeholders) + // Return placeholder IDs in DOM order, but with the 'status messages' + // placeholders at the end, if they are present. + $ordered_placeholder_ids = array_merge( + array_diff($placeholder_ids, $message_placeholder_ids), + array_intersect($placeholder_ids, $message_placeholder_ids) ); - return $ordered_placeholders; + return $ordered_placeholder_ids; } }