Drupal - How do I get the field label from a node object?

Your first guess was very close, but both are protected properties, so are accessed by getters - functions with the prefix get:

$field_label = $node->field_name->getFieldDefinition()->getLabel();

For reference:

https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Field%21FieldItemInterface.php/interface/FieldItemInterface/8.2.x

https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Field%21FieldDefinitionInterface.php/interface/FieldDefinitionInterface/8.2.x

Edit:

Btw, in twig you don't need the prefix get, because if you tell twig to get something with the name label, it tests all possibilities on the object, first it tries to access the property label, then the method label() and finally the getter getLabel().

{{ node.field_name.fielddefinition.label }}

To get the field label in Twig (in this case in a node-NODETYPE.html.twig):

{{ content.field_FIELDNAME['#title'] }}

Tags:

8