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.
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.
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');
comment, contact_message, file, node, shortcut, taxonomy_term, user, menu_link_content
Commented
Aug 10, 2016 at 9:30
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).
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);