diff -u b/core/lib/Drupal/Core/Render/Renderer.php b/core/lib/Drupal/Core/Render/Renderer.php --- b/core/lib/Drupal/Core/Render/Renderer.php +++ b/core/lib/Drupal/Core/Render/Renderer.php @@ -216,20 +216,20 @@ // A non-empty #children property takes precedence. This happens only if // it has been manually set into the render array. if (!empty($elements['#children'])) { - $children = ['#children']; + $children_keys = ['#children']; } else { - $children = Element::children($elements); - } + $children_keys = Element::children($elements); - if (empty($children)) { - return ''; + if (empty($children_keys)) { + return ''; + } } // Remove all elements except the children because the main level has been - // already rendered when the #render_children is set, and therefore they - // shouldn't have any effect on the render children. - $new_elements = array_intersect_key($elements, array_flip($children)); + // already rendered when the #render_children is set and therefore they + // should not have any effect on the render children. + $new_elements = array_intersect_key($elements, array_flip($children_keys)); // Create a new variable that references the render array that was passed // in. This allows the markup and cache information to be attached after // rendering the new elements array. @@ -539,12 +539,10 @@ $elements['#markup'] = Markup::create($prefix . $elements['#children'] . $suffix); - // #cache, #attached and #markup values should be always saved to the - // referenced elements variable to prevent re-rendering. + // #markup should be always saved to the referenced elements variable to + // prevent re-rendering. if (isset($original_elements)) { $original_elements['#markup'] = $elements['#markup']; - $original_elements['#attached'] = $elements['#attached']; - $original_elements['#cache'] = CacheableMetadata::createFromRenderArray($elements['#cache']); } // We've rendered this element (and its subtree!), now update the context. diff -u b/core/tests/Drupal/KernelTests/Core/Render/RenderTest.php b/core/tests/Drupal/KernelTests/Core/Render/RenderTest.php --- b/core/tests/Drupal/KernelTests/Core/Render/RenderTest.php +++ b/core/tests/Drupal/KernelTests/Core/Render/RenderTest.php @@ -2,6 +2,7 @@ namespace Drupal\KernelTests\Core\Render; +use Drupal\Core\Cache\Cache; use Drupal\KernelTests\KernelTestBase; /** diff -u b/core/tests/Drupal/Tests/Core/Render/RendererTest.php b/core/tests/Drupal/Tests/Core/Render/RendererTest.php --- b/core/tests/Drupal/Tests/Core/Render/RendererTest.php +++ b/core/tests/Drupal/Tests/Core/Render/RendererTest.php @@ -558,6 +558,17 @@ // We don't want to reprint already printed render arrays. $this->assertEquals('', $this->renderer->renderRoot($build)); + + $build = [ + 'child' => [ + '#markup' => 'kittens', + ], + ]; + $this->assertEquals('kittens', $this->renderer->renderRoot($build)); + $this->assertTrue($build['#printed']); + + // We don't want to reprint already printed render arrays. + $this->assertEquals('', $this->renderer->renderRoot($build)); } /**