SubformState should complain when the subform doesn't have #tree = TRUE

Created on 10 May 2019, over 5 years ago
Updated 22 August 2024, 17 days ago

Problem/Motivation

SubformState doesn't work properly if the subform doesn't have #tree = TRUE. That's because without that, the subform's form values are the top of the values array, and SubformState::getValues() won't find them.

So if you do something like this:

  public function submitForm(array &$form, FormStateInterface $form_state) {
    $subform_state = SubformState::createForSubform($form['plugin_form'], $form, $form_state);
    $plugin->submitConfigurationForm($form['plugin_form'], $subform_state);

then the plugin's submitConfigurationForm() won't find the values it expects.

Proposed resolution

Given this is a requirement for SubformState to work properly, I think it should throw an exception in createForSubform() if $form is non-empty and doesn't have #tree set.

It needs to allow an empty array, because in buildForm() you typically do this:

      $form['plugin_form'] = [];
      $subform_state = SubformState::createForSubform($form['plugin_form'], $form, $form_state);
      $form['plugin_form'] = $action->buildConfigurationForm($form['plugin_form'], $subform_state);
      $form['plugin_form']['#tree'] = TRUE;

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

๐Ÿ› Bug report
Status

RTBC

Version

11.0 ๐Ÿ”ฅ

Component
Formย  โ†’

Last updated less than a minute ago

Created by

๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom joachim

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

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024