You’re not short on choice when it comes to debugging a Drupal website.
You can install Devel and use Kint to print variables to the screen. Or you could use Web Profiler to add a toolbar at the bottom of your site to see how things are performing.
If you’re looking for a proper debugger look no further than Xdebug. It integrates with a lot of IDEs and text editors and I’d recommend you try it out if you’ve never used it.
I recorded a webinar about Drupal 8 debugging which you can watch above.
Here is what I covered in the video:
- Turn off caching (02:01)
- Twig debugging (08:26)
- Using Kint (19:25)
- Print variables using Kint in Twig template (21:16)
- Using WebProfiler (22:15)
- WebProfiler IDE link (26:37)
- Drupal console debugging commands (31:41)
- Adding breakpoints to PhpStorm (39:14)
- Adding breakpoints to Twig templates (43:48)
- Drupal integration with PhpStorm (45:26)
- PhpStorm plugins (47:52)
PHP Functions
PHP has two handy functions which can be used to print variables, objects and arrays to the screen.
Drupal core comes with its own function: debug()
.
Devel Module
Devel has been around for as long as I’ve been using Drupal. It comes with a bunch of helper functions for module developers and it has a few handy sub-modules.
The two sub-modules worth mentioning are Kint and Web Profiler.
Kint
This module integrates the Kint library into Drupal and allows you to print variables using the PHP functions: ksm()
and kint()
.
You can also print variables in Twig templates using {{ kint() }}
.
Click here to learn how to use Kint in Drupal 8.
Web Profiler
The Web Profiler sub-module adds a toolbar at the bottom of your site and displays useful stats about the number of queries, memory usage and more.
The toolbar gives valuable insight into what’s happening in your Drupal site.
If you want to learn more about Web Profiler, check out our tutorial on using Web Profiler in Drupal 8.
Drupal Console
Drupal Console is a CLI tool for Drupal. It’s implemented using the Symfony Console component. It can be used to provision new Drupal sites, generate boilerplate code and debug Drupal.
Drupal Console comes with a bunch of debug commands. Just search for any command with the term “debug”.
drupal list | grep "debug"
The two that I found most useful are router:debug and container:debug.
Drupal Settings
Drupal 8 caches a lot more things than Drupal 7. Individual rendered elements like a block for example will be cached. Even if you’re logged in or not.
Rendered Twig templates are also cached. This makes Drupal 8 fast, but it can complicate things when you’re writing code. You don’t want to rebuild the site cache every time you make a change in a template or a rendered array.
Most of this caching can be turned off by disabling them in a settings file.
Drupal.org has a good page: “Disable Drupal 8 caching during development”.
Twig Template Discovery
To turn on Twig debugging, make sure you follow the link above. Then add the following into development.services.yml:
parameters: twig.config: debug: true auto_reload: true cache: false
The debug: true
parameter turns on Twig’s debugging, Twig will display information such as which template it used and its path. It does this my adding HTML comments.
Use the HTML comments to figure out which Twig template you should override and its file name.
PhpStorm
PhpStorm is a commercial IDE which is popular with PHP and Drupal developers. It integrates nicely with Xdebug and the Drupal code base.
Xdebug
Using PhpStorm, you can add a breakpoint somewhere in PHP code and step through as the Drupal request is executed. You can also see what variables are available.
Learn how to configure Xdebug in PhpStorm.
Drupal Integration
PhpStorm also offers Drupal integration and when enabled it allows autocomplete functionality for hooks. No longer will you have to remember a specific hook and its arguments.
Make sure you turn on the integration by searching for “drupal” in the Preferences section.
Extra PhpStorm Plugins
Drupal 8 uses the YAML format for a lot of things throughout its code base; services, routing, permissions, etc…
And in these files you’ll see references to classes and methods.
Take for example this route:
node.add: path: '/node/add/{node_type}' defaults: _controller: 'DrupalnodeControllerNodeController::add' _title_callback: 'DrupalnodeControllerNodeController::addPageTitle'
There’s no easy way to navigate to the NodeController other than searching for the class name.
However, if you install these three PhpStorm plugins, you’ll be able to navigate to the class or method by pressing command or control then clicking on the reference.
Once you’ve downloaded these plugins, enable them and then enable “Symfony integration”.
Then you should be able to navigate to classes by clicking on the reference while pressing command or control.
Summary
As you can see, you have options when it comes to debugging. If you’re looking for something simple then use Kint. If you prefer a proper debugger then look at Xdebug.
What’s your preferred technique? Leave a comment below.
As always nice and useful article Ivan, thanks for share.
Thanks.
In a typical Drupal 8 installation “symfony/var-dumper” is available and it provides the handy “dump()” function which works for console and web output.
Also, when using “dump()” or “debug()” I usually assemble an array of all the info I need, using keys as labels, and then output only once per function.
Ciao,
Antonio
Hi Ciao,
Thanks for the tip. Didn’t know about VarDumper.
I added a link to the tutorial.
Cheers,
Ivan
If you want to see what’s inside an entity — they are hard to dump — check $entity->toArray()
Hi Chx,
Thanks for the tip. I didn’t know that.
Cheers,
Ivan
Thanks for your tutorial! It shows how to be productive with drupal 8
Thanks.
I didn’t know about those plugins for PhpStorm. Definitely installing those right away! Thanks for the write up.
Thanks.
Hi Ivan, thanks for that post. I’ve built a docker image for easy installation of a “debugable” drupal 8. Maybe your are interested? It is here: https://hub.docker.com/r/feikede/drupal-dev/
Gives you a debugable D8 in about 30 Secs. I’d like to hear what you think about it.
Regards, Rainer
Hi Rainer,
Thanks, I’ll check it out.
Hi Ivan,
How to print form array inside form_alter using kint(); ?
Thanks
Hi,
Have you tried
ksm();
?If so, what problem are you having?
Cheers,
Ivan
Hello Ivan,
Thanks for the video, it help me a lot.
I notice that you using phpstorm ide link to connect the event and file.
How can I achieve the same effect using vscode ?
Thanks.
Hi Victor,
A user on YouTube commented and said that “vscode://file/@file:@line” works for vscode.
I haven’t tested it.
Cheers,
Ivan
Thanks. I already test the IDE link you provided. It worked perfectly.
There’s a useful module Search Kint. You can search items in output
https://www.drupal.org/project/search_kint
Hi Victor,
Thanks for the module.
Cheers,
Ivan