Kint output

Kint

Published by Alan Saunders on Sunday, May 16, 2021 - 19:00

Reading Time: 2 minutes

Sections

Title
What is kint

Body

Kint is a really powerful/ useful debugging tool for php, you can read more about here. The purpose of this blog post is to talk about how to use it.

To get kint working for Drupal 8, you need too;

  • Add the kint package using composer.
  • If you're using devel then you should update a setting in devel to use kint.
  • Finally it is a good idea to enable twig debug.

Now this should give us the ability to kint data out in php code and via twig templates.

Title
Adding the kint & devel

Body

Using composer to add the devel and / or kint, you can use the below composer commands, you should check if there updates released for devel and / or kint, that you need to be aware of.

composer require drupal/devel:4.1.1

 

composer require kint-php/kint:^3.3

 

If you added the devel module, which I suggest you do anyhow, then navigate to the websites extend page, find devel and install the module.

Image
Modules page showing modules with the word devel listed
Body

Once devel is installed, you should navigate to devels settings file and change the variables dumper setting to use kint.

Image
Devel modules configuration page

Title
Enable twig debug

Body

For enabling twig debug, you can refer to my earlier blog post here.

Title
Using kint

Body

In twig templates you should now have access to a twig function called kint(), in this function you pass any variable that you wish to output, our example was to output some of the part of an array from a link field showing the link text and url.

{{ kint(content.field_link.0) }}

You also have available a kint() function that can be used in php files too.

kint($variable);

Below screenshot shows an example of the kint output, which is in fact for the link mentioned above.

Image
Screenshot showing example of kint output
Categories:

Related blog posts

Creating custom content entities in Drupal 8/9

Authored by Alan Saunders on Wednesday, March 24, 2021 - 22:00
Reading Time: 7 minutes

Creating a custom theme in Drupal 8/9

Authored by Alan Saunders on Tuesday, April 13, 2021 - 23:00
Reading Time: 7 minutes

Theme debug in Drupal 7/8/9

Authored by Alan Saunders on Saturday, April 17, 2021 - 22:00
Reading Time: 2 minutes