How do I call MaintenanceMode::applies(RouteMatchInterface $route_match)
from hook_theme_suggestions_page()
?
2 Answers
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)) {
// ...
}
-
1thanks, 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?– MatoeilSep 11, 2017 at 15:18
-
5
-
2I 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
-
4Note 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')– BerdirSep 11, 2017 at 16:44
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');
}