I have a view that outputs a list of nodes for a menu. The view displays the title linked to the node. The View allows published nodes or nodes in the moderations states draft or review. How can I add a class around the titles that are in draft or review. I would like to style these differently. I am using drupal_view to print the block inside a template. Can this be done in a theme preprocess or custom module?

  • This is similar to drupal.stackexchange.com/questions/276153/…. You can create a Views field plugin "link_with_moderation_state" which adds a class to the link (or add a surrounding div to add the class) depending on the moderation state of the entity.
    – 4uk4
    Commented Feb 13, 2019 at 21:36
  • Thank you @4x4. I think I found another way Commented Feb 14, 2019 at 17:04

1 Answer 1


This can be done through the Views interface.

You can add classes based on existing Field values to entire rows in Format > Settings, or you can add classes to individual Fields (you mentioned title) under Style settings wrapper or field HTML options.

Tokens to use in all of these cases can be found in the settings of the Field according to which you want to style the rows/other fields. The settings are under Rewrite results > Override the output of this field with custom text > Replacement patterns options.

You don't have to actually keep the rewrite enabled, you just need to temporarily reveal and uncollapse to copy the tokens you need.

Then add CSS for those classes to your subtheme as usual, or through CSS editor module. You can hide the field ("Exclude from display") but it needs to be there on the list, arranged before any other fields that may use its value for classes.

  • I found that I could a new field for moderation_state. Then exclude it from display. Then add a rewrite to the title field like this {{ title_1 }} {{ moderation_state }} Commented Feb 14, 2019 at 17:05
  • But I only want to display the moderation state IF its in draft or review state. I tried this below but its also prints 'Published' and I only want to display it for draft. {{ title_1 }}{% if field.moderation_state == draft %} {{ moderation_state }} {% endif %} {% endif %} Commented Feb 14, 2019 at 17:06
  • Your question was about a class, and you can still display "draft" text through CSS content property, for example after the title text. But if you don't need different styles and just want to output the state as a Field, you can Rewrite it the way you tried, only fix your syntax. You have one endif too many, quote the string value, and check the debug for correct name, it's usually something like {{ content.field_name }}.
    – prkos
    Commented Feb 14, 2019 at 18:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.