I have a list field for a custom content type that looks like this:

Field Machine name is: field_app_os


I can access the key in Twig template by using content.field_app_os or Twig Field Value and the output of both is Android:

Twig Field Value allows Drupal 8 themers to get partial data from field render arrays. It gives them more control over the output without drilling deep into the render array or using preprocess functions.
- field_label : Returns the field label value.
- field_value : Returns the render array of the field value(s) without the field wrappers.

/* with use of Twig Field Value module */
{{ content.field_app_os|field_value }}  {# outputs 'Android' #}

 /* without use of Twig Field Value module */
{{ content.field_app_os }}  {# outputs 'Android' #}

However, if I do a simple comparison, the output of both is False.

{% set app_os = content.field_app_os|field_value %}
{{ kint(app_os == 'Android') }} {# outputs 'bool FALSE' #}

Or When I use:

{% set app_os = content.field_app_os %}
{{ kint(app_os == 'Android') }} {# outputs 'bool FALSE' #}

What's wrong in my code?

  • What is the output when you do {% set app_os = content.field_app_os|field_value %} {% if app_os == 'Android' %} app_os is Android{% endif %} ?
    – Matt
    Sep 28 '17 at 17:48
  • Hmm weird. I wonder if 'content.field_app_os|field_value' is slightly different. Like maybe there's white space or something. As a test what happens if you just straight do {% set app_os = 'Android' %} does it work then?
    – Matt
    Sep 28 '17 at 17:52
  • Okay well At least we got it down to 'content.field_app_os|field_value' being the issue. And you are completely positive it's producing just that string with nothing else? And it doesn't produce that string inside an array or anything like that?
    – Matt
    Sep 28 '17 at 18:21
  • 1
    For those who are attempting to resolve the issue: As a test {{ content.field_app_os }} produced 'Android' but {% if content.field_app_os == 'Android' %} True {% endif %} didn't work.
    – Matt
    Sep 28 '17 at 19:41
  • 1
    @4k4: Thanks for your help and attention, When I use node.field_app_os.value then all things work correctly, But can you explain a little more about the cause of this issue, Please. Sep 28 '17 at 21:57

{{ content }} is a render array, where you find the label of the key/label pair probably here:

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

But this is not ideal for logic:

  1. The position in the render array can change, for example if you install a layout module.
  2. The content of #markup depends on how the field formatter is configured in the view mode.
  3. The label is translatable, so the logic breaks on a multilingual site.

So it's better to use the database value in:

{{ node.field_app_os.value }}

This is the first part of the key/label pair, which is untranslatable and you can access the value independent on how the field is configured in the view mode.

  • Thank you so much, But for what when I use Twig Field Value, The {{ content.field_app_os|field_value }} not work correctly? Sep 29 '17 at 6:40
  • 1
    I don't know this module, but this is probably to extract the part of the render array which renders the value without the field div's and not to extract a string value you can use in a logic expression. In general use content. for rendering and node. for logic.
    – 4k4
    Sep 29 '17 at 6:50

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.