2

In Drupal 8, I defined a content type (say example) and made a separate template for it: node--example.html.twig.

I wonder if I can reference the item or label of a specify field in node--example.html.twig, instead of reference them in field.html.twig? By which I mean can I use something like the following, which I know is not working?

{{ content.field_example_field.label }}
1
  • Item(as a Field API field attached to an entity) yes, but not the label due to the structure. Although maybe something like {{ content.myfield.und[0]['#title'] }}could potentially work.
    – user21641
    Commented Jan 28, 2016 at 15:37

1 Answer 1

2

If you can't find the label somewhere inside of content, then the code, which will prepare the label for rendering did not run at this point.

If you are inside the node twig, then there is a fully loaded node object. You can use this to get the field definition and the label:

{{ node.field_myfield.fieldDefinition.label }}

Or the first field item:

{{ node.field_myfield.0.value }}
1
  • That's what I was looking for, thx.
    – MuSaka
    Commented Jan 29, 2016 at 15:25

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.