Debugging in Drupal 8 Vs. Drupal 7: Quick reference

Submitted by John on Fri, 03/22/2019 - 15:14

I keep digging up this same information from some obscure sources, so I thought I would post it here. Now if you have not set up XDebug with a project locally, I highly recommend that, but still, nothing beats a good stack trace. In Drupal 7, I would drop this into settings.php to show errors, regardless of the site settings:

Drupal 7

error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
$conf['error_level'] = 2;

What's more, starting in 7.33, we got access to this option:

$conf['theme_debug'] = true;

Adding this to your settings.php (or similar included file), would enable HTML comments to be added directly within the markup of your code.

<!-- THEME DEBUG -->
<!-- CALL: theme('html') -->
<!-- FILE NAME SUGGESTIONS:
   * html--front.tpl.php
   * html--home.tpl.php
   x html.tpl.php
-->
<!-- BEGIN OUTPUT from 'sites/all/themes/my_module/templates/html.tpl.php' -->

... All of the markup...

<!-- END OUTPUT from 'sites/all/themes/my_module/templates/html.tpl.php' -->

With this new configuration system in Drupal 8, this is now as follows:

Drupal 8

error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
$config['system.logging']['error_level'] = 'verbose';

Drupal 8 also has a theme_debug option so to speak, just add this to sites/default/services.yml.

parameters:
  twig.config:
    debug: true 

Be careful though, as these theme debug options can break your markup in some unsuspecting places. Also, be sure not to commit it! One more thing you should do when debugging Drupal 8, especially to get a good view of your variables in twig templates, is to enable the Kint module, which is now packaged with devel. This will allow you to see a nicely formatted variable output over the dump function.

{{ kint() }}
Another tip is getting the debugger enabled when executing PHP from the command line, this is useful for debugging issues that are run via drush commands in Drupal. This line will enable debugging inside a virtual machine managed by the Forum One web starter tool:

export PHP_IDE_CONFIG="serverName=localhost" && export XDEBUG_CONFIG="idekey=PHPSTORM remote_host=10.11.12.1 remote_port=9000"
Newer versions of web-starter now use Docker, and I'll provide a command for then once I figure it out. Happy debugging!