In one of my content types, I have a Status field (field_status) which is a List(text) field displaying 3 radio button options set to the following key|label values:

  • 0|In Stock
  • 1|Under Deposit
  • 2|Sold

I have a custom twig template file for this content type and was wondering how I would display the label value ONLY if the option isn't 0|In Stock. In Stock is pretty much the default option so when the item is sold/under deposit, I want a "Sold/Under Deposit" banner to appear in the upper corner of the picture of the item.

Also, I have a view displaying a list of these content types so how would I only show this value again if the selection isn't "In Stock". I don't have a custom template for the view as I did it all in the UI so I'm hoping there's a way to do this there as well.

I feel like this should be a simple solution but for some reason I'm having issues with it.

I figured it would be something like the following but this doesn't seem to work:

{% if field_status is not ['0'] %}
  {{ field_status }}
{% endif %}

I'm assuming my syntax is wrong. What would be the proper way of doing this?

Comments

SnowCoder’s picture

Ok, so I knew I was overthinking my issue. The way I got this to work for me was the following.
In a pre-process function I created the itemstatus variable with the following code:

function happydays_preprocess_node(&$vars) {
  $vars['itemstatus'] = $vars['content']['field_status'][0]['#markup'];
}

And then just applied it to my twig template like so:

{% if itemstatus != "In Stock" %}
  <div class="corner-ribbon">
    {{ itemstatus }}
  </div>
{% endif %}