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'] = ERROR_REPORTING_DISPLAY_ALL;

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!