0

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?

1 Answer 1

2

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.

1
  • This is helpful, but I think tells me I can't do what I want to do.
    – andileco
    Nov 8, 2021 at 15:25

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.