diff --git a/core/core.services.yml b/core/core.services.yml index 2fa0b45bd5..07b6d0ec0b 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -1536,12 +1536,14 @@ services: arguments: ['@renderer', '@url_generator', '@theme.manager', '@date.formatter', '@file_url_generator'] tags: - { name: twig.extension, priority: 100 } - # @todo Figure out what to do about debugging functions. - # @see https://www.drupal.org/node/1804998 twig.extension.debug: class: Twig\Extension\DebugExtension tags: - - { name: twig.extension } + - { name: twig.extension, priority: 50 } + twig.extension.varDumper: + class: Drupal\Core\Template\DebugExtension + tags: + - { name: twig.extension, priority: 25 } twig.loader: class: Twig\Loader\ChainLoader public: false diff --git a/core/lib/Drupal/Core/Template/DebugExtension.php b/core/lib/Drupal/Core/Template/DebugExtension.php index e69de29bb2..bcf384db65 100644 --- a/core/lib/Drupal/Core/Template/DebugExtension.php +++ b/core/lib/Drupal/Core/Template/DebugExtension.php @@ -0,0 +1,49 @@ + TRUE]), + ]; + } + + return []; + } + + /** + * Dumps information about variables using Symfony VarDumper. + * + * @param array $context + * Variables from the Twig template. + * @param mixed $variable + * (optional) The variable to dump. + */ + public static function dump(array $context, $variable = NULL): void { + $var_dumper = '\Symfony\Component\VarDumper\VarDumper'; + if (class_exists($var_dumper)) { + call_user_func($var_dumper . '::dump', func_num_args() === 1 ? $context : $variable); + } + else { + throw new \LogicException('Could not dump the variable because symfony/var-dumper component is not installed.'); + } + } + +} diff --git a/core/lib/Drupal/Core/Template/TwigExtension.php b/core/lib/Drupal/Core/Template/TwigExtension.php index af43b4cad7..71f587630c 100644 --- a/core/lib/Drupal/Core/Template/TwigExtension.php +++ b/core/lib/Drupal/Core/Template/TwigExtension.php @@ -108,8 +108,6 @@ public function getFunctions() { new TwigFunction('active_theme_path', [$this, 'getActiveThemePath']), new TwigFunction('active_theme', [$this, 'getActiveTheme']), new TwigFunction('create_attribute', [$this, 'createAttribute']), - new TwigFunction('drupal_dump', [self::class, 'drupalDump'], ['needs_context' => TRUE]), - new TwigFunction('dd', [self::class, 'drupalDump'], ['needs_context' => TRUE]), ]; } @@ -655,22 +653,4 @@ public function withoutFilter($element) { return $filtered_element; } - /** - * Dumps information about variables using Symfony VarDumper when available. - * - * @param array $context - * Variables from the Twig template. - * @param mixed $variable - * (optional) The variable to dump. - */ - public static function drupalDump(array $context, $variable = NULL): void { - $var_dumper = '\Symfony\Component\VarDumper\VarDumper'; - if (class_exists($var_dumper)) { - call_user_func($var_dumper . '::dump', func_num_args() === 1 ? $context : $variable); - } - else { - throw new \BadMethodCallException('Could not dump the variable because symfony/var-dumper component is not installed. See https://symfony.com/doc/current/components/var_dumper.html for more information.'); - } - } - } diff --git a/core/modules/system/tests/modules/twig_theme_test/src/TwigThemeTestController.php b/core/modules/system/tests/modules/twig_theme_test/src/TwigThemeTestController.php index 87b2b59f41..1ef74306b0 100644 --- a/core/modules/system/tests/modules/twig_theme_test/src/TwigThemeTestController.php +++ b/core/modules/system/tests/modules/twig_theme_test/src/TwigThemeTestController.php @@ -114,8 +114,8 @@ public function embedTagRender() { /** * Renders for testing drupal_dump function. */ - public function drupalDump() { - return ['#theme' => 'twig_theme_test_drupal_dump']; + public function dump() { + return ['#theme' => 'twig_theme_test_dump']; } } diff --git a/core/modules/system/tests/modules/twig_theme_test/templates/twig_theme_test.dump.html.twig b/core/modules/system/tests/modules/twig_theme_test/templates/twig_theme_test.dump.html.twig index 3199596b7e..210524981f 100644 --- a/core/modules/system/tests/modules/twig_theme_test/templates/twig_theme_test.dump.html.twig +++ b/core/modules/system/tests/modules/twig_theme_test/templates/twig_theme_test.dump.html.twig @@ -1,4 +1,4 @@ -{% set foo = '🐈' %} +{% set foo = '💩' %} {% set bar = '🐣' %} -{{ drupal_dump(foo) }} -{{ dd() }} +{{ dump(foo) }} +{{ dump() }} diff --git a/core/modules/system/tests/modules/twig_theme_test/twig_theme_test.module b/core/modules/system/tests/modules/twig_theme_test/twig_theme_test.module index 32d1281de8..e287696785 100644 --- a/core/modules/system/tests/modules/twig_theme_test/twig_theme_test.module +++ b/core/modules/system/tests/modules/twig_theme_test/twig_theme_test.module @@ -77,9 +77,9 @@ function twig_theme_test_theme($existing, $type, $theme, $path) { 'variables' => [], 'template' => 'twig_theme_test.embed_tag', ]; - $items['twig_theme_test_drupal_dump'] = [ + $items['twig_theme_test_dump'] = [ 'variables' => [], - 'template' => 'twig_theme_test.dd', + 'template' => 'twig_theme_test.dump', ]; return $items; } diff --git a/core/modules/system/tests/modules/twig_theme_test/twig_theme_test.routing.yml b/core/modules/system/tests/modules/twig_theme_test/twig_theme_test.routing.yml index 0e909066c7..1ecab44e1b 100644 --- a/core/modules/system/tests/modules/twig_theme_test/twig_theme_test.routing.yml +++ b/core/modules/system/tests/modules/twig_theme_test/twig_theme_test.routing.yml @@ -77,9 +77,9 @@ twig_theme_test_embed_tag: requirements: _access: 'TRUE' -twig_theme_test_drupal_dump: - path: '/twig-theme-test/drupal-dump' +twig_theme_test_dump: + path: '/twig-theme-test/dump' defaults: - _controller: '\Drupal\twig_theme_test\TwigThemeTestController::drupalDump' + _controller: '\Drupal\twig_theme_test\TwigThemeTestController::dump' requirements: _access: 'TRUE' diff --git a/core/modules/system/tests/src/Functional/Theme/TwigExtensionTest.php b/core/modules/system/tests/src/Functional/Theme/TwigExtensionTest.php index 25db32b610..aa3d844434 100644 --- a/core/modules/system/tests/src/Functional/Theme/TwigExtensionTest.php +++ b/core/modules/system/tests/src/Functional/Theme/TwigExtensionTest.php @@ -95,12 +95,12 @@ public function testsRenderZeroValue() { /** * Tests the drupal_dump function. */ - public function testDrupalDump() { - $this->drupalGet('/twig-theme-test/drupal-dump'); + public function testDump() { + $this->drupalGet('/twig-theme-test/dump'); $this->assertSession()->elementsCount('css', '.sf-dump', 2); $dumps = $this->getSession()->getPage()->findAll('css', '.sf-dump'); $this->assertEquals(2, count($dumps)); - $this->assertStringContainsString('🐈', $dumps[0]->getText()); + $this->assertStringContainsString('💩', $dumps[0]->getText()); $this->assertStringNotContainsString('🐣', $dumps[0]->getText()); $this->assertStringContainsString('"bar" => "🐣"', $dumps[1]->getText()); }