18

I've seen quite a bit of discussion about how kint() from the Devel module is difficult to work with because it maxes out PHP memory limits and makes the browser drag. I've heard some thoughts on improving its speed by limiting the number of levels. For example, in settings.php, you can do the following:

require_once DRUPAL_ROOT . '/modules/contrib/devel/kint/kint/Kint.class.php';
Kint::$maxLevels = 3;

Any other ways to improve the speed of kint()?

1
  • 1
    The performance issue is caused by the fact that D8 is OOP so there is now a shitload of data to display. So I don't see how it can get any better since it's a JS that really render all of that structured data.
    – user21641
    Commented Aug 18, 2016 at 9:36

8 Answers 8

23

There are two other ways other users have mentioned for setting the max output levels in addition to the settings.php method mentioned in the question, plus a couple other ideas below:

  1. Create a /modules/contrib/devel/kint/kint/config.php file and add / modify the line $_kintSettings['maxLevels'] = 3; (by @mdrummond on Slack)

  2. In a preprocess function, add the following: (by @thpoul on Slack)

    kint_require();
    Kint::$maxLevels = 3;
    
  3. Be sure to never click the + sign, that shows the entire tree. Click the block instead. You can click the right arrow to open the output in a new window for easier viewing (like when the output is in a block). (by @cwightrun on Slack)

  4. Instead of using kint(), create a preprocess function and use a debugger to explore the variables. (by @danny_englander on Slack)

  5. Pass kint() a specific variable instead of making it roll through the whole tree (by @No Sssweat below).

  6. If you just need to know what variables exist, you can just view the keys by doing a {% for key, value in _context %} loop (by @mortendk below)

3
  • Should also add Casey's answer from here: drupal.stackexchange.com/a/214713/28813 Commented Sep 27, 2016 at 14:07
  • 1
    "Be sure to never click the + sign, that shows the entire tree" - Thank you! I was clicking the 'plus' sign because, well, its the symbol for expand. Now I know :-)
    – tanc
    Commented Oct 26, 2016 at 17:02
  • kint_require(); Kint::$maxLevels = 3; in your preprocess function is GOLD. Thank you! Allows you to set arbitrary levels as needed. Just using it now to dump stuff with 2 levels and then immediately afterward dump a different object with 5 levels.
    – Will
    Commented Jan 9, 2019 at 8:30
8

Kint is not the only library that you can use for debbuging variables and twig templates.

I suggest to use Symfony var-dumper that IMHO is one of the most powerful lib for debuging variables. Symfony var-dumper is integrated with latest version of devel module (see this issue in the devel issue queue Add a basic symfony var-dumper integration)

For use var-dumper with devel

  • download and install devel module
  • install var-dumper via composer (composer require symfony/var-dumper)
  • go to devel settings page and set var-dumper as the default dumper
  • use {{ devel_dump() }} or {{ kpr() }} or {{ dpm() }} instead of {{ kint() }} for debug variables in twig templates
0
7

FYI, as of Devel v3, you'll need to make two updates if you use the top method suggested by @chrisshattuck

  1. Go to admin/config/development/devel?q=/admin/config/development/devel
  2. Make sure "Kint" is selected.
  3. To override the max depth, go to settings.local.php and update the max_depth variable.
  include_once(DRUPAL_ROOT . './../vendor/kint-php/kint/src/Kint.php');
  if (class_exists('Kint')) {
    Kint::$max_depth = 4;
   }  
1
  • 2
    You only need the class_exists, not the include_once since Kint has a Composer dependency has been autoloaded already.
    – leymannx
    Commented Aug 12, 2020 at 6:14
2

Here is another tip:

Instead of using {{ kint() }} which will return everything, you can be more specific by passing the variable that you need; thus, it will load faster.

For example: In node--[type].html.twig you can pass the content variable to kint:

{{ kint(content) }}

If you know the name of the field, you can be even more specific:

{{ kint(content.field_name['#items'].getValue()) }}

This will only return the value(s) of field name.

2

instead of using kint you can do this little trick instead just to get an idea of whats in there

node.html.twig

<ol>
{% for key, value in _context  %}
<li>{{ key }} </li>
 {% if loop.index == 2 %}}
    <pre>{{ dump( value ) }}</pre>
{% endif %}
{% endfor %}
</ol>

yes it dont gives all the love that kint wants to show you, but at least you get the names etc out

0
0

When it is impossible to get the HTML view or it is too slow to load the kint output.

I will serialize the variable and save it with

\Drupal::logger('custom debug')->notice($string);

Then I will copy it to unserialize.com and choose Krumo display....

1
  • I just want Krumo for D8
    – Felix Eve
    Commented Nov 7, 2017 at 23:16
0

To limit the number of levels from Kint, you go to your local.settings.php or settings.php and use

$_kintSettings['maxLevels'] = 3;
1
-2

Adding $GLOBALS['_kint_settings']['maxLevels'] = 4; in your settings.local.php should do the trick. Feel free to change the number of levels here.

2
  • 3
    This does not work for me in Drupal 8.2, because $GLOBALS['_kint_settings']['maxLevels'] in settings.local.php is overwritten by the default value in /modules/contrib/devel/kint/kint/config.php. Commented Mar 17, 2017 at 8:05
  • Yes config.default.php doesnt make sense it will override anything you put as $GLOBALS, so you can either change it in kint config or delete that value in config and put it as global to make it work.
    – Blissful
    Commented Feb 14, 2019 at 13:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.