Given a nid X, how do I have Drupal render it as the content for the maintenance page?
-
4A maintenance page needs to be available when the database isn't available. So basing it on anything that comes from the database, like an entity, probably isn't going to work out– Clive ♦May 24, 2021 at 16:42
-
@Clive Yeah, that's certainly a valid point. In this case the only concern we really have though is the maintenance page for during a site upgrade, not during "catastrophic failure". I think my first attempt at solving both issues would be to cache the maintenance page in varnish/nginx and return that if hitting a 5xx.– LetharionMay 24, 2021 at 17:20
2 Answers
Use the Twig tweak module and create an overwrite template for maintenance-page.html.twig
. Then copy & paste stuff from your base theme, but replace {{ page.content }}
from the base theme with {{ drupal_entity('node', X) }}
E.g. for stable 9 theme this would look like
<header role="banner">
{% if logo %}
<a href="{{ front_page }}" title="{{ 'Home'|t }}" rel="home">
<img src="{{ logo }}" alt="{{ 'Home'|t }}"/>
</a>
{% endif %}
{% if site_name or site_slogan %}
{% if site_name %}
<h1>
<a href="{{ front_page }}" title="{{ 'Home'|t }}" rel="home">{{ site_name }}</a>
</h1>
{% endif %}
{% if site_slogan %}
<div>{{ site_slogan }}</div>
{% endif %}
{% endif %}
</header>
<main role="main">
{% if title %}
<h1>{{ title }}</h1>
{% endif %}
{{ page.highlighted }}
{# disabled {page.content} here #}
{{ drupal_entity('node', X) }}
</main>
{% if page.sidebar_first %}
<aside role="complementary">
{{ page.sidebar_first }}
</aside>
{% endif %}
{% if page.sidebar_second %}
<aside role="complementary">
{{ page.sidebar_second }}
</aside>
{% endif %}
{% if page.footer %}
<footer role="contentinfo">
{{ page.footer }}
</footer>
{% endif %}
-
This looks really promising, but is there a way to do it without adding Twig tweak? Like could I pass in the node as an argument in the preprocessor? May 24, 2021 at 17:12
-
Just google
drupal programmatically render node
, you'll find a ton of answers here on this site.– HudriMay 24, 2021 at 17:15 -
Apart from that, Twig Tweak really is a very well-maintained, wideley used - and IMHO must-have - module. This is no big dependency, see it as a collection of code recipes.– HudriMay 24, 2021 at 17:18
In order to customize the Maintenace Page in Drupal 8/9 We need to do following items but not restricted to:
Create a Basic Page or Article in Drupal for Maintenance page and keep its node id in handy
Update settings.php to include your custom theme machine name in the config value like: $settings['maintenance_theme'] = 'your_theme_machine_name';
Copy maintenance-page.html.twig from batrik theme or core/modules/system/templates/maintenance-page.html.twig and keep at your_theme_name/templates/system/maintenance-page.html.twig
Update your maintenance-page.html.twig as in below snippet to embed the content you have in place from Article/Basic page in 1st step
{# /** * @file * Bartik's theme implementation to display a single Drupal page while offline. * * All available variables are mirrored in page.html.twig. * * @see template_preprocess_maintenance_page() */ #} <header role="banner"> {% if logo %} <a href="{{ front_page }}" title="{{ 'Home'|t }}" rel="home"> <img src="{{ logo }}" alt="{{ 'Home'|t }}"/> </a> {% endif %} </header> <article{{ attributes }}> {% block article_title %} <div class="container"> <div class="row"> <div class="col-12 col-xs-12"> <div class="s-inner-content"> {# disabled {page.content} here #} {# render body field from Article or Basic page with specific node id #} {{ drupal_field('body', 'node', nid) }} </div> </div> </div> </div> {% endblock %} </article>
In order to have some static page like maintenance-page--ofline.html.twig in drupal 8/9 as in Drupal 7 when Database is down we need to refer to the ongoing discussion on issues: https://www.drupal.org/project/drupal/issues/2720109
We can also refer existing discussion: Drupal 8 maintenance page theming