diff --git a/core/modules/system/lib/Drupal/system/Tests/Theme/TwigDebugMarkupTest.php b/core/modules/system/lib/Drupal/system/Tests/Theme/TwigDebugMarkupTest.php new file mode 100644 index 0000000..98707a5 --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Tests/Theme/TwigDebugMarkupTest.php @@ -0,0 +1,55 @@ + 'Twig debug markup', + 'description' => 'Tests Twig debug markup.', + 'group' => 'Theme', + ); + } + + /** + * Tests that debug markup is added to Twig template output. + */ + function testTwigDebugMarkup() { + theme_enable(array('test_theme_twig')); + variable_set('theme_default', 'test_theme_twig'); + // Enable debug, rebuild the service container, and clear all caches. + $this->settingsSet('twig_debug', TRUE); + $this->rebuildContainer(); + $this->resetAll(); + + $output = theme('theme_test_template_test'); + $this->assertTrue(strpos($output, '') !== FALSE, 'Twig debug markup found in theme output when debug is enabled.'); + + // Disable debug, rebuild the service container, and clear all caches. + $this->settingsSet('twig_debug', FALSE); + $this->rebuildContainer(); + $this->resetAll(); + + $output = theme('theme_test_template_test'); + $this->assertFalse(strpos($output, '') !== FALSE, 'Twig debug markup not found in theme output when debug is disabled.'); + } + +} diff --git a/core/themes/engines/twig/twig.engine b/core/themes/engines/twig/twig.engine index d318263..8936661 100644 --- a/core/themes/engines/twig/twig.engine +++ b/core/themes/engines/twig/twig.engine @@ -31,22 +31,46 @@ function twig_init($template) { } /** - * Render twig templates. + * Renders a Twig template. * - * This retrieves the Twig_Environment from the Drupal Injection container and - * renders the template. + * If the Twig debug setting is enabled, HTML comments including theme() call + * and template file name suggestions will surround the template markup. * * @param $template_file - * The filename of the template to render. + * The file name of the template to render. * @param $variables * A keyed array of variables that will appear in the output. * * @return - * The output generated by the template. + * The output generated by the template, plus any debug information. */ function twig_render_template($template_file, $variables) { $variables['_references'] = array(); - return drupal_container()->get('twig')->loadTemplate($template_file)->render($variables); + $output = array( + 'debug_prefix' => '', + 'debug_info' => '', + 'rendered_markup' => drupal_container()->get('twig')->loadTemplate($template_file)->render($variables), + 'debug_suffix' => '', + ); + if (settings()->get('twig_debug', FALSE)) { + $output['debug_prefix'] .= "\n\n"; + $output['debug_prefix'] .= "\n"; + if (!empty($variables['theme_hook_suggestions'])) { + $extension = twig_extension(); + $current_template = basename($template_file); + $suggestions = $variables['theme_hook_suggestions']; + $suggestions[] = $variables['theme_hook_original']; + foreach ($suggestions as $key => &$suggestion) { + $template = $suggestion . $extension; + $prefix = ($template == $current_template) ? 'x' : '*'; + $suggestion = $prefix . ' ' . str_replace('_', '-', $template); + } + $output['debug_info'] .= "\n"; + } + $output['debug_info'] .= "\n\n"; + $output['debug_suffix'] .= "\n\n\n"; + } + return implode('', $output); } /** diff --git a/sites/default/default.settings.php b/sites/default/default.settings.php index 4c1239a..1356d79 100644 --- a/sites/default/default.settings.php +++ b/sites/default/default.settings.php @@ -286,11 +286,16 @@ /** * Twig debugging: * - * When enabled, you can use the 'dump' function in Twig templates to output - * information about variables, and templates are automatically recompiled - * whenever the source code changes. - * - * @see http://drupal.org/node/1906392 + * When debugging is enabled: + * - The markup of each Twig template is surrounded by HTML comments which + * contain theming information such as template file name suggestions. + * - The 'dump' function can be used in Twig templates to output information + * about template variables. + * - Twig templates are automatically recompiled whenever the source code + * changes (see twig_auto_reload below). + * + * For more information about debugging Twig templates, see + * http://drupal.org/node/1906392. * * Not recommended in production environments (Default: FALSE). */