Skip to main content

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 array
 * @return a filteredFiltered field map of entity reference fields.
   */
  protected function getEntityReferenceFieldsByEntityType($entity_type, $entity_bundle = '') {
 
    // Gather a list of all entity reference fields.
    $map = \Drupal::entityManagerservice('entity_field.manager')->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.
  $filtered_map = [];
  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;
  }

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;
  }

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 array
 *   Filtered field map of entity reference fields.
 */
protected function getEntityReferenceFieldsByEntityType($entity_type, $entity_bundle = '') {
  // Gather a list of all entity reference fields.
  $map = \Drupal::service('entity_field.manager')->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.
  $filtered_map = [];
  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;
}
Source Link
JamesWilson
  • 2.2k
  • 1
  • 24
  • 48

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;
  }