Problem/Motivation
An entity reference field can be configured programmatically to allow references to all bundles by setting target_bundles to NULL. This isn't possible in the UI as the target_bundles field is marked #required.
The specified auto_create_bundle can still determine where new entities should be created.
When building the autocomplete widget, Drupal mistakes this unrestricted bundle configuration for a bundleless entity type, therefore using the entity type ID for the bundle as well.
For taxonomy terms, it therefore ignores the configured vocabulary and uses "taxonomy_term" as the vocabulary ID.
Both the new term and the referencing node are saved without an error. However, the term belongs to a vocabulary that does not exist. It is missing from the vocabulary overview, and opening its edit form produces an error 500.
alexpott asked in #2821352-28: EntityReferenceAutocompleteWidget::getAutocreateBundle() unnecessarily requires the 'target_bundles' setting (3.) whether we should check if it's a bundleless entity type, but in the end that issue wasn't addressed in the committed fix.
Steps to reproduce
- Create a vocabulary named "Tags" (machine name "tags").
- Add a taxonomy term reference field to a content type. Use the autocomplete widget and enable autocreate.
- Programmatically set
handler_settings.target_bundlesto NULL andhandler_settings.auto_create_bundleto "tags". This perfectly valid configuration allows references to all vocabularies while specifying Tags for new terms. - Create a node, enter a new term in the reference field, and save.
- Observe that the term has the vocabulary ID "taxonomy_term" instead of "tags".
| Comment | File | Size | Author |
|---|---|---|---|
| #6 | Screenshot From 2026-10-27 17-01-21.png | 150.61 KB | sourav_paul |
| #6 | Screenshot From 2026-10-27 16-59-19.png | 150.66 KB | sourav_paul |
Issue fork drupal-3632777
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #3
sourav_paulChecking this issue.
Comment #4
sourav_paulYes, I've successfully recreated the issue locally, it seems root cause is in EntityReferenceAutocompleteWidget::getAutocreateBundle() (core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/EntityReferenceAutocompleteWidget.php:173-175):
// If there's no target bundle at all, use the target_type. It's the
// default for bundleless entity types.
if (empty($target_bundles)) {
$bundle = $this->getFieldSetting('target_type'); // => 'taxonomy_term', NOT 'tags'
}
When target_bundles is empty/NULL, it falls back to target_type. That's only correct for genuinely bundleless entity types (e.g. user), not for taxonomy terms, which do have bundles (vocabulary IDs).
Observed output:
- getAutocreateBundle() → 'taxonomy_term' (expected 'tags')
- Autocreated term saved with vid = 'taxonomy_term'
- Vocabulary 'taxonomy_term' exists: NO
- Term missing from the tags vocabulary overview
- Term edit form throws an error (Call to a member function getEntityTypeId() on null — the 500 the issue describes)
Comment #6
sourav_paulI've found that EntityReferenceAutocompleteWidget treated "no bundle limit" as "entity type has no bundles." which is only true for entity like users, not for terms.
so i've added a check in getAutocreateBundle()
is the target_type actually bundleless ?
if yes - keep using the entity type ID,
else, use configured auto_create_bundle (tags).
Also guarded count(NULL) since bundled types now reach that branch.
Included testUnrestrictedTargetBundles() to the existing autocreate test — sets target_bundles = NULL, autocreates a term, asserts it lands in the right vocabulary.
Before:

After:

Comment #7
smustgrave commentedHave not reviewed
IS is incomplete.