4

I'm trying to access the key of a list (Text) in a paragraph twig file

This is what I have in my list
small|Small Item
big|Big Item

{{content.field_option.0['#context'].value}}
{% set value = content.field_option.0['#markup'] %}
{{ value }}

I always got the text. Small Item or Big Item. I would like to obtain small or big of course depending of the user selection.

I'm going to use this value as an HTML Class.

Thanks for your help

1
  • 1
    That's a valid quest(ion), there is no need to down-vote it without leaving a comment on how to improve it.
    – LarS
    Commented Mar 19, 2019 at 18:13

2 Answers 2

2

I changed the entity_view_display of my paragraph so that my field output the key, instead of the value. I was then able to access the key. I hope that helps?

I'm able to use this: content.field_button_chrome[0]['#markup']

2

There are (at least) two options:

  • Option 1: Change the Display of that field from default to key and use {{ content.field_name.0 }}
  • Option 2: Do not use content settings and get the what the entity (paragraph, node etc.) sees: {{ paragraph.field_name.0.value }} for radio/select fields or for multi value fields:
    {% for k,v in paragraph.field_variant %}
      {{ v.value }}
    {% endfor %}
    

See also Getting Drupal 8 field values in Twig > List > Keys.

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.