Get a list of all entity reference fields that reference specific entity types Is there a way to get a list of entity reference fields by field type (similar to what is produced by $all_reference_fields = $this-&gt;entityFieldManager-&gt;getFieldMapByFieldType('entity_reference');) but filtered down to just list the entity reference fields that have target entity types and bundles that match a specified list.

I'm not seeing anything in the entityFieldManager that would make it easy to filter the results of the getFieldMap() or getFieldMapByFieldType() down  to just specific reference fields I'm looking for, because the mapping doesn't contain any of the field's storage information, that contains the list of target bundles and handlers.

Looks like the entity_reference module in core doesn't provide any services that would be the logical place for something like this, so my current thinking is that I'll need to do this in two steps:


Call getFieldMapByFieldType('entity_reference');.
Work through every single entity type in the field map, and call buildFieldStorageDefinitions($entity_type); or something of that nature, to figure out the target entity_type:bundle_name combinations, and track down fields that reference 'taxonomy_term:tag'. This seems horribly inefficient, due to the sheer number of entity_reference type fields in this site, so maybe there is a better way to do this step?


In an ideal world, there'd be just a set of database calls I could make, or even a service in core for entity_reference module! (wishful thinking) ;)
Answering this myself based on the investigation I did using field_entity_bundle_delete for inspiration.  Would be great if someone could take this code and improve it.

  /**
   * Helper method gets a list of all entity reference fields that reference
   * the specified entity type.
   *
   * @param $entity_type string
   * @param $entity_bundle string (optional)
   * @return a filtered field map of entity reference fields.
   */
  protected function getEntityReferenceFieldsByEntityType($entity_type, $entity_bundle = '') {

    // Gather a list of all entity reference fields.
    $map = \Drupal::entityManager()-&gt;getFieldMapByFieldType('entity_reference');
    $ids = [];
    foreach ($map as $type =&gt; $info) {
      foreach ($info as $name =&gt; $data) {
        foreach ($data['bundles'] as $bundle_name) {
          $ids[] = "$type.$bundle_name.$name";
        }
      }
    }

    // Determine if any of the reference fields reference a specific entity type
    // and bundle type.
    foreach (FieldConfig::loadMultiple($ids) as $field_config) {
      $field_name = $field_config-&gt;getName();
      $target_type = $field_config-&gt;getSetting('target_type');
      if (!empty($target_type) &amp;&amp; $target_type == $entity_type)) {
        if (!empty($entity_bundle) {
          $handler_settings = $field_config-&gt;getSetting('handler_settings');
          if (isset($handler_settings['target_bundles'][$entity_bundle])) {
            $filtered_map[$entity_type][$field_name] = $map[$entity_type][$field_name];
          }
        } else {
          $filtered_map[$entity_type][$field_name] = $map[$entity_type][$field_name];
        }
      }
    }

    return $filtered_map;
  }