How to Print Variables using Kint in Drupal 8

While developing a module or modifying a template in Drupal you’ll often print variables, especially if you’re in a preprocess hook.

You learn early on how to use var_dump and print_r function. But these functions can sometimes display too much information and can be hard to filter through the arrays or methods in the variable.

In Drupal 7, with the Devel module, you could use the dpm or dsm function. When used, these functions will print variables at the top of the page or in the message area using Krumo.

Now in Drupal 8, Devel has adopted a new library to print variables and it’s called Kint.

Fig 1.0

Please note, Krumo has been removed from the Drupal 8 version of Devel. The dpm and dsm functions are still there but the variables are printed without formatting.

Fig 1.0

Getting Started

Kint ships as a sub-module in Devel and the library itself is also in the sub-module. So all you need to do is download and install it.

Below are the Drush and Drupal Console commands to download and install:

Using Drush:

$ drush dl devel
$ drush en kint

Or using Drupal Console:

$ drupal module:download devel --latest
$ drupal module:install kint

Using Kint

As mentioned earlier, Kint comes as a sub-module so it hasn’t replaced the good-old beloved dpm and dsm, these functions are still there.

Instead, you get two new ones: kint and ksm.


Fig 1.0

The kint function prints everything at the top of the page.


Fig 1.0

The ksm function prints the Kint output in the message region of your theme.

Expanding and Collapsing

Once you have a variable printed on the screen you 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.

Fig 1.0

Viewing Stack Trace

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

Fig 1.0

Using Kint in Twig

Kint can also be used in Twig templates. To print a variable, 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 to learn how to do it.

If you’re using the 8.x-1.0-alpha1 version of Devel, then you’ll get a PHP warning when using Kint in a Twig template. This has been fixed in the latest dev version.


Kint is a good upgrade for the Devel module. The only drawback is you’ll have to retrain your muscle memory to type kint or ksm instead of the old functions.


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

No, 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 to learn how.

34 thoughts on “How to Print Variables using Kint in Drupal 8”

  1. Hello Ivan,

    First of all I thank you for your tutorials, I find them very interesting and very helpful too. It’s a great job!!

    IMHO Kint is too slow printing objects. When I have used it I have recived a WSOD or may be the duration of loading becomes eternal. I don’t know exactly why.
    Have you had problems with the performance?
    Can it be a problem with XDebug?
    Have you ever installed and tested XHProf with Drupal 8?

    Thank you in advance for your answers.

    1. I’ve never had performance problems with it. If you’re getting a WSOD, find out what the error is. You could be running out of memory.

      Yes, XDebug does slows things down, try disabling it and see if it fixes it.

  2. How to access kint array second variable and protected variable and print sum values of the fields on twig template .

    i understand how to work kint but need more suggestions to print variable like {{ pagecontent[‘#colossal_menu_link’].contents.field_main_menu_caption.value) }} how to .

  3. Thanks for the nice tutorial! kint(content) now tells me what are available arrays for user login block. By using {{ content.user_login_form }} I now can display the login form only and not the user_links. But I want to go one step forward by extracting specific fields in user_login_form array element, i.e. if I want to only display the username field what should I do? content.user_login_form.username or something? I could not figure that out from kint data.

  4. Hi Ivan, Good video

    I´m trying to use your guide to generate my attempt to generate my own D8 Entity Print PDF output but are struggling as the {{ kint (content) }} output is listing the defined content like title ,but its is a Protected Value.

    How can I get a step further as i´m really stucked

  5. Very helpful. I seem to find myself in the following situation frequently:
    I’ll be working on a specific template (field.html.twig), in order to change the behavior, add some class, etc. but I need to know something from the parent element.
    In other words, I’m way down at the field level, but need to know what the content type is so that I only run my code for a particular content type. Is there a higher level var name that I can run kint against? (I’ve tried kint(args), kint(item), etc. and they are all coming back null.)

  6. Hi Ivan,
    I show a some rows produced by a view via a twig-file.
    The view contain 3 blocks
    I try to discover the name of such a block in my twig file.
    Adding a row class in the format-settings of my block does not work because the are copied to all the other blocks in my view.
    Do you have a suggestion?

      1. Hello Ivan
        I use a custom twigtemplate. This template receives content “{% for row in rows %}” from a view.
        Depending on which block of the view that generates the row, i would like to alter the representation.

  7. Thanks for this article. I’m trying to output the value of a block field called “field_link”. If I add {{ kint(content) }, I see an enormous array and see an element called [‘_field_layout’], then one called [‘first’], then under that, a “field_link”. I tried then to output {{ kint(content[‘_field_layout’]) }, cleared cache, then returned to the page, and it will only load the drupal installation page. I have to completely remove the kint statement and clear cache to get my page back. What could I be doing wrong?

  8. Drupal 8.9.0 Devel 8.x-2.1 In devel 8.x-3.0-beta1 there is no KINT submodule

    Hi Ivan, I followed your tutorial but I am in trouble. when I enter the kint ($ variables) command; in the bartik theme this error comes back to me
    $ args array (1)
    → Called from /test/modules/devel/kint/kint.module:18 [kint ()]
    The website encountered an unexpected error. Please try again later.
    The same if I enter the command in a custom form

  9. Sorry ignorance, where I activate the error handling

    I have activated devel and set the page view in devel settings and it seems to work, but it does not show me all the fields

  10. Sorry if this is somewhat off the orig topic…. I love Kint after finding it here to use with my regular PHP applications… I am having a strange problem. When I call Kint it outputs the data in my variable or object, etc… but it does not include the object name itself… all objects are named $0 instead of the variable or array name that I called it with. So if I issue d($myarray); the kint output shows: array (6) instead of the name of the array and its length like: $myarray array(6)
    as I expect it to.

    any ideas whats happening?

  11. FYI – With the release of D8/9 Devel 4.0, there is no longer a kint submodule. Instead you need to use composer to download the kint library to enable it. E.g. composer require kint-php/kint See 4.0 release notes for more info.

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.