I want to retrieve all fields that entities are using (including locked fields).

How can I get a list of fields that are used in entities?

Like we get in admin/reports/fields.

enter image description here

  • Do you want to do this programmatically, or review a site's use of fields through the UI? Aug 10 '16 at 8:05
  • I want this programatically..
    – Ajay Reddy
    Aug 10 '16 at 8:07
  • Why not just borrow the code from Drupal core then? FieldStorageConfigListBuilder.
    – anonymous
    Oct 27 '20 at 3:20

I would recommend EntityFieldManager::getFieldMap. The EntityManager is only a service to provide functions from other classes.

From documentation:

Return value

array An array keyed by entity type. Each value is an array which keys are field names and value is an array with two entries:

  • type: The field type.
  • bundles: An associative array of the bundles in which the field appears, where the keys and values are both the bundle's machine name.

EntityFieldManagerInterface::getFieldMap() produces a keyed array by entity type > field name with further information on the field type and its associated bundles.

$field_map = \Drupal::entityManager()->getFieldMap();
$node_field_map = $field_map['node'];
$node_fields = array_keys($node_field_map['node']);

There's also EntityFieldManagerInterface::getFieldDefinitions() if you want further field metadata. It produces an array of BaseFieldDefinition & FieldConfig objects that you can parse through for further detail.

  $node_article_fields  =\Drupal::entityManager()->getFieldDefinitions('node','article');
  • Thanks Shawn, I am not getting fields like they listed in above image. Below field's i am getting: comment, contact_message, file, node, shortcut, taxonomy_term, user, menu_link_content
    – Ajay Reddy
    Aug 10 '16 at 9:30
  • There was a typo with the code. As mentioned with the answer, the function produces a keyed array that contains entities, field names, then field details.
    – Shawn Conn
    Aug 10 '16 at 14:41
  • 1
    Method 'entityManager' is deprecated. Use \Drupal::entityTypeManager() instead in most cases
    – JFK
    May 28 '20 at 1:40

field_info_instance() would do what you want, though if you look in the docs you will see that in D8 it is deprecated in favour of

Field::fieldInfo()->getBundleInstance($entity_type, $bundle, $field_name).
  • Thank for your answer, But i need to get list of all field(s) that are only in use. I don't want to get using by entity-type, bundle and field-name. For reference uploading image.
    – Ajay Reddy
    Aug 10 '16 at 8:15

Different ways to get field data:

Get a list of fields include base fields like nid. (contains entity type, bundle, field name and field type).

$field_map = \Drupal::entityTypeManager()->getFieldMap();
// Can also get by field type.
$field_map = \Drupal::entityTypeManager()->getFieldMapByFieldType('entity_reference');

Also see @shawn-conn's answer.

Get a list of storage fields and their data. (non-bundle specific - e.g. 'node.field_image')

// Get a list of storage fields
$field_config_ids = \Drupal::entityQuery('field_storage_config')
  // Allow access to all regardless of permissons.
  ->condition('type', 'entity_reference')
  ->condition('status', 1)

// Load all the fields.
$field_config_entities = \Drupal::entityTypeManager()->getStorage('field_storage_config')->loadMultipleOverrideFree($field_config_ids);

// Get where each config is used.
foreach (field_config_entities as $field_config_entity) {
  $bundles = $field_config_entity->getBundles();

Get a list of field instances and their data. (fields per bundle - e.g. 'node.story.field_image')

$field_config_ids = \Drupal::entityQuery('field_config')
  ->condition('field_type', 'entity_reference')
  ->condition('status', 1)
// Get also filter to only get feilds with a default value.
$field_config_ids = \Drupal::entityQuery('field_config')
  ->condition('field_type', 'entity_reference')
  ->condition('status', 1)
  ->condition('default_value.0.target_uuid', NULL, 'IS NOT NULL')
// Load the data.
$field_config_entities = \Drupal::entityTypeManager()->getStorage('field_config')->loadMultipleOverrideFree($field_config_ids);

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.