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.

About The Author

3 thoughts on “How to Print Variables using Devel and Kint in Drupal”

  1. I do this in Drupal 9 in a lot of installations and works fine.
    But in a Drupal 10 i tried with different combinations like:
    Devel 5.1.1, kint-php/kint 4.2 and devel_kint_extras 1.1.0
    Devel 5.0.0, kint-php/kint 3.3 and devel_kint_extras 1.1.0
    And i get never the option Kint Extended in config.
    Has someone tried in Drupal 10?

      1. Andre Angelantoni

        The Kint Extended option wasn’t working for me either in D10 so I went digging and discovered that the latest version’s (1.1.0) CHANGELOG points to this issue:

        Override the default kint dumper
        https://www.drupal.org/project/devel_kint_extras/issues/3309491

        There were drawbacks to adding another plugin so the maintainer changed it so that the new functionality overrides the default Kint plugin.

        There is code in a new install hook to switch a site from the Kint Extended plugin to the Kint plugin, as well.

        Once someone upgrades from Devel Kint Extras 1.0 to 1.1 (on D9 or D10), the Kint Extended option will disappear. From now on, just select the Kint option.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top