Score:2

Is there a way to add a page element to all unpublished pages?

ao flag

I am trying to add an "unpublished" graphic on all content that is unpublished so editors can clearly see that the page is unpublished, but I have about 15 different content types.

Is there a way I can add this to all content easily with twig templates or otherwise?

Kevin avatar
in flag
By default, Drupal adds a css class on the body (if I am not mistaken) indicating it is not published. In default themes, the page would then have a reddish hue behind the content so you knew it was unpublished. It is also visible from the edit form or content overview screen in the admin.
yuuuu avatar
ao flag
I managed to find that it is adding `note--unpublished` to some content types, is there a way to make it do it to all content types?
Kevin avatar
in flag
It would do that to all content types. That makes me believe there is something in your custom theme that may be altering this class list based on content types that are not receiving it?
yuuuu avatar
ao flag
I will do some more digging. Thanks
apaderno avatar
us flag
The *node--unpublished* CSS class is listed in [node.html.twig](https://api.drupal.org/api/drupal/core%21modules%21node%21templates%21node.html.twig/9.3.x), which means Drupal adds it independently from the used theme. If then a theme removes it from `attributes.class`, that is a different matter.
Score:2
cn flag
function THEME_preprocess_node(&$variables) {
  $node = $variables['node'];
  if (!$node->isPublished()) {
    $variables['attributes']['class'][] = 'unpublished-class-for-node';
  }
}
leymannx avatar
ne flag
Yeah or `$variables['some_element'] = ['#markup' => '<h1>unpublished</h1>'];` and then in the node.html.twig put `{% if some_element %}{{ some_element }}{% endif %}`. Wondering if you could access the unpublished status maybe directly in the template as well.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.