How do I set the title tag for the maintenance page?
-
01-10-2020 - |
質問
I need to change the default title tag for the maintenance page, which currently is "Site under maintenance | [sitename]".
I tried to set $variables['head_title']
in hook_preprocess_maintenance_page()
but it doesn't work.
Also I think it can be set in hook_preprocess_html()
, but I don't know how to check the served page is the maintenance page.
How can I achieve this?
解決
TEMPLATE_preprocess_maintenance_page
changes only variables for maintenance-page.html.twig. But <title>
is outputted in html.html.twig, so you have to choose another preprocessor template_preprocess_html
. So you can use this code(don't forget to clean cache after it):
function mytheme_preprocess_html(&$variables) {
$maintenance_mode = \Drupal::state()->get('system.maintenance_mode');
if (!empty($maintenance_mode)) {
$variables['head_title'] = (string)t('My Custom Maintenance Title');
}
}
他のヒント
To change this message, you can go Adm -> Config -> Maintenance page and write you own message.
If you really want do this in a preprocess function:
function TEMPLATE_preprocess_maintenance_page(&$variables) {
$variables['head_title'] = "A custom var that YOU going to use in maintenance-page.html.twig";
$variables['title'] = "Default var used to display title message 'Site under maintenance' ";
}
Or you can even override maintenance-page.html.twig
file to put your own message, but its not the best option.