Drupal 8 module debugging: taming Kint's output

Published September 22, 2018

Debugging a Drupal 8 module can take many forms. Often, one of the first tools most developers use is the ability to output variables using the "Devel Kint" module (part of the Devel project). Much like the dsm() function from pre-Drupal 8 versions of Drupal core, the ksm() function provided by Devel Kint provides a slick way to output any variable type to the screen in a readable way. 

Unfortunately, many folks find Kint output to be a bit more cumbersome, and they're right, but Kint is doing a whole lot more than dsm() ever did. In addition to outputting just the variable's values, if said variable is an object, then Kint also outputs that object's methods and other valuable debugging information. Unfortunately, all that information comes with a cost - it tends to really bog down web browsers.

I was finding that some large variables in Drupal core ($form and $node, I'm looking at you) could even crash my browser (out of memory) due to the sheer depth of information returned by a simple ksm(). Out of desperation, I searched for a way to tame Kint a bit so that I could go about my debugging business.

Luckily, I stumbled upon this gist by Julian Pustkuchen which demonstrated how to limit the number of levels returned by a ksm() call. This solved my problem - no more browser out-of-memory issues. The downside is that sometimes a ksm($form) call doesn't get me deep enough into the array, so I have to be a bit more specific when ksm-ing. 

I took Julian's gist and dropped it in my settings.local.php file as follows:

include_once(DRUPAL_ROOT . '/modules/contrib/devel/kint/kint/Kint.class.php');
if(class_exists('Kint')){
  // Set the maxlevels to prevent out-of-memory. Currently there doesn't seem to be a cleaner way to set this:
  Kint::$maxLevels = 4;
}

 

Sign up to receive email notifications of whenever we publish a new blog post or quicktip!

Name
CAPTCHA