Drupal 8 - Tip: Enable debug mode for development

By Tom Sat, 03/12/2016 - 22:42

In Drupal 8 by default it already set to caching power mode. This is very important for your site performance. Hoever when you are working on development stage, this caching mode is recommend disable. Follow step by step to disable caching mode in Drupal 8

Disable render caching and JavaScript/CSS aggregation

  • Edit your settings.php file

    Edit your sites/default/settings.php file and un-comment the code that includes an optional settings.local.php file. Make sure this code is at the bottom of your settings.php file so that local settings can override default settings.

    The code at the bottom of your sites/default/settings.php file should look like this:

    
    if (file_exists(__DIR__ . '/settings.local.php')) {
        include __DIR__ . '/settings.local.php';
      }
    
    
  • Copy example.settings.local.php
    Copy copy sites/example.settings.local.php to sites/default/settings.local.php, and clear the cache.
  • Use settings.local.php
    Drupal will now locate your sites/default/settings.local.php file, if it exists, when sites/default/settings.php is loaded. Since the local settings file is loaded last, any variables set there will override settings in the default file.
  • Use development.services.yml
    Using the sites/default/settings.local.php file will also include and use sites/development.services.yml.
  • Modify debugging configuration
    Take a look at the variables in sites/default/settings.local.php, specifically:
    
    $config['system.performance']['css']['preprocess'] = FALSE;
      $config['system.performance']['js']['preprocess'] = FALSE;
    
    
    
    

    —which disables the CSS and JavaScript aggregation features, and:

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

    This line disables Drupal's Render API caching by telling Drupal to use the cache.backend.null cache service instead of the default. This new service is defined in the sites/development.services.yml and essentially returns a MISS for every cache request, thus bypassing the Render API cache.

 

Enable Twig debugging options

Enabling Twig debugging involves locating the twig.config[debug] settings in your services.yml file and changing their values.

These variables can be edited either directly in sites/default/services.yml, or alternately they can be added to the sites/development.services.yml file if you followed the steps above to use a settings.local.php file.

Edit the following variables under the twig.config: section:

debug: true
auto_reload: true
cache: false

If you're placing this into your sites/development.services.yml file, the complete YAML entry looks like this:

# Enable Twig debugging.
parameters:
 twig.config:
   debug: true
   auto_reload: true
   cache: false

 

This is what each of those settings is used for:

debug
(boolean) Enable various debugging features in the Twig engine.
auto_reload
(boolean) When set to true, Twig will automatically recompile all templates when their source code changes.
cache
(boolean) Disabling the Twig cache by setting this to false will recompile the templates from source each time they are used. In most cases the auto_reload setting above should be enabled rather than disabling the Twig cache.

With Twig debugging enabled, changes you make to your theme should show up the first time you refresh the page. In addition, you can learn about theme hook suggestions and inspecting variables in a template file to see what you can do with the new debugging tools.

Note: This can also be done via the Drupal console command: drupal site:mode dev. Running this command will toggle all the Twig settings outlined above to their debugging state. However, this will make changes directly to your sites/default/services.yml file, and not the development.services.yml which is arguably a better place for these settings.

 

#Original source: Drupalize