From f2ba76f15497792b32490ca4d7f55a10847b02db Mon Sep 17 00:00:00 2001 From: nod_ <nod_@598310.no-reply.drupal.org> Date: Thu, 29 Feb 2024 21:25:10 +0100 Subject: [PATCH] Issue #3420709 by gorkagr, ressa, Ruturaj Chaubey, larowlan, alexpott: Make it more obvious that a Twig template is overridden --- .../src/Functional/Theme/TwigDebugMarkupTest.php | 8 ++++---- .../views/tests/src/Kernel/ViewsTemplateTest.php | 10 +++++----- core/themes/engines/twig/twig.engine | 13 ++++++++++--- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/core/modules/system/tests/src/Functional/Theme/TwigDebugMarkupTest.php b/core/modules/system/tests/src/Functional/Theme/TwigDebugMarkupTest.php index b28a83fc0db4..53fdfdf3f525 100644 --- a/core/modules/system/tests/src/Functional/Theme/TwigDebugMarkupTest.php +++ b/core/modules/system/tests/src/Functional/Theme/TwigDebugMarkupTest.php @@ -55,18 +55,18 @@ public function testTwigDebugMarkup() { $output = (string) $renderer->renderRoot($build); $this->assertStringContainsString('<!-- THEME DEBUG -->', $output, 'Twig debug markup found in theme output when debug is enabled.'); $this->assertStringContainsString("THEME HOOK: 'node'", $output, 'Theme call information found.'); - $this->assertStringContainsString('* node--1--full' . $extension . PHP_EOL . ' x node--1' . $extension . PHP_EOL . ' * node--page--full' . $extension . PHP_EOL . ' * node--page' . $extension . PHP_EOL . ' * node--full' . $extension . PHP_EOL . ' * node' . $extension, $output, 'Suggested template files found in order and node ID specific template shown as current template.'); + $this->assertStringContainsString('â–ªï¸ node--1--full' . $extension . PHP_EOL . ' ✅ node--1' . $extension . PHP_EOL . ' â–ªï¸ node--page--full' . $extension . PHP_EOL . ' â–ªï¸ node--page' . $extension . PHP_EOL . ' â–ªï¸ node--full' . $extension . PHP_EOL . ' â–ªï¸ node' . $extension, $output, 'Suggested template files found in order and node ID specific template shown as current template.'); $this->assertStringContainsString(Html::escape('node--<script type="text/javascript">alert(\'yo\');</script>'), (string) $output); $this->assertStringContainsString('<!-- INVALID FILE NAME SUGGESTIONS:' . PHP_EOL . ' See https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Render!theme.api.php/function/hook_theme_suggestions_alter' . PHP_EOL . ' invalid_theme_suggestions' . PHP_EOL . '-->', $output, 'Twig debug markup found invalid suggestions.'); $template_filename = $templates['node__1']['path'] . '/' . $templates['node__1']['template'] . $extension; - $this->assertStringContainsString("BEGIN OUTPUT from '$template_filename'", $output, 'Full path to current template file found.'); + $this->assertStringContainsString("💡 BEGIN CUSTOM TEMPLATE OUTPUT from '$template_filename'", $output, 'Full path to current template file found.'); // Create another node and make sure the template suggestions shown in the // debug markup are correct. $node2 = $this->drupalCreateNode(); $build = $builder->view($node2); $output = (string) $renderer->renderRoot($build); - $this->assertStringContainsString('* node--2--full' . $extension . PHP_EOL . ' * node--2' . $extension . PHP_EOL . ' * node--page--full' . $extension . PHP_EOL . ' * node--page' . $extension . PHP_EOL . ' * node--full' . $extension . PHP_EOL . ' x node' . $extension, $output, 'Suggested template files found in order and base template shown as current template.'); + $this->assertStringContainsString('â–ªï¸ node--2--full' . $extension . PHP_EOL . ' â–ªï¸ node--2' . $extension . PHP_EOL . ' â–ªï¸ node--page--full' . $extension . PHP_EOL . ' â–ªï¸ node--page' . $extension . PHP_EOL . ' â–ªï¸ node--full' . $extension . PHP_EOL . ' ✅ node' . $extension, $output, 'Suggested template files found in order and base template shown as current template.'); // Create another node and make sure the template suggestions shown in the // debug markup are correct. @@ -75,7 +75,7 @@ public function testTwigDebugMarkup() { $build += $builder->view($node3); $output = (string) $renderer->renderRoot($build); $this->assertStringContainsString("THEME HOOK: 'node__foo__bar'", $output, 'Theme call information found.'); - $this->assertStringContainsString('* node--foo--bar' . $extension . PHP_EOL . ' * node--foo' . $extension . PHP_EOL . ' * node--<script type="text/javascript">alert('yo');</script>' . $extension . PHP_EOL . ' * node--3--full' . $extension . PHP_EOL . ' * node--3' . $extension . PHP_EOL . ' * node--page--full' . $extension . PHP_EOL . ' * node--page' . $extension . PHP_EOL . ' * node--full' . $extension . PHP_EOL . ' x node' . $extension, $output, 'Suggested template files found in order and base template shown as current template.'); + $this->assertStringContainsString('â–ªï¸ node--foo--bar' . $extension . PHP_EOL . ' â–ªï¸ node--foo' . $extension . PHP_EOL . ' â–ªï¸ node--<script type="text/javascript">alert('yo');</script>' . $extension . PHP_EOL . ' â–ªï¸ node--3--full' . $extension . PHP_EOL . ' â–ªï¸ node--3' . $extension . PHP_EOL . ' â–ªï¸ node--page--full' . $extension . PHP_EOL . ' â–ªï¸ node--page' . $extension . PHP_EOL . ' â–ªï¸ node--full' . $extension . PHP_EOL . ' ✅ node' . $extension, $output, 'Suggested template files found in order and base template shown as current template.'); // Disable debug, rebuild the service container, and clear all caches. $parameters = $this->container->getParameter('twig.config'); diff --git a/core/modules/views/tests/src/Kernel/ViewsTemplateTest.php b/core/modules/views/tests/src/Kernel/ViewsTemplateTest.php index 37a057a7dd3e..d911aa2efbb5 100644 --- a/core/modules/views/tests/src/Kernel/ViewsTemplateTest.php +++ b/core/modules/views/tests/src/Kernel/ViewsTemplateTest.php @@ -57,11 +57,11 @@ public function testThemeSuggestionsContainerAlter() { $output = $this->render($build); $extension = '.html.twig'; $expected = '<!-- FILE NAME SUGGESTIONS:' . PHP_EOL - . ' * container--more-link--test-page-display--default' . $extension . PHP_EOL - . ' * container--more-link--default' . $extension . PHP_EOL - . ' * container--more-link--test-page-display' . $extension . PHP_EOL - . ' * container--more-link' . $extension . PHP_EOL - . ' x container' . $extension . PHP_EOL + . ' â–ªï¸ container--more-link--test-page-display--default' . $extension . PHP_EOL + . ' â–ªï¸ container--more-link--default' . $extension . PHP_EOL + . ' â–ªï¸ container--more-link--test-page-display' . $extension . PHP_EOL + . ' â–ªï¸ container--more-link' . $extension . PHP_EOL + . ' ✅ container' . $extension . PHP_EOL . '-->' . PHP_EOL; $this->assertStringContainsString($expected, $output, 'Views more link container suggestions found in Twig debug output'); } diff --git a/core/themes/engines/twig/twig.engine b/core/themes/engines/twig/twig.engine index f0581b15cd32..f3a8e176e536 100644 --- a/core/themes/engines/twig/twig.engine +++ b/core/themes/engines/twig/twig.engine @@ -89,7 +89,7 @@ function twig_render_template($template_file, array $variables) { continue; } $template = strtr($suggestion, '_', '-') . $extension; - $prefix = ($template == $current_template) ? 'x' : '*'; + $prefix = ($template == $current_template) ? '✅' : 'â–ªï¸'; $suggestion = $prefix . ' ' . $template; } $output['debug_info'] .= "\n<!-- FILE NAME SUGGESTIONS:\n " . Html::escape(implode("\n ", $suggestions)) . "\n-->"; @@ -101,8 +101,15 @@ function twig_render_template($template_file, array $variables) { $output['debug_info'] .= "\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"; + // Check if the template_file belongs to a custom theme + $template_override_status_output = "BEGIN OUTPUT"; + $template_override_suffix_output = "END OUTPUT"; + if (str_starts_with($template_file, $variables['directory'])) { + $template_override_status_output = "💡 BEGIN CUSTOM TEMPLATE OUTPUT"; + $template_override_suffix_output = "END CUSTOM TEMPLATE OUTPUT"; + } + $output['debug_info'] .= "\n<!-- " . $template_override_status_output . " from '" . Html::escape($template_file) . "' -->\n"; + $output['debug_suffix'] .= "\n<!-- " . $template_override_suffix_output . " from '" . Html::escape($template_file) . "' -->\n\n"; } // This output has already been rendered and is therefore considered safe. return Markup::create(implode('', $output)); -- GitLab