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

Events and Event Subscribers in Drupal 8 and 9

Authored by Alan Saunders on Wednesday, June 30, 2021 - 10:00
Reading Time: 9 minutes

Config vs State in Drupal 8/9

Authored by Alan Saunders on Saturday, March 6, 2021 - 22:00
Reading Time: 5 minutes

Building a custom page in Drupal 8/9

Authored by Alan Saunders on Sunday, December 27, 2020 - 10:00
Reading Time: 12 minutes