Problem/Motivation

When using an entity reference autocomplete field on a webform for a single taxonomy vocabulary bundle with the option to auto-create entities if they do not exist, submitting the form with a non-existent term value produces a validation error stating There are no taxonomy terms matching "[value]".

Screenshots attached showing the relevant webform field configuration and the validation error for clarity.

Steps to reproduce

  1. Add an entity reference autocomplete field to your webform
  2. Select Taxonomy Term as the type of item to reference
  3. Select your desired reference method
  4. Tick the box "Create referenced entities if they don't already exist"
  5. Select ONLY ONE Vocabulary bundle to use

    Note that in this case, there is NO field to select which bundle should be used for the auto-creation.
  6. Save the field and go test your form
  7. Enter a value into the entity reference autocomplete field that does NOT exist in your selected vocabulary and submit the form
  8. See the validation error stating that no terms exist matching what you entered

Proposed resolution

I have narrowed this down to the Drupal\webform\Plugin\WebformElement\EntityAutocomplete class in src/Plugin/WebformElement/EntityAutocomplete.php. The issue is in the prepare method, which starts on line 99 in Webform versions 6.2.8 as well as the 6.3.x branch.

On line 108, the element will set the $element['#autocreate']['bundle'] value required for the core field's auto-create function to work, but ONLY if both $element['#selection_settings']['auto_create'] AND $element['#selection_settings']['auto_create_bundle'] are not empty. The problem here is that when only one target bundle is selected in the Webform element's configuration UI, the field for selecting the bundle to use for auto-creation is hidden and no value is saved with the configuration.

Upon reviewing the core entity autocomplete field's code for when using that widget type on, say, a node entity, it figures out which bundle to auto-create when only one is selected, otherwise expecting one to be specified. However, the Webform element provides no similar code, so there's no bundle set and therefore it does not add the $element['#autocreate'] settings to the field at all. As a result, it doesn't try to auto-create the non-existent entity.

One solution I propose would be to update the code in the prepare method to set the auto create bundle value when auto create is not empty but auto create bundle IS empty.

Another solution would be to update the code for saving the field configuration so that it automatically sets and stores the auto create bundle value when only one target bundle is selected for the reference field.

To me the former seems more straightforward and is a simpler change to the code.

Remaining tasks

  • Update code in prepare method per above
  • Create a merge request

Issue fork webform-3490096

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

teknocat created an issue. See original summary.

cilefen’s picture

Version: 6.2.8 » 6.2.x-dev
Status: Active » Needs review
liam morland’s picture

Version: 6.2.x-dev » 6.3.x-dev
Status: Needs review » Needs work
Issue tags: +Needs reroll

Please re-roll on 6.3.x.

teknocat’s picture

I've provided a second merge request to re-roll the same fix onto the 6.3.x branch.

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

  • jrockowitz committed 7b8b76b1 on 6.3.x authored by teknocat
    [#3490096] feat: Entity reference field fails to create entities that...
jrockowitz’s picture

Status: Needs work » Fixed

Now that this issue is closed, please review the contribution record.

As a contributor, attribute any organization helped you, or if you volunteered your own time.

Maintainers, please credit people who helped resolve this issue.

  • jrockowitz committed 7b8b76b1 on 6.x authored by teknocat
    [#3490096] feat: Entity reference field fails to create entities that...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.