Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
By Amjad_dokhan on
Hello everyone,
I'm still new to Drupal 8 and I was wondering if some can help me with accessing node values with kint(). I've tried everything. {{ kint() }} gives me the following:
But {{ kint(node.values) }} gives me nothing. I'm still trying to learn more about Drupal 8 theming and the correct use of kint() but that amount of documentation online is very limited. Can somebody please shed some light on how to dig deeper in node arrays using kint ? Thank you all very much in advance.
Comments
To use kint in a twig
To use kint in a twig template, rather than a module, set twig debugging to true in sites/default/services.yml (create the file from default.settings.yml if it does not exist). Add the
{{kint(node)}}{{kint(page.header)}} (or pass in one of the other regions listed in the template's comment) to page.html.twig to get thewhole nodecontents of that region. Clear cache. However, if node.values is not an available variable in the twig template you cannot print it with kint. You cannot get the raw php object in Twig.In node.html.twig you can get the available variables listed in the comment, so for example in that particular template you can do {{kint(content)}} to get the node content. The brief official documentation is at https://www.drupal.org/node/1906780#kint
Video tutorial: https://www.ostraining.com/blog/drupal/devel-kint/
Digit Professionals specialising in Drupal, WordPress & CiviCRM support for publishers in non-profit and related sectors
Hi...
Dear John,
Thanks for your reply. I have already been trying to insert {{ kint(node) }} in the page.html.twig and in the node.html.twig. and I get the same results. The array shows values, fields, etc as protected, and maybe this is why when I insert {{ kint(node) }} it returns NULL. What I don't understand is , I'm planning on building a custom theme and I would like to show nodes in a why different than Drupal does, for instance I want the title in one Div and the subject in another Div far way, then I need to be able to access node values individually, and I don't know how am I supposed to do that if the values are protected and i can't access them. Thank you again for your reply.
Screen Cap
Yes, kint(node) is wrong, as
Yes, kint(node) is wrong, as you will see if check the corrected version of my post, you need to use the template variables listed in the template comment sections.
Digit Professionals specialising in Drupal, WordPress & CiviCRM support for publishers in non-profit and related sectors
Cracked it...
The comment at the top of page.html.twig (and the API docs) say that 'node' is an available variable and it contains a fully loaded node object. If you have the devel module enabled and you do {{ kint() }} in this template you get a full print-out of available variables which includes the node object and all its properties and methods.
However, doing {{ kint( node ) }} in this twig file results in a white-screen-of-death. Assuming something was broken, I was about to look into using hook_preprocess_page to set a node variable myself, but then I stumbled upon this post where someone mentions that {{ node.gettype }} should work, which turns out to be true, woop!
... so methods belonging to the node object are available but trying to run kint() on the object itself or any of its properties causes an error? I would expect this to be mentioned in one of both of the following documentation sections but I haven't seen anything -
https://www.drupal.org/docs/8/theming/twig
https://api.drupal.org/api/drupal/core!modules!system!templates!page.htm...