How to Print Variables using Devel and Kint in Drupal

As a Drupal developer, you will often want to inspect variables in your modules or themes to view the actual values. PHP has default functions such as var_dump() and print_r() that will print all the information but it’s not very intuitive nor printed in an easy to understand way. In most cases, it prints too much information and it can be time-consuming to find the variable you actually want by filtering through all the arrays and methods.

Using Devel and the Devel Kint Extras modules, you can print variables in a more user-friendly way.

This tutorial will walk through how to set up these modules so you can print variables in PHP and Twig using Kint.

Getting Started

To get started go and download the Devel and Devel Kint Extras module.

Devel Kint Extras ships with the kint-php library so installing this via Composer will take care of it automatically.

We can install both using Composer:

composer require drupal/devel drupal/devel_kint_extras

We can now enable both devel and devel_kint_extras with just one Drush command:

drush en devel_kint_extras -y

Finally, we have to enable Kint Extended as the Variables Dumper. To do this go to:

admin/config/development/devel

and select Kint Extender and Save the configuration as shown below:

Printing variables using Kint in PHP

Devel comes with the Kint functions kint() and ksm() so you can use this to print any variable in your custom module.

For e.g. Here we have used kint() in our custom module:

function custom_kint_preprocess_page(&$variables) {
 kint($variables['page']);
}

The output will look like this:

NOTE: You may have to clear the site cache to see the output.

And if we used the ksm function instead of kint() the output will look like this:

As you can see, the output was printed to the Messages region of the theme. “ksm” is likely the abbreviation for “kint set message”.

Expanding and Collapsing

Once you have a variable printed on the screen, you will want to drill down and see what’s in it.

Kint allows you to navigate around in two ways. First, if you click anywhere on the row, it’ll expand just the next level.

But if you click on the + icon, it’ll expand all child items below.

Have a look at the GIF below to see how it works:

Viewing the Stack Trace

Kint also displays a stack trace just below the output when viewing a printed variable. Click on the + icon below the variable and it’ll expand a stack trace.

Printing variables using Kint in Twig

Kint can also be used in Twig templates. To print a variables, just add {{ kint() }} into the template and pass a variable in, for example, {{ kint(page) }}.

For this to work, you must turn on Twig debugging. I won’t cover it in this tutorial, but read this documentation page on drupal.org to learn how to do it.

Summary

In this tutorial, we showed you how you can use the Devel and the Devel Kint Extras modules to print variables within your custom module (PHP) and also in your theme (Twig).

Using Kint to print variables is a more user friendly way for Drupal developers to inspect the value of variables. These simple tools can be used to increase the productivity of your development.

FAQs

Q: Has dpm() and dsm() been removed?

These functions are still there but they won’t print the variable using Krumo as it has been removed from Devel in Drupal 8.

Q: I’m trying to print a variable in a Twig template and nothing is happening.

You must turn on Twig debugging for the {{ kint() }} function to render. Read this page on drupal.org to learn how.

Q: Can’t I just use Xdebug?

Yes, you can and I would recommend that you set up Xdebug. However, sometimes Xdebug isn’t available so in that case use Devel and Devel Kint Extra to quickly inspect variables.

Leave a Comment

You have to agree to the comment policy.

This site uses Akismet to reduce spam. Learn how your comment data is processed.