6

In Drupal 7, we could override the $conf variables in settings.php to force a site (e.g. development) in maintenance mode. In Drupal 8, this has changed to $config, but the maintenance mode variable is not part of the configuration anymore. How can I force system.maintenance_mode in a specific environment's configuration?

5
  • 2
    It's in state now, normally you'd set that with \Drupal::state()->set('system.maintenance_mode', TRUE); but there doesn't seem to be a direct way to override state from settings.php
    – Clive
    Feb 25, 2017 at 2:12
  • Thanks. Indeed that call doesn't work from within settings.php
    – bohemier
    Feb 25, 2017 at 13:07
  • Yeah the service container hasn't been initialised by that point so services aren't available :/
    – Clive
    Feb 25, 2017 at 20:33
  • Is there an alternative way of doing this?
    – bohemier
    Feb 26, 2017 at 13:03
  • There are any number of hacks (e.g. stuff a var into config, read it back in an early page build hook, set state var based on it) but I don't know what the right way to do it is, "right way" as in plays nicely with cache, doesn't cause problems with random unknown stuff being inside config, etc
    – Clive
    Feb 26, 2017 at 13:06

1 Answer 1

2

I think you might want to take a different approach.

The maintenance mode is now a state in Drupal 8, not a configuration. The difference is a state is a ephemeral state that's specific to the current environment (see the state API docs for more info).

As such, you might want to replace the State service with your own service class that uses the StateInterface interface if you're looking to fundamentally alter the way state behavior (which could lead to unforeseen problems).

Another tact might involve some sort one-time toggling of state (e.g. programmatically as shown above, a drush state-set / drupal state:override) based on some event (e.g. deployment of site).

1
  • Thanks for the suggestions. A one time toggle during deployment is an alternative but it does not enforce maintenance mode at all times... for instance someone with proper privileges could turn off maintenance mode in the interface. As Shawn Conn mentions, replacing the State service seems a bit risky. I've updated the question title to better reflect what I'm trying to achieve.
    – bohemier
    Mar 2, 2017 at 15:11

Your Answer

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

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