4

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
  • 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. Jul 26, 2018 at 13:59

5 Answers 5

6

There are a few ways:

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

When field cardinality is 1

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

When field cardinality is more than 1

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

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

<div>
  {{ content.filter_list }}
</div>
4
  • 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, 2018 at 8:19
  • 1
    @clestcruz good point, I updated my answer Jul 26, 2018 at 8:42
  • Hmm I tried your solution but nothing seems to output within the twig template
    – clestcruz
    Jul 26, 2018 at 9:11
  • @clestcruz ahh yes, ->value returns only single value, I updated answer, try one more time Jul 26, 2018 at 9:38
4

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...

2

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.

4
  • 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, 2018 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. Jul 26, 2018 at 8:58
  • Yah I tried that also, the thing it is rendered within page.html.twig
    – clestcruz
    Jul 26, 2018 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. Jul 26, 2018 at 9:06
0

I'm using Drupal9 and did the same as @alex_kuzava. But got a problem with the key, seems that it was an array or something. So this is my answer:

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

Notice the [0] before field_location_tag. I don't really know whats happening here but my first clue was that list_key wasn't a string. So I try to apply the |string filter and Drupal told me that I was trying to do an array to string transformation.

I guess is something to do that the content I'm getting is from a lists of nodes inside a paragraphs that are loaded using \Drupal\node\Entity\Node::loadMultiple($nids);

Or maybe that I'm using the language module. Somehow the value is converted to an array first.

0

Adding a PHP code solution since Search Engines are leading here

private function getListTypeValue($node, $fieldName) {
  $listField = $node->get($fieldName);
  return $listField->getSettings()['allowed_values'][$listField->value] ?? '';
}

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.