Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I've written a patch based off of https://github.com/barelon/CgKintBundle to provide Kint functionality inside Twig templates.
I will upload a patch and a couple screenshots momentarily.
Testing steps
- Enable Kint module
- Apply patch
- Add
{{ kint() }}
to any active Twig template (for example page.html.twig in Bartik) - Clear cache (or
drush cr
)
Visit the homepage and you should see all variables available to the page.html.twig template.
Of course you can pass variable(s) into kint() as well.
Comment | File | Size | Author |
---|---|---|---|
#13 | devel-2218949-8.patch | 5.04 KB | star-szr |
#11 | interdiff.txt | 1.55 KB | star-szr |
#3 | interdiff.txt | 526 bytes | star-szr |
#3 | devel-2218949-3.patch | 5.17 KB | star-szr |
#1 | devel-2218949-kint-node.png | 92.86 KB | star-szr |
Comments
Comment #1
star-szrHere's the patch and screenshots. Feedback welcome.
Example in page.html.twig:
Example in node.html.twig (displays in context!):
Comment #2
star-szrGrammar fix in issue summary.
Comment #3
star-szrRemoved trailing whitespace.
Comment #4
moshe weitzman CreditAttribution: moshe weitzman commentedThis is a big enough piece of code that it is going to need a Maintainer. Would anyone be up for that? It could be the same as the kint module maintainer or someone else.
Comment #5
star-szrI might be up for that once I get the code into a bit better shape.
Comment #6
moshe weitzman CreditAttribution: moshe weitzman commentedCode looks fine. Would be ideal if someone tested this before commit.
Comment #7
moshe weitzman CreditAttribution: moshe weitzman commentedCan be replaced by @\Kint::dump($kint_args);. This did not work with prior version of kint but now that we are on 1.0.0-wip branch it does.
Comment #8
star-szrHm, making that change seems to make it stop working. I pulled the latest 8.x-1.x of devel, rebased my branch and reinstalled D8, everything worked, then I changed all the \Kint to @\Kint and there is no longer any Kint output. Is there something I'm missing?
Comment #9
star-szrHere's the patch and interdiff for testing/comparison.
Comment #10
corbacho CreditAttribution: corbacho commented@Cottser
The @ operator is used by Kint as a "modifier".
http://raveren.github.io/kint
So, there is no need to capture the buffer output, just use the value returned.
Comment #11
star-szrThat makes much more sense, thank you @corbacho!
Here is the change with an interdiff from #3.
Comment #12
moshe weitzman CreditAttribution: moshe weitzman commentedI think you posted the interdiff twice.
Comment #13
star-szrThanks, here's the actual patch.
Comment #15
moshe weitzman CreditAttribution: moshe weitzman commented11: devel-2218949-11.patch queued for re-testing.
Comment #16
moshe weitzman CreditAttribution: moshe weitzman commentedHow can I get this tested again? I am seeing yellow, not green.
Comment #17
star-szrI think the test is postponed because Devel 8.x-1.x-dev HEAD is failing:
https://qa.drupal.org/pifr/test/137984
Comment #18
moshe weitzman CreditAttribution: moshe weitzman commentedCommitted. We should document this somewhere.
Comment #20
Amber Himes MatzThis is great! I think it might make sense to add documentation for this feature on the Debugging variables in Twig templates (https://drupal.org/node/1906780) or else add a page to that section called "Debugging Twig templates using Devel and Kint."
Would that be a good place for this documentation to live? Other suggestions?
Also, FWIW, I am writing up a blog post on debugging Twig in D8 which will be published on the Drupalize.Me blog next Tuesday and I will include a bit about using kint() in a twig template file.
Comment #21
star-szr@agentolivia - thanks! I can't find a handbook section for Devel (at least it's not linked from the project page) so that seems like a good place to start out with the documentation anyway. It can always be moved + linked later.
Comment #22
joelpittetA note for the documentation,
twig_debug
must be turned on in settings.php, or nothing happens. Should be the first sentence IMO. That caught me when trying it out today:)Comment #26
pcambraComment #27
JohnAlbinDid a google search for drupal and kint and landed here. Followed Amber's link and noticed the Theming handbook still doesn't have docs about kint(). So I added them. https://www.drupal.org/node/1906780#kint
dump() is hard to use. kint() is easier.
Comment #28
willzyx CreditAttribution: willzyx commented@JohnAlbin thanks for the doc update! note that you can also use
{{ devel_dump() }}
and{{ devel_message() }}
(and their aliases kpr, dpm, dsm) and take advantage of the pluggable dumper system