0

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->entityFieldManager->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:

  1. Call getFieldMapByFieldType('entity_reference');.
  2. 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) ;)

  • Doh! entity_reference module is deprecated already. Trying to track down where else in the codebase this stuff might be located. – JamesWilson Feb 16 at 2:47
  • I found some sample code that might work in field.module's field_entity_bundle_delete() ... – JamesWilson Feb 16 at 3:00
0

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()->getFieldMapByFieldType('entity_reference');
    $ids = [];
    foreach ($map as $type => $info) {
      foreach ($info as $name => $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->getName();
      $target_type = $field_config->getSetting('target_type');
      if (!empty($target_type) && $target_type == $entity_type)) {
        if (!empty($entity_bundle) {
          $handler_settings = $field_config->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;
  }

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.