Score:0

How can one programmatically get the bundle constraint for an entity reference field?

rs flag

I'm working in the context of a Views plugin. I have $entity_type and $field, as required in the snippet below. How can I get the bundle constraint for the entity reference field programmatically?

/** @var Drupal\field\Entity\FieldStorageConfig $field_configuration */
$field_configuration = FieldStorageConfig::loadByName($entity_type, $field);

$field_configuration->getType() gives me "entity_reference"

$field_configuration->getSettings()['target_type'] gives me "taxonomy_term"

...but what methods return the vocabulary or vocabularies (bundles) that the entity reference field is set to be limited by (if at all) in its configuration?

Score:2
in flag

The types allowed by an entity reference field is stored in the field config (FieldConfig.php and the field.field.* yaml), not the field storage config (FieldStorageConfig.php and the field.storage.* yaml).

If you take a peek at one instance of a field.field.* yaml of an entity reference field, you'll see the settings.handler_settings.target_bundle property containing a list of allowed bundles for that entity reference field. To get this data from code, you'll need FieldConfig::load(id) or FieldConfig::loadByName($entity_type_id, $bundle, $field_name) depending on what data you have at hand. Once you get the field config instance, there is a getSetting() method which you can use to grab handler_settings.

andileco avatar
rs flag
This is helpful, but I think tells me I can't do what I want to do.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.