7

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? – autopoietic Aug 10 '16 at 8:05
  • I want this programatically.. – Ajay Reddy Aug 10 '16 at 8:07
8

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');
  • 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
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).
  • 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

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.