Goodbye Krumo, hello Symfony VarDumper

Submitted by Juan on Tue, 05/02/2017 - 16:40
Demonstration code as shown by Symfony's VarDump

Every Drupal 7 developer in ways of expanding its knowledge to work now with Drupal 8 will find out a not so pleasing surprise when working with the Devel module, this essential and powerful tool that simplifies a lot the debugging work of custom modules and themes. That's because in Drupal 7 we got the almighty dsm and dpm functions to query the values on variables, arrays and objects in a single line of code.

Of course these functions still work in the Drupal 8 version of the module, nevertheless its magic ability to display the data in an expansible tree where we can navigate easily is gone. That was possible thanks to Krumo, a useful debugging tool for PHP which now seems to be deprecated as it has not received any update for a long time. Now the people behind Devel decided to replace Krumo with another similar tool called Kint, which is included as a Devel submodule.

Yes folks, we know your intentions were the best but sadly Klimt does not fit the bill. In my experience Klimt as integrated with Devel becomes very slow or even hangs up the entire web browser, even when I tried it on a brand new website with barely a minimal configuration and the most basic Bartik theme. If so, is everything lost already?

The best alternative I have found is to use the Symfony's VarDumper component. We just need to enable it by using Composer on our console:

bobby@fischer /var/www/frankie $ composer require symfony/var-dumper
Using version ^3.2 for symfony/var-dumper
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Removing symfony/var-dumper (v3.2.1)
  - Installing symfony/var-dumper (v3.2.4)
    Downloading: 100%         

> Drupal\Core\Composer\Composer::vendorTestCodeCleanup
Writing lock file
Generating autoload files
> Drupal\Core\Composer\Composer::preAutoloadDump
> Drupal\Core\Composer\Composer::ensureHtaccess
bobby@fischer /var/www/frankie $

After that, we enable VarDumper as the default dumper in the configuration of Devel. To make it, we have to navigate to Configuration > Devel Settings and select the Symfony var-dumper option in the Variables dumper list.

Devel configuration screen

Then we click on the Save configuration button and now we just need to clear the cache through the Drupal web interface or the drush command:

drush cache-rebuild

And voilà! The dsm() command now works just like the Symfony's dump() function, with a better performance and friendly messages to easily debug our code.