Problem:

It's really difficult to know what variables are available when writing templates.

Solution

Let's implement the Symfony VarDumper in with core. So, people can just add {{ dd(content) }} into their templates, and get output such as this:

Already implemented within Twig Tweak

The Twig Tweak module already does this (see https://git.drupalcode.org/project/twig_tweak/-/blob/3.x/docs/cheat-shee...). It works great!

Comments

mherchel created an issue. See original summary.

mherchel’s picture

Issue summary: View changes
lauriii’s picture

Status: Active » Needs review
StatusFileSize
new5.12 KB
lauriii’s picture

StatusFileSize
new5.12 KB
new550 bytes

🙈

Status: Needs review » Needs work

The last submitted patch, 4: 3306864-4.patch, failed testing. View results

lauriii’s picture

Status: Needs work » Needs review
StatusFileSize
new5.37 KB
new2.45 KB

I swear that test was passing locally.. But it did fail locally 😬

I improved the tests and it should pass now.

mherchel’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new214.84 KB

OMG this is amazing! You're doing the Lord's work here @lauriii!

I tried to break this through various means of inserting emojis and nonsensical data. I also installed the Twig Tweak module and tested with that to make sure it works... and it all worked as expected!

Below is the result from inserting a {{ dd(_context) }} into core/themes/olivero/templates/content/node--teaser.html.twig

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

I think we should reconsider the naming of drupal_dump() and dd(). I think dd() originates in Laravel and means dump and die as in stop execution. It means the same thing in Symfony too - see vendor/symfony/var-dumper/Resources/functions/dump.php. For more info see https://laravel.com/docs/9.x/helpers#method-dd

I think we should be adding a single function here called dump - I don't think the "drupal_" adds anything. And dump would do pretty much what it does in Symfony and Laravel too.

alexpott’s picture

Another argument in favour of #8 is if you have core's dev dependencies installed and you do dd() in PHP code it will dump and die.

lauriii’s picture

Status: Needs work » Needs review
StatusFileSize
new6.2 KB
new7.81 KB

Wondering if this could fly.. I'm overriding the dump() function when \Symfony\Component\VarDumper\VarDumper is available.

alexpott’s picture

  1. +++ b/core/lib/Drupal/Core/Template/DebugExtension.php
    @@ -0,0 +1,49 @@
    +    $var_dumper = '\Symfony\Component\VarDumper\VarDumper';
    ...
    +    $var_dumper = '\Symfony\Component\VarDumper\VarDumper';
    

    Let's make this a private class const

  2. Also looking in twig_var_dump() there's
            foreach ($context as $key => $value) {
                if (!$value instanceof Template && !$value instanceof TemplateWrapper) {
                    $vars[$key] = $value;
                }
            }
    

    Should we be doing something similar? I wonder why this is there?

  3. +++ b/core/core.services.yml
    @@ -1536,12 +1536,14 @@ services:
    +      - { name: twig.extension, priority: 25 }
    

    Weird - so a lower priority comes first?

alexpott’s picture

Oh and the twig one does:

    if (!$env->isDebug()) {
        return;
    }

Which also seems reasonable in a way. Not sure.

Status: Needs review » Needs work

The last submitted patch, 10: 3306864-10.patch, failed testing. View results

lauriii’s picture

Status: Needs work » Needs review
StatusFileSize
new6.94 KB
new3.53 KB

#11.2: I think that may be something specific to the fact that twig_var_dump uses var_dump. The var dumper code is a bit hard to grasp but it looks like it may have some processing for this use case.

#11.3: One with lower priority takes precedent because it's loaded last.

lauriii’s picture

StatusFileSize
new7.6 KB
new4.27 KB

Got some feedback from @alexpott on Slack. This should address that.

alexpott’s picture

+++ b/core/lib/Drupal/Core/Template/DebugExtension.php
@@ -0,0 +1,70 @@
+        new TwigFunction('dump', [self::class, 'dump'], ['needs_context' => TRUE, 'needs_environment' => TRUE]),

I think this needs 'is_variadic' => TRUE in the options too now.

lauriii’s picture

StatusFileSize
new7.62 KB
new1.39 KB

That's true 👍

alexpott’s picture

  1. I like all the new improvements
  2. +++ b/core/lib/Drupal/Core/Template/DebugExtension.php
    @@ -0,0 +1,70 @@
    +final class DebugExtension extends AbstractExtension {
    ...
    +    if (class_exists($this::SYMFONY_VAR_DUMPER_CLASS)) {
    ...
    +        call_user_func(static::SYMFONY_VAR_DUMPER_CLASS . '::dump', $context);
    

    final and static or $this... imo we should be doing self:: everywhere here. There's no point doing $this::

  3. +++ b/core/lib/Drupal/Core/Template/DebugExtension.php
    @@ -0,0 +1,70 @@
    +        foreach ($variables as $variable) {
    +          call_user_func(static::SYMFONY_VAR_DUMPER_CLASS . '::dump', $variable);
    +        }
    

    This can be a 1 liner with less variables :)

    array_walk($variables, self::SYMFONY_VAR_DUMPER_CLASS . '::dump');
    
lauriii’s picture

StatusFileSize
new7.55 KB
new1.4 KB

This should address #18. Thanks for the reviews! 🙏

mherchel’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new146.01 KB

This looks amazing to me.

Things I tested

  • {{ dump(_context) }}
  • {{ dump(node, content) }}
  • {{ dump(sdfsadfasfsd) }}

I also disabled Twig debug and verified that the output does not show.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed 07994b06fb to 10.1.x and 6c1225de86 to 10.0.x and 6425af2663 to 9.5.x. Thanks!

  • alexpott committed 07994b0 on 10.1.x
    Issue #3306864 by lauriii, mherchel, alexpott: Integrate Twig with...

  • alexpott committed 6c1225d on 10.0.x
    Issue #3306864 by lauriii, mherchel, alexpott: Integrate Twig with...

  • alexpott committed 6425af2 on 9.5.x
    Issue #3306864 by lauriii, mherchel, alexpott: Integrate Twig with...
chi’s picture

I'm a bit late to the party, but don't you think that PHPDoc for the $variables parameter is wrong?

@param array $variables

References:
https://git.drupalcode.org/project/drupal/-/blob/9.5.x/core/modules/upda...
https://git.drupalcode.org/project/drupal/-/blob/9.5.x/core/lib/Drupal/C...

Also we could have native mixed typehint for that parameter.

lauriii’s picture

I think we could fix that in a follow-up 👍 I'm happy to review that so feel free to link that here.

chi’s picture

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.