Get all field mappings of a taxonomy term. Cycle through the fields, and select only the fields having references to a specific bundle.
<?php
/**
* Finds suitable data sources.
*
* Looks in taxonomy bundles for fields referencing content extensions.
*
* @return array
* Sources in format ['vocabulary' => 'field_name'].
*/
public static function getSuitableSources() {
$sources = [];
$all_vocabs = taxonomy_vocabulary_get_names();
$fieldManager = \Drupal::service('entity_field.manager');
foreach ($all_vocabs as $bundle) {
$defs = $fieldManager->getFieldDefinitions('taxonomy_term', $bundle);
foreach ($defs as $field_name => $def) {
if ($def->getType() == 'entity_reference') {
$settings = $def->getSettings();
if ($settings['target_type'] == 'my_target_bundle') {
$sources[$bundle] = $field_name;
}
}
}
}
return $sources;
}
?>