Problem/Motivation
The VirtualSelect form element does not work for forms with nested structures.
This is because of the vallueCallback method, that does not take into account the form structure, by not utilizing the parents , when getting the value from userInput:
public static function valueCallback(&$element, $input, FormStateInterface $form_state): mixed {
$fieldName = $element['#name'] . '-input';
$form_state->addCleanValueKey($element['#name'] . '-input');
return $form_state->getUserInput()[$fieldName] ?? [];
}
In the code above, we do not dig into the array structure properly.
Steps to reproduce
Create a custom form where you set the virtual_select element in the nested structure:
$form = [];
$form['#tree'] = TRUE;
$form['wrapper'] = [
'#type' => 'details',
];
$form['wrapper']['select'] = [
'#type' => 'virtual_select',
]
Proposed resolution
Fix the valueCallback so that it uses:
- first the input parameter if not empty
- then the nested structure of the form, by using $element['#parents']
Remaining tasks
Implement
User interface changes
None
API changes
None
Data model changes
None
Issue fork virtual_select-3538078
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 #3
tom_ek commentedI've pushed the fix to the branch.
Comment #4
kevin.brocatus commented