0
$fields = \Drupal\Core\Entity\EntityTypeManagerInterface->getStorage('field_storage_config')->loadMultiple();

This is listing me all the fields.

foreach ($fields as $key => $instance) {
   $label = $instance->getLabel();
   $name = $instance->getName();
}

$label is giving me node.field_image

$name is giving me field_image

My question is how can we get the label of the field in drupal 8

3

The label is not stored in the field storage configuration. You'll find the label in the bundle specific field definition:

$definitions = \Drupal::service('entity_field.manager')->getFieldDefinitions('node', 'page');
foreach (array_keys($definitions) as $key) {
  // get label for field machine name $key:
  $label = $definitions[$key]->getLabel();
}

Reference: EntityFieldManager::getFieldDefinitions

  • This will give me field list of a bundle specific. How can i get the total fields list – Mahtab Alam Jun 25 '18 at 12:19
  • The labels are bundle specific, so the node's image field can have a different label for each content type. See the edit to be more precise about the original question how to get the label for the field machine name. You could however loop over each bundle if you want to get a total field list. Then start with \Drupal::service('entity_type.bundle.info')->getAllBundleInfo(); – 4k4 Jun 25 '18 at 12:32
  • Although if you would ask for a total field lists I found this duplicate drupal.stackexchange.com/questions/211057/… – 4k4 Jun 25 '18 at 12:40

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.