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()
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 #5
dahousecat commentedThanks for the fix here halth - I've updated the test and applied your changes in version 1.0.5.
Comment #7
halthI 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!