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 web and AI developer with over 10 years of expertise in PHP, Drupal, Python, JavaScript, React, and React Native. He founded this website and enjoys sharing his knowledge.