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 }}
  • 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 Jan 28 '16 at 15:37

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 }}
  • That's what I was looking for, thx. – MuSaka Jan 29 '16 at 15:25

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.