Entity Autocomplete field does not add Taxonomy Term even when "Create referenced entities if they don't already exist" is selected

Created on 30 April 2024, 8 months ago

Steps to reproduce

  1. Install and enable the module (Using version ^6.2 for drupal/webform)
  2. Add an entity autocomplete field for taxonomy term (Tags) and specify auto_create: true.
  3. Add a tag (Test) using Drupal's default interface (/admin/structure/taxonomy/manage/tags/overview)
  4. (OK) Test the webform, enter Test, and submit.
  5. (Error) Test the webform, enter "Test2", and submit. Error message: There are no taxonomy terms matching "Test2".

The form is in this shape:

tag:
  '#type': entity_autocomplete
  '#title': Tag
  '#target_type': taxonomy_term
  '#selection_handler': 'default:taxonomy_term'
  '#selection_settings':
    target_bundles:
      tags: tags
    sort:
      field: name
      direction: asc
    auto_create: true
    auto_create_bundle: ''

I am guessing the issue comes from the following validation code.

  • When a taxonomy term does not exist already, $form_state->getValue($element['#parents']) is empty.
  • $element['#value'] holds the text value of the new/non-existing tag. But it's not used to create a new tag.
  /**
   * Form API callback. Remove target id property and create an array of entity ids.
   */
  public static function validateEntityAutocomplete(array &$element, FormStateInterface $form_state) {
    // Must use ::getValue($element['#parents']) because $element['#value'] is
    // not being updated.
    // @see \Drupal\Core\Entity\Element\EntityAutocomplete::validateEntityAutocomplete
    $value = $form_state->getValue($element['#parents']);
    if (empty($value) || !is_array($value)) {
      return;
    }

    ... 

πŸ› Bug report
Status

Active

Version

6.2

Component

Code

Created by

πŸ‡―πŸ‡΅Japan hktang

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @hktang
  • Status changed to Closed: works as designed 28 days ago
  • πŸ‡ΊπŸ‡ΈUnited States jrockowitz Brooklyn, NY

    The webform module does not support the autocreation of entity reference via the UI or YAML.

Production build 0.71.5 2024