Skip to content

Instantly share code, notes, and snippets.

@loominade
Last active March 16, 2022 07:22
Show Gist options
  • Save loominade/23445006f5cea2d1bfd3e95d48c8eb3d to your computer and use it in GitHub Desktop.
Save loominade/23445006f5cea2d1bfd3e95d48c8eb3d to your computer and use it in GitHub Desktop.
Removes whitespace caused by the twig debug mode in Drupal 8.

Nothing will change in Chrome Debug tool. The sourcecode might look a bit messy.

Add the following line to the extras key in your composer.json:


        "patches": {
            "drupal/core": {
                "Removes whitespace caused by the twig debug mode": "https://gist.githubusercontent.com/loominade/23445006f5cea2d1bfd3e95d48c8eb3d/raw/f2f8e82ecae0f86dc913588b8a6362b8a874c082/drupal-8-remove-breaks-in-debug.diff"
            }
        }
--- /core/themes/engines/twig/twig.engine
+++ /core/themes/engines/twig/twig.engine
@@ -74,8 +74,8 @@
throw $e;
}
if ($twig_service->isDebug()) {
- $output['debug_prefix'] .= "\n\n<!-- THEME DEBUG -->";
- $output['debug_prefix'] .= "\n<!-- THEME HOOK: '" . Html::escape($variables['theme_hook_original']) . "' -->";
+ $output['debug_prefix'] .= "<!-- THEME DEBUG -->";
+ $output['debug_prefix'] .= "<!-- THEME HOOK: '" . Html::escape($variables['theme_hook_original']) . "' -->";
// If there are theme suggestions, reverse the array so more specific
// suggestions are shown first.
if (!empty($variables['theme_hook_suggestions'])) {
@@ -109,10 +109,10 @@
$prefix = ($template == $current_template) ? 'x' : '*';
$suggestion = $prefix . ' ' . $template;
}
- $output['debug_info'] .= "\n<!-- FILE NAME SUGGESTIONS:\n " . Html::escape(implode("\n ", $suggestions)) . "\n-->";
+ $output['debug_info'] .= "<!-- FILE NAME SUGGESTIONS:\n " . Html::escape(implode("\n ", $suggestions)) . "\n-->";
}
- $output['debug_info'] .= "\n<!-- BEGIN OUTPUT from '" . Html::escape($template_file) . "' -->\n";
- $output['debug_suffix'] .= "\n<!-- END OUTPUT from '" . Html::escape($template_file) . "' -->\n\n";
+ $output['debug_info'] .= "<!-- BEGIN OUTPUT from '" . Html::escape($template_file) . "' -->";
+ $output['debug_suffix'] .= "<!-- END OUTPUT from '" . Html::escape($template_file) . "' -->";
}
// This output has already been rendered and is therefore considered safe.
return Markup::create(implode('', $output));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment