Get the list of fields for an entity type

Components

To get the field definitions of any entity type use the following code. We first get all bundles, and then for each bundle we are getting the field definitions.

$bundles = \Drupal::service('entity_type.bundle.info')->getBundleInfo('node');

foreach ($bundles as $bundle => $data) {
  $fields = \Drupal::service('entity_field.manager')->getFieldDefinitions('node', $bundle);
}

About the Author

Goran Nikolovski is a senior developer with over 10 years of experience in web, app, and AI solutions. He founded this website to share his expertise, provide useful resources, and contribute to the broader developer community.

AI Assistant