Twig VarDumper

Provides enhanced dump() and vardumper() Twig functions using Symfony VarDumper for better debugging output of PHP variables in Twig templates.

twig_vardumper
6,818 sites
60
drupal.org

Install

Drupal 11, 10, 9 v3.2.0
composer require 'drupal/twig_vardumper:^3.2'

Overview

Twig VarDumper is a developer tool that enhances variable debugging in Twig templates by leveraging Symfony's VarDumper component. It overrides the default Twig dump() function and provides an additional vardumper() function, both of which produce beautifully formatted, interactive output for inspecting PHP variables.

When Twig debug mode is enabled, these functions display variables in a collapsible, color-coded format that is far superior to the standard var_dump() output. This makes it significantly easier to inspect complex data structures such as render arrays, entity objects, and nested arrays during theme development.

The module requires Twig debug mode to be enabled in your development environment. When debug mode is disabled (typically in production), the dump functions return nothing, ensuring no debug output appears on live sites.

Features

  • Provides enhanced dump() Twig function that outputs variables using Symfony VarDumper with pretty, interactive formatting
  • Provides vardumper() Twig function as an alias for the enhanced dump functionality
  • Automatically checks for Twig debug mode and only outputs when debugging is enabled
  • Displays collapsible, color-coded, and interactive variable dumps in the browser
  • Supports dumping the entire Twig context when called without arguments
  • Supports dumping specific variables when passed as arguments
  • Works with any variable type including arrays, objects, render arrays, and entity objects

Use Cases

Debugging Render Arrays

When building or modifying Twig templates, use {{ dump(content) }} to inspect render arrays and understand their structure, available fields, and nested elements before rendering them.

Inspecting Entity Objects

Use {{ dump(node) }} or {{ dump(user) }} to explore entity objects and discover available properties and methods when working with entity templates like node.html.twig or user.html.twig.

Exploring Twig Context

Call {{ dump() }} without arguments to see all variables available in the current template context, which is helpful when working with unfamiliar templates or understanding variable inheritance.

Field Value Inspection

Use {{ dump(content.field_name) }} to inspect field render arrays and understand their structure, making it easier to customize field output in templates.

Debugging Preprocess Variables

After implementing hook_preprocess functions, use vardumper() in templates to verify that custom variables are being passed correctly and contain expected values.

Tips

  • Always disable Twig debug mode in production environments for security and performance reasons
  • Use dump() with specific variables rather than the entire context to avoid overwhelming output
  • Combine with browser developer tools to collapse/expand the VarDumper output for easier navigation
  • Remember that dump() output appears inline where the function is called in the template
  • For complex nested structures, dump individual keys to focus on specific data: {{ dump(content['#node']) }}

Technical Details

Hooks 1
hook_help

Provides help text for the module on the admin help page

Troubleshooting 3
dump() outputs nothing or returns false

Ensure Twig debug mode is enabled. Set twig.config.debug: true in development.services.yml and uncomment the container_yamls line in settings.local.php. Clear all caches after making changes.

Error: symfony/var-dumper component is not installed

Install the VarDumper component via Composer: composer require symfony/var-dumper. If using Drupal with Composer, this should be installed automatically as a dependency.

Installation fails with requirement error

The module checks for the VarDumper library during installation. Ensure you've run composer require drupal/twig_vardumper which will install all dependencies automatically.

Security Notes 3
  • Never enable Twig debug mode on production sites as it can expose sensitive information and degrade performance
  • The module automatically prevents output when debug mode is disabled, providing a safety net
  • Remove or comment out dump() calls before deploying templates to production