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

  1. Create a vocabulary named "Tags" (machine name "tags").
  2. Add a taxonomy term reference field to a content type. Use the autocomplete widget and enable autocreate.
  3. Programmatically set handler_settings.target_bundles to NULL and handler_settings.auto_create_bundle to "tags". This perfectly valid configuration allows references to all vocabularies while specifying Tags for new terms.
  4. Create a node, enter a new term in the reference field, and save.
  5. Observe that the term has the vocabulary ID "taxonomy_term" instead of "tags".

Issue fork drupal-3632777

Command icon 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

pancho created an issue. See original summary.

hoxton made their first commit to this issue’s fork.

sourav_paul’s picture

Checking this issue.

sourav_paul’s picture

Yes, 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)

sourav_paul’s picture

Status: Active » Needs review
StatusFileSize
new150.66 KB
new150.61 KB

I'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:
img

After:
Only local images are allowed.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs issue summary update

Have not reviewed

IS is incomplete.