When rendering HTML in hook_node_presave()
, everything works as expected when I save the node manually.
However, when I use the "Save content" action on the content admin view (at /admin/content), then the page is rendered with the admin theme templates (Seven), instead of the templates of my custom theme/the site theme.
I tried using both Claro and Seven as the admin theme, and I get the same result: I can save the page manually and the site theme's templates are used, but if I use the "Save content" action to bulk re-save nodes, they are rendered incorrectly with the admin theme templates.
Here's the code that is causing the issue:
function MYMODULE_node_presave(NodeInterface $node) {
$content_type = $node->getType();
if ($content_type == 'page') {
// Render the entire node and save that HTML to a specific field.
$render_controller = \Drupal::entityTypeManager()
->getViewBuilder($node->getEntityTypeId());
$render_output = $render_controller->view($node, 'mymarkup_torender', 'ja');
$html = \Drupal::service('renderer')->renderPlain($render_output);
$node->set('field_text_cached_html', $html);
$node->field_text_cached_html->format = 'mymarkup_processed';
}
Is there a way to force Drupal to use the site theme instead of the admin theme when rendering?
On the Appearance administration page, "Use the administration theme when editing or creating content" is unchecked.