Using Entity Type Manager to get a list of fields

Components

Get all entity reference fields that target media entities

To get a list of all entity reference fields that target media entities you can use the Entity type manager and the loadByProperties() method.

$fields = \Drupal::entityTypeManager()->getStorage('field_storage_config')->loadByProperties([
  'type' => 'entity_reference',
  'settings' => [
    'target_type' => 'media',
  ],
]);

Get all dynamic entity reference fields

In the same way, you can get a list of dynamic entity reference fields.

$fields = \Drupal::entityTypeManager()->getStorage('field_storage_config')->loadByProperties([
  'type' => 'dynamic_entity_reference',
]);

The $fields variable will contain an array of FieldStorageConfig instances for each field that satisfies the condition.

Image

This won't return fields that are defined as BaseFieldDefinition in your code.

About the Author

Goran Nikolovski is a senior web developer who loves working with Drupal, React, and React Native. He is the founder of this website and he enjoys sharing his knowledge. Need help with anything? Get in touch.