I'm trying to fetch the label of the list and display it on the twig template.

Using node.field_list.value outputs the key value. I tried using node.field_list.label but it returns empty. I tried using node.field_list.title, node.field_list.0.label or node.field_list[0]['#markup'] but it still returns empty on the twig template

  • 1
    Ah, I understand your question properly now from the accepted answer, I was thinking of the field label rather than the human names for the values. – Alfred Armstrong Jul 26 '18 at 13:59

There are a few ways:

It calls List allowed values and it stores in the field configuration.

When field cardinality is 1

  {% set list_value = node.filter_list.value %}
  {{ node.filter_list.getSetting('allowed_values')[list_value] }}

When field cardinality is more than 1

  {% set allowed_values = node.filter_list.getSetting('allowed_values') %}
  {% set list_values = node.filter_list.getValue() %}
  {% for list_value in list_values %}
      {{ allowed_values[list_value['value']] }}
  {% endfor %}

In case when your template is node.html.twig you can render it based on field formatter in your view mode.

  {{ content.filter_list }}
  • Thanks, but what if the field is set to unlimited and not to limited? I tried this {% set list_value = node.field_list.value %} {% for list in node.field_list.getSetting('allowed_values')[list_value] %} {{ list }} {% endfor %} but it does not seem to output anything on the loop – clestcruz Jul 26 '18 at 8:19
  • 1
    @clestcruz good point, I updated my answer – Alex Kuzava Jul 26 '18 at 8:42
  • Hmm I tried your solution but nothing seems to output within the twig template – clestcruz Jul 26 '18 at 9:11
  • @clestcruz ahh yes, ->value returns only single value, I updated answer, try one more time – Alex Kuzava Jul 26 '18 at 9:38

You can use the module Twig Field Value to help with this task. Once installed the code:

{{ content.field_list|field_label }}

should display the label. The way the label is stored and accessed not affected by the field's cardinality.

  • I tried using Twig Field Value to output the value as you mentioned but it only returns empty. I even tried {{ node.field_list.value|field_label }} – clestcruz Jul 26 '18 at 8:38
  • Try using "content" instead of "node". I just checked the comments in the base node template and that's the correct way to get rendered field data. – Alfred Armstrong Jul 26 '18 at 8:58
  • Yah I tried that also, the thing it is rendered within page.html.twig – clestcruz Jul 26 '18 at 9:00
  • In page.html.twig? That's not where you'd normally render field data directly. If you need to go off-piste it would be better to sort out your data in a preprocess hook instead. – Alfred Armstrong Jul 26 '18 at 9:06

Here is how I've been able to display the label of a Single Choice Text List inside a custom Entity referenced on a node with an EntityReference field :

{% for entity_ref_item in node.field_my_entity_ref_field %}
  {% set my_choice_key = entity_ref_item.entity.field_my_list_field.value %}
  {% set my_choice_label = entity_ref_item.entity.field_my_list_field.getSettings('allowed_values')['allowed_values'][my_choice_key] %}
  <div>{{ my_choice_label }}</div>
{% endfor %}

I believe that Alex Kuzava's code might have been missing one array ['allowed_values']

{{ node.filter_list.getSetting('allowed_values')[list_value] }}

might need to be

{{ node.filter_list.getSetting('allowed_values')['allowed_values'][list_value] }}

instead. To be sure, you can use devel kint()

{{ kint(node.filter_list.getSetting('allowed_values'))}}

That's how I found out there was one more array level before getting to the label. But maybe my situation was different because I am accessing a custom entity...

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.