Ali_Hr
Ali_Hr

Reputation: 4655

how to print node object in drupal 8

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>

Upvotes: 1

Views: 3952

Answers (3)

sami
sami

Reputation: 1

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) }} {# #}

Upvotes: 0

featherbelly
featherbelly

Reputation: 954

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

Upvotes: 0

Ali_Hr
Ali_Hr

Reputation: 4655

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.

Upvotes: 2

Related Questions