Drupal Development and Consultation - Proudly operating in The United States of America

Optimize Drupal for Local Development

By Xandermar LLC, October 13, 2021

Community best practices on how to prepare your local environment for Drupal development.

Debugging Twig templates

Enable debugging

You enable Twig Debugging in sites/default/services.yml.

Set the debug variable to true. And clear cache.

parameters:
  twig.config:
    debug: true 
  • If services.yml does not yet exist; copy default.services.yml to services.yml.
  • If Drupal has already been installed, permissions on the sites/default directory may need to be temporarily changed to allow write access.
  • How to change directory permissions
  • Once services.yml has been created and edited, change permissions back to lock down the sites/default directory.

To verify that Drupal is getting the twig.config parameter set as expected, run:

drush php:eval "var_export(\Drupal::getContainer()->getParameter('twig.config'));"

Source: https://www.drupal.org/docs/theming-drupal/twig-in-drupal/debugging-twig-templates

Disable Drupal (>=8.0) caching during development

Copy sites/example.settings.local.php to sites/default/settings.local.php

$ cp sites/example.settings.local.php sites/default/settings.local.php

Open settings.php file in sites/default and uncomment these lines:

if (file_exists($app_root . '/' . $site_path . '/settings.local.php')) {
  include $app_root . '/' . $site_path . '/settings.local.php';
}

Configure development.services.yml

The development.services.yml file is located under /sites.

Your final development.services.yml should look as follows (mind the indentation):

# Local development services.
#
# To activate this feature, follow the instructions at the top of the
# 'example.settings.local.php' file, which sits next to this file.
parameters:
  http.response.debug_cacheability_headers: true
  twig.config:
    debug: true
    auto_reload: true
    cache: false
services:
  cache.backend.null:
    class: Drupal\Core\Cache\NullBackendFactory

Open settings.local.php and make sure development.services.yml is enabled.

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

Configure settings.local.php

Change the following to be TRUE if you want to work with enabled css- and js-aggregation:

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

Uncomment these lines to disable the render cache and disable dynamic page cache:

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

Add the following lines

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

If you do not want to install test modules and themes, set the following to FALSE:

$settings['extension_discovery_scan_tests'] = FALSE;

Rebuild cache

Rebuild the Drupal cache otherwise your website will encounter an unexpected error on page reload. This can be done by with drush:

drush cr

Source: https://www.drupal.org/node/2598914

Tags

Comments