Skip to content

Instantly share code, notes, and snippets.

@andypost
Last active September 4, 2015 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andypost/2871dc5fd39bb4e70f59 to your computer and use it in GitHub Desktop.
Save andypost/2871dc5fd39bb4e70f59 to your computer and use it in GitHub Desktop.
diff --git a/core/lib/Drupal/Core/Render/Renderer.php b/core/lib/Drupal/Core/Render/Renderer.php
index 48b3bcd..825a0ff 100644
--- a/core/lib/Drupal/Core/Render/Renderer.php
+++ b/core/lib/Drupal/Core/Render/Renderer.php
@@ -7,6 +7,7 @@
namespace Drupal\Core\Render;
+use Drupal\Component\Serialization\Json;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Component\Utility\UrlHelper;
@@ -395,6 +396,9 @@ protected function doRender(&$elements, $is_root_call = FALSE) {
$elements['#cache']['max-age'] = isset($elements['#cache']['max-age']) ? $elements['#cache']['max-age'] : Cache::PERMANENT;
$elements['#attached'] = isset($elements['#attached']) ? $elements['#attached'] : array();
+ // Save cache state before render.
+ $elements['#debug']['#cache_start'] = $elements['#cache'];
+
// Allow #pre_render to abort rendering.
if (!empty($elements['#printed'])) {
// The #printed element contains all the bubbleable rendering metadata for
@@ -519,7 +523,8 @@ protected function doRender(&$elements, $is_root_call = FALSE) {
$prefix = isset($elements['#prefix']) ? $this->xssFilterAdminIfUnsafe($elements['#prefix']) : '';
$suffix = isset($elements['#suffix']) ? $this->xssFilterAdminIfUnsafe($elements['#suffix']) : '';
- $elements['#markup'] = SafeString::create($prefix . $elements['#children'] . $suffix);
+ // Safe string created latter for debug.
+ $elements['#markup'] = $prefix . $elements['#children'] . $suffix;
// We've rendered this element (and its subtree!), now update the context.
$context->update($elements);
@@ -553,8 +558,23 @@ protected function doRender(&$elements, $is_root_call = FALSE) {
// Rendering is finished, all necessary info collected!
$context->bubble();
+ // Add debug output.
+ $interesting_keys = ['keys', 'contexts', 'tags', 'max-age'];
+ $prefix = '';
+ if (array_intersect(array_keys($elements['#debug']['#cache_start']), $interesting_keys)) {
+ $prefix .= '<!--' . Json::encode($elements['#debug']['#cache_start']) . '-->';
+ }
+ if (array_intersect(array_keys($elements['#cache']), $interesting_keys)) {
+ $prefix .= '<!--' . Json::encode($elements['#cache']) . '-->';
+ }
+ if ($prefix) {
+ $prefix = '<!--RENDERER_START-->' . $prefix;
+ $suffix = '<!--RENDERER_END-->';
+ $elements['#markup'] = $prefix . $elements['#markup'] . $suffix;
+ }
+
$elements['#printed'] = TRUE;
- return $elements['#markup'];
+ return SafeString::create($elements['#markup']);
}
/**
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment