I've read How can I check if a module is enabled? and How can we use \Drupal class inside Settings.php and came to the conclusion that I cannot use the module_handler service in settings.php.

How can I check if a module -- in this case the redis module -- is enabled in settings.php?


I am using docker for a Drupal 9.4 installation. When I initialize a new container from the image, and I install Drupal (initialize a new database), the redis module isn't initially activated, although present.

This means I cannot initially specify $settings['cache']['default'] = 'cache.backend.redis'; in settings.php (it will make the system crash).

I would have like to do

$moduleHandler = \Drupal::service('module_handler');
if ($moduleHandler->moduleExists('redis')) {
  $settings['cache']['default'] = 'cache.backend.redis';

Of course this doesn't work and I understand why. But I would like to avoid going into my container to edit settings.php after activating the redis module every time I start a new container.

  • It seems like this question is really "How do I use redis as a cache backend at install time?".
    – mona lisa
    Commented Nov 28, 2022 at 15:28
  • 2
    You cannot check if modules are enabled in settings.php, as Drupal has not been bootstrapped when this file is loaded. Usually if you want a module like Redis to be installed on some environments and not others, you use the Config Split module.
    – Jaypan
    Commented Nov 28, 2022 at 15:31
  • @cilefen It could be it. But, it would be nice if the site would function if the redis module is disabled without the need for editing settings.php again. And I think in general the answer could prove useful for other cache backends (or even other modules).
    – leducvin
    Commented Nov 28, 2022 at 15:32
  • @Jaypan thank you I was not aware of this module. If it's not possible to do what I want, then that answers that ;)
    – leducvin
    Commented Nov 28, 2022 at 15:36
  • There are some tips here: docs.platform.sh/guides/drupal9/redis.html
    – mona lisa
    Commented Nov 28, 2022 at 15:53

2 Answers 2


The answer to this precise question is now part of the Redis module:

use Drupal\Core\Installer\InstallerKernel;
if (!InstallerKernel::installationAttempted() && extension_loaded('redis')) {
  $settings['cache']['default'] = 'cache.backend.redis';
  • I'm going to accept this answer at it seems now possible to do it (but I haven't tried it yet).
    – leducvin
    Commented Mar 25 at 15:59

For the same reasons mentioned in How can we use \Drupal class inside Settings.php, namely that the Drupal bootstrap process hasn't completed yet, this cannot be done.

see @Jaypan's comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.