8

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

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

4 Answers 4

9

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.
5

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');
3
  • 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
    Commented Aug 10, 2016 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
    Commented Aug 10, 2016 at 14:41
  • 1
    Method 'entityManager' is deprecated. Use \Drupal::entityTypeManager() instead in most cases
    – JFC
    Commented May 28, 2020 at 1:40
2

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).
3
  • 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
    Commented Aug 10, 2016 at 8:15
  • The first link is spam, goes to steroids site.
    – user31782
    Commented Nov 30, 2022 at 0:12
  • Corrected - looks like drupalcontrib.org got hacked.. Commented Dec 5, 2022 at 22:09
1

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.
  ->accessCheck(FALSE)
  ->condition('type', 'entity_reference')
  ->condition('status', 1)
  ->execute();

// 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')
  ->accessCheck(FALSE)
  ->condition('field_type', 'entity_reference')
  ->condition('status', 1)
  ->execute();
    
// Get also filter to only get feilds with a default value.
$field_config_ids = \Drupal::entityQuery('field_config')
  ->accessCheck(FALSE)
  ->condition('field_type', 'entity_reference')
  ->condition('status', 1)
  ->condition('default_value.0.target_uuid', NULL, 'IS NOT NULL')
  ->execute();
  
// 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 and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.