1

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
4
  • 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 Feb 14, 2019 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 Feb 14, 2019 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 Feb 14, 2019 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 Feb 15, 2019 at 1:07

2 Answers 2

1

You don't need to strip HTML tags. This works for me:

{% if field_tags == "green" %}
   GREEN
{% else %}
   NOT GREEN
{% endif %}

where field_tags is a taxonomy reference field with "Link label to the referenced entity" disabled.

1

Try to create an additional variable and use this for the conditions.

{% set my_var = field_tags|striptags|trim %}

{% if my_var == "green" %}
   GREEN
{% else %}
   NOT GREEN
{% endif %}
1
  • In my particular problem, creating a variable and using it to compare was the only thing that worked.
    – LeraA
    Nov 29, 2022 at 17:46

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.