3

How do I call MaintenanceMode::applies(RouteMatchInterface $route_match) from hook_theme_suggestions_page()?

1
  • 2
    You can chec kdirectly with \Drupal::state()->get('system.maintenance_mode')
    – user21641
    Sep 11, 2017 at 15:32

2 Answers 2

5

From a procedural hook you'll probably need to use something like:

use Drupal\Core\Routing\RouteMatch;

...

$maintenance_mode = \Drupal::service('maintenance_mode');
$route_match = RouteMatch::createFromRequest(\Drupal::request());

if ($maintenance_mode->applies($route_match)) {
  // ...
}
4
  • 1
    thanks, can i ask how do u find from the api doc that this maintenance class for exmaple was instanciable as a service and with this name?
    – Matoeil
    Sep 11, 2017 at 15:18
  • 5
    you can get the list with drupal console : drupal debug:container
    – Kevin
    Sep 11, 2017 at 15:19
  • 2
    I got it from the docs this time round @Matoeil: Provides the default implementation of the maintenance mode service Emphasis mine. From there I knew it was a service, checked for a specific method for it on \Drupal, but there isn't one so the generic \Drupal::service does the job
    – Clive
    Sep 11, 2017 at 15:22
  • 4
    Note that this might not actually be what you want, it for example return FALSE when you are on user/login because that page is excluded from the maintenance mode as you could not log in anymore otherwise). If you just want to know if it is enabled or not, then just check it with \Drupal::state()->get('system.maintenance_mode')
    – Berdir
    Sep 11, 2017 at 16:44
0

In case someone needs a different approach to the accepted answer above. I use this to remove status messages when the site is in maintenance mode.

$maintenance_mode = \Drupal::state()->get('system.maintenance_mode');
  if (!empty($maintenance_mode)) {
  //use can do other stuff here, but in my case I want to remove status message
   \Drupal::messenger()->deleteByType('status');

  }

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.