Score:0

How can I check if a module is enabled in settings.php?

cm flag

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 is enabled in settings.php?

Context

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.

id flag
It seems like this question is really "How do I use redis as a cache backend at install time?".
Jaypan avatar
de flag
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.
leducvin avatar
cm flag
@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 avatar
cm flag
@Jaypan thank you I was not aware of this module. If it's not possible to do what I want, then that answers that ;)
id flag
There are some tips here: https://docs.platform.sh/guides/drupal9/redis.html
leducvin avatar
cm flag
@cilefen thank you, I will have to dig deeper in platform.sh to understand fully. I think I will answer my question for now and say it cannot be done.
Score:2
cm flag

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.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.