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 @@ -209,9 +209,12 @@ return ''; } - // Render only the children if the #render_children property is set. + // Render only the children if the internal #render_children property is + // set. + // @see \Drupal\Core\Theme\ThemeManager::render(). if (isset($elements['#render_children'])) { - // A non-empty #children property takes precedence. + // 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']; } @@ -223,9 +226,10 @@ return ''; } - // It is okay to modify the original elements for this, because this will - // be called from a twig template, where the variable is passed by value. - $elements = array_intersect_key($elements, array_flip($children)); + // Avoid making this change for the state because it could cause + // unexpected behaviour with other templating engines than Twig. + $new_elements = array_intersect_key($elements, array_flip($children)); + $elements = &$new_elements; } if (!isset($elements['#access']) && isset($elements['#access_callback'])) {