Enable debug mode and error reporting for local development in Drupal 8

Enable debug mode and error reporting for local development in Drupal 8

If you are just starting with Drupal 8 then one of the most important things you should know is to enable the debug mode and error reporting on Drupal 8. This is really important for backend as well as frontend developer to the full error on your screen while you are working because you might not know what is the exact issue with the site with just a generic error statement.

This is not going to help :(

The website encountered an unexpected error. Please try again later.

Enable Drupal 8 backend debugging

In Drupal 8 there is a way to backtrace the error and displaying it in the message area. You can put the following code in your settings.local.php file which you can create by copying sites/example.settings.local.php and rename it to sites/default/settings.local.php.

$config['system.logging']['error_level'] = 'verbose';

This makes the development easier on local as it will show the backtrace of the error which you can debug now.

For local Drupal 8 development, you should also enable error reporting, display errors and display startup error to help you further debugging and fixing major runtime error.

error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

Enable Twig debugging in Drupal 8 for frontend guys

If you are a frontend person and works around twig files to beautify the Drupal 8 site then enabling the twig debug configuration is a must. You can enable this by coping sites/default/services.yml file and renaming the new file to development.services.yml. Once you have the development.services.yml file you can just add new twig debug parameter to it to enable the twig debugging.

parameters:
    twig.config:
        debug: true

By enabling the twig config you will be able to find which HTML markup is coming from which twig template file in the backend. With this Drupal 8 debug trick you will also see what are the other twig template suggestions which will override the current template. It will also let you know the theme in which the twig template file is located so that you don't start editing wrong twig file.

Twig debugging in Drupal 8

One more amazing thing Twig debugging does for frontend as well backend developers is to print a variable in a structured format in frontend by using a simple line of code in the Twig file.

{{ dump(my_variable) }}

Drupal 8 local development bonus tips

Whenever you are starting working and project in Drupal 8 make sure to set these settings for your local development environment.

Disable Twig caching for Drupal 8 in development.services.yml file you created earlier.

parameters:
    twig.config:
        debug: true
        auto_reload: true
        cache: false

Disable CSS and JS aggregation on the local environment in Drupal 8 by uncommenting below lines to settings.local.php.

$config['system.performance']['css']['preprocess'] = FALSE;
$config['system.performance']['js']['preprocess'] = FALSE;

Disable Drupal 8 render cache and dynamic page caching by uncommenting below lines to settings.local.php.

$settings['cache']['bins']['render'] = 'cache.backend.null';
$settings['cache']['bins']['dynamic_page_cache'] = 'cache.backend.null';

If you are using Drupal version greater than 8.4 then do add the following line in settings.local.php file.

$settings['cache']['bins']['page'] = 'cache.backend.null';

In settings.local.php file uncomment following line to enable the null cache service in Drupal 8.

$settings['container_yamls'][] = DRUPAL_ROOT . '/sites/development.services.yml';

 

NOTE: Make sure to clear the cache on your Drupal 8 site after all the settings you have done for the local environment development.

WARNING: Make sure to not enable all these setting on production site, unless you need to ;)