Problem/Motivation

The Entity Reference Filterable Autocomplete widget only works when used in main node forms but fails when used inside paragraph subforms or other nested form contexts. The issue occurs because the widget directly accesses $form_state->getUserInput()[$fieldName][$delta]['bundle'] which assumes the field is at the top level of the form structure.

Steps to reproduce

1. Add an entity reference field with the "Filterable Autocomplete" widget to a paragraph type
2. Add this paragraph to a node form
3. Try to use the bundle filter dropdown - it won't work properly
4. The AJAX callback also fails to update the autocomplete field

Root cause

In EntityReferenceFilterableAutocompleteWidget.php:

- Line 80-81: Direct array access $input[$fieldName][$delta]['bundle'] only works for top-level forms
- Line 168-169: AJAX callback assumes $form[$fieldName]['widget'][$delta]['target_id'] structure

In nested forms like paragraphs, the actual structure is:

  $input['field_content'][0]['subform'][$fieldName][$delta]['bundle']
  $form['field_content']['widget'][0]['subform'][$fieldName]['widget'][$delta]['target_id']

Proposed resolution

Use Drupal's NestedArray::getValue() utility with proper parent arrays to navigate nested form structures, similar to how core modules like Paragraphs handle this:

1. For form element access: Use $element['#field_parents'] combined with field path
2. For AJAX callback: Use $triggeringElement['#array_parents'] with NestedArray::getValue()

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

halth created an issue. See original summary.

  • halth committed 47da4d87 on 1.0.x
    Issue #3548192 by halth: Fixed PHPCS errors.
    

  • halth committed a421ec9b on 1.0.x
    Issue #3548192 by halth: Fixed issue with widget not working on subforms
    
dahousecat’s picture

Status: Active » Fixed

Thanks for the fix here halth - I've updated the test and applied your changes in version 1.0.5.

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

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

Maintainers, please credit people who helped resolve this issue.

halth’s picture

I thank you for this amazing module, Felix! It really helped me out on a particular use case last week.

I really appreciate your efforts in putting this project together and feel honored to have the chance to make a small contribution.

Ah! And thanks for fixing the tests. I didn't really have the time to get into that still.

Keep up with the great work!

Status: Fixed » Closed (fixed)

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