Drupal 8 is still beta-ish in so many aspects, that “workarounds” is becoming a separate genre of Drupal development tips and tricks.
The today’s workaround is for the bug in “node preview” functionality. It is not frequently used, but can be rather convenient for large websites with moderation enabled. The bug can be spotted only when CSS styles, applied to the node/page template rely on the <body> CSS classes, namely the core-added page-node-type-[node bundle] body class. It helps to effectively distinguish different node templates, and therefore are widely relied on by front-end developers. However, in preview mode, this class appears entirely missing, which breaks the styling.
The workaround is simple and relies on the hook_preprocess_html() theme hook:
1 2 3 4 5 6 7 8 9 10 11 |
use Drupal\node\NodeInterface; /** * Implements hook_preprocess_HOOK() for HTML document templates. */ function yourmodule_preprocess_html(&$variables) { // If on an individual node page, add the node type to body classes. if (($node = \Drupal::routeMatch()->getParameter('node_preview')) && $node instanceof NodeInterface) { $variables['node_type'] = $node->getType(); } } |
So you can add this hook to you custom module, and solve the problem – and the functionality won’t be broken even after this is fixed in Drupal 8 core.
Thanks for the ideas here.
There is also core patch that tries to address the context issues related to node_preview here:
https://www.drupal.org/project/drupal/issues/2890758
I’m not sure if it will solve your exact problem, but it worked fro my case and might be worth a look.
Yeah, that’s a good finding, James. Thank you