Configuration locale et Twig debugging dans Drupal 8
Le cache est omniprésent dans Drupal 8 et s’il est nécessaire dans un environnement de recette et de production, il se révèle peu utile, voire gênant dans un environnement de développement. Voici la procédure afin de configurer Drupal 8 localement pour le développement sans altérer les paramètres utilisés en production.
Attention toutefois à rétablir le paramétrage de cache pour vos tests finaux pour éviter des surprises désagréables lors de la mise en recette.
Voici la procédure pas à pas :
1. Copier le fichier example.settings.local.php qui se trouve dans /web/sites/ et renommé le settings.local.php.
2. Ouvrez le fichier settings.local.php et décommentez la ligne concernant le cache.
$settings['cache']['bins']['render'] = 'cache.backend.null';
3. Configurer le fichier settings.php qui se trouve dans /web/sites/default/ pour prendre en charge le fichier local en décommentant les lignes en fin de fichier
# if (file_exists($app_root . '/' . $site_path . '/settings.local.php')) {
# include $app_root . '/' . $site_path . '/settings.local.php';
# }
4. Vider tous les caches Drupal
5. Ouvrez le fichier developpement.services.yml qui se trouve dans /web/sites/ et copier dedans les lignes relatives à la gestion de Twig que vous trouverez dans le fichier default.services.yml qui se trouve dans /web/sites/default/
parameters:
session.storage.options:
# Default ini options for sessions.
#
# Some distributions of Linux (most notably Debian) ship their PHP
# installations with garbage collection (gc) disabled. Since Drupal depends
# on PHP's garbage collection for clearing sessions, ensure that garbage
# collection occurs by using the most common settings.
# @default 1
gc_probability: 1
# @default 100
gc_divisor: 100
#
# Set session lifetime (in seconds), i.e. the time from the user's last
# visit to the active session may be deleted by the session garbage
# collector. When a session is deleted, authenticated users are logged out,
# and the contents of the user's $_SESSION variable is discarded.
# @default 200000
gc_maxlifetime: 200000
#
# Set session cookie lifetime (in seconds), i.e. the time from the session
# is created to the cookie expires, i.e. when the browser is expected to
# discard the cookie. The value 0 means "until the browser is closed".
# @default 2000000
cookie_lifetime: 2000000
#
# Drupal automatically generates a unique session cookie name based on the
# full domain name used to access the site. This mechanism is sufficient
# for most use-cases, including multi-site deployments. However, if it is
# desired that a session can be reused across different subdomains, the
# cookie domain needs to be set to the shared base domain. Doing so assures
# that users remain logged in as they cross between various subdomains.
# To maximize compatibility and normalize the behavior across user agents,
# the cookie domain should start with a dot.
#
# @default none
# cookie_domain: '.example.com'
#
twig.config:
# Twig debugging:
#
# When debugging is enabled:
# - The markup of each Twig template is surrounded by HTML comments that
# contain theming information, such as template file name suggestions.
# - Note that this debugging markup will cause automated tests that directly
# check rendered HTML to fail. When running automated tests, 'debug'
# should be set to FALSE.
# - The dump() function can be used in Twig templates to output information
# about template variables.
# - Twig templates are automatically recompiled whenever the source code
# changes (see auto_reload below).
#
# For more information about debugging Twig templates, see
# https://www.drupal.org/node/1906392.
#
# Not recommended in production environments
# @default false
debug: false
# Twig auto-reload:
#
# Automatically recompile Twig templates whenever the source code changes.
# If you don't provide a value for auto_reload, it will be determined
# based on the value of debug.
#
# Not recommended in production environments
# @default null
auto_reload: null
# Twig cache:
#
# By default, Twig templates will be compiled and stored in the filesystem
# to increase performance. Disabling the Twig cache 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.
#
# Not recommended in production environments
# @default true
cache: true
et de changer ensuite les paramètres en :
debug: true
auto_reload: true
cache : false
6. Vider les caches
Vous aurez ainsi une version de Drupal 8 propre au développement et au débuggage de Twig.
Si lors de la procédure, vous rencontrez des erreurs en vidant les caches, pensez à effectuer un rebuild en utilisant le lien /rebuild.php