4

I have a content type (appel_offre) with a field_categorie_avis field, which is a list type with these values.

  • ouvert | Ouvert toute l'année
  • restreint | Restreint 24h
  • concurrence | Appel public à concurrence

In the node--appel-offre.html.twig template, I want to print this field but I only get the key, not the value. For example, for Appel public à concurrence, I always get concurrence.

I am using node.field_categorie_avis.value. Is there another way to get the same value?

3
  • what does it spit out if you use dump() on the field?
    – Matt
    Dec 29 '16 at 20:07
  • Hello and happy new years !
    – lchabrand
    Jan 2 '17 at 0:29
  • Happy New year to you too!
    – Matt
    Jan 4 '17 at 15:57
10

You can use

{{ content.field_categorie_avis[0]['#markup'] }}

to display label.

Dont miss the [0] because drupal always store items in array in case of field size limit change.

2
  • Very useful. I was struggling with this for last couple of hours. Apr 25 '18 at 11:26
  • This did not work for me in Drupal 8.9. Instead, use: element['#object'].field_name.setting('allowed_values')[element['#object'].field_name.value]
    – komlenic
    Apr 23 at 20:53

Your Answer

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

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