Skip to content

Instantly share code, notes, and snippets.

@zakiya
Last active February 2, 2021 00:34
Show Gist options
  • Save zakiya/7bf9ae848437cce24ecbc29d5555dd9e to your computer and use it in GitHub Desktop.
Save zakiya/7bf9ae848437cce24ecbc29d5555dd9e to your computer and use it in GitHub Desktop.
Get a list of entities that reference a paragraph type.
_field_usage_check('event', 'entity_reference');
/**
* string $reference_type
* Example: 'node' or 'paragraph'
* string $referenced_entity_type
* Example: 'entity_reference' or 'entity_reference_revisions'
*
* Given the machine name of a paragraph, return/print the entities that reference it.
*/
function _field_usage_check($referenced_entity_type, $reference_type) {
$map = \Drupal::service('entity_field.manager')->getFieldMap();
foreach ($map as $entity_type => $entity_type_array) {
foreach ($entity_type_array as $field_name => $field_data) {
if (str_starts_with($field_name, 'field_') && $field_data['type'] == $reference_type) {
$parent_bundles = $field_data['bundles'];
foreach ($parent_bundles as $parent_bundle_key => $parent_bundle_name) {
$field_config_string = sprintf('field.field.%s.%s.%s', $entity_type, $parent_bundle_name, $field_name);
$bundles = \Drupal::config($field_config_string)
->get('settings.handler_settings.target_bundles');
$x = is_array($bundles);
$y = is_object($bundles);
foreach ($bundles as $child_bundle_key => $child_bundle_name) {
if ($child_bundle_name == $referenced_entity_type) {
dump( $entity_type . ' - ' . $parent_bundle_name . ' - ' . $field_name);
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment