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

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

tom_ek created an issue. See original summary.

tom_ek’s picture

Assigned: tom_ek » Unassigned
Status: Active » Needs review

I've pushed the fix to the branch.

kevin.brocatus’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

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