1
votes

in drupal 7 we could use kpr() from devel module or php print_r() and var_dump() functions to print $node object in node.tpl file or hook preprocess node in purpose of debugging and finding a field or ..

but in drupal 8 we can't use php functions in twig and I tried kint() and dump() to print node in twig but no success. ( I've already set debug: true in services.yml ) ( more detail: kint(node) in twig file makes an infinite loop and causes memory size exhausted in browser).

so the question is how to print node object in drupal 8 using twig or hook preprocess node?

Q update v 1.0:

I have custom twig template for article: node--article.html.twig and it works fine:

<article>

<div>
    {{ content.body|render }} {# this works #}
</div>

<footer>
    {{ kint(node.field_custom.value) }} {# prints the custom field value without any problem #}
    {{ kint(node) }} {# this causes infinite loop and memory issue #}
    {{ content }} {# prints all content fields without any problem #}
    {{ kint(content) }} {# nothing happen or display with this! #}
    {{ kint(label) }} {# infinite like node #}
</footer>

3
with the devel kint module installed, {{ kint() }} in a template file works for me. See this page - 2pha
You would have to make sure that the "status messages" block is on the page. - 2pha
@2pha I've updated my question with some examples. I checked the status message and it's active in block list to header region and header region is printed to page--article.html.twig but i don't see any relation between this and kint output. I'm confused what's could be wrong! - Ali_Hr
@2pha ksm() function prints the variable in status messages position because it uses drupal_set_message function - Ali_Hr

3 Answers

2
votes

I knew that the kint() function is not the problem because it displays custom arrays and objects that I created for test. so problem was the node object itself. it was very big and printing it using kint() made memory limit issues. when I changed memory limit to -1 memory_limit= -1 in php.ini file for testing , it took all 16GB ram of my system and it wasn't enough!

so I decreased the depth of kint() function from 7 to 4 in modules/devel/kint/kint/config.default.php ( $_kintSettings['maxLevels'] = 4; ) and memory_limit=128M in php.ini.

now every thing works, hope it help someone.

0
votes

Another option is to use the VarDumper module which isn't quite so memory intensive as Kint. I find it a bit more user-friendly and better-looking!

function YOURTHEME_preprocess_node(&$variables){
  vardumper($variables);
}

Drupal VarDumper example

0
votes

1- install Devel + Twig VarDumper

2- in www.example.com/admin/config/development/devel

enable Display $page array

3- in www.example.com/admin/config/development/devel

enable Symfony var-dumper

4- in twig add

{{ dump() }} {# all #}
{{ dump(attributes) }} {# one #}
{{ dump(_charset) }} {# #}