0

How can I add a Views field rewrite using Twig that will display the moderation state only if the node is in draft state or review state? I tried the code below but it prints all of the moderation states.

{{ title_1 }}
{% if field.moderation_state == 'draft' %}Draft{% endif %}

This is how my list of titles look:

page1 Draft
page2 Published
page3 Submitted

I want all of the page titles to always display. I never want Published to display. So I want to first check if the moderation state is equal to draft and review.

I also tried variations like this

{% if moderation_state__value == 'draft' %}

Below are the available replacement pattters

   {{ moderation_state }} == Moderation state
   {{ path }} == Link to Content
   {{ title_1 }} == Title
   {{ title_1__value }} == Raw value
   {{ arguments.nid }} == Content: ID title
   {{ raw_arguments.nid }} == Content: ID input
  • Your if statement depends on raw value of the field, however, the whole output of the field is printed with views unless you strip the html tags and remove default views markup from the field and its wrapper – Ismail Cherri Feb 14 at 20:08
  • Thanks @Ismail Cherri, I removed any default classes and stripped html and removed whitespace from the field. It still does not work – paulcap1 Feb 14 at 20:36
  • Ok.Is this field a list text field? If so, then you should output the key not value. Also inspet its output in the view previee area – Ismail Cherri Feb 14 at 21:54
  • These are the fields I am using, in this order: moderations_state - excluded from display path - excluded from display title <- I am adding the rewrite here – paulcap1 Feb 15 at 1:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.