Make #group FAPI / render feature work on all form/render #types out of the box

Created on 6 February 2014, almost 11 years ago
Updated 4 September 2023, about 1 year ago

Split from #2177469: Move node base widgets to the top level of the form β†’

Currently, an $element that wants use the #group feature needs to make sure 'form_process_group' & 'form_pre_render_group' run as part of its processing & rendering.

So far, only 'container' types specify those in their hook_element_info() definition : fieldset, details, container
Patch linked above adds it to: datetime, textfield, textarea, checkbox as well, which sounds a bit arbitrary, and not too scalable.

Ideally #group should "just work" on any element type, not just on the ones that provision for it.
form_process_group() & form_pre_render_group() should just run on every sub-element in a form / render array as part of the default process / render stack.
No biggie for form_pre_render_group(), it's an early opt-out if $element['#group'] is not set. form_process_group() might need some refactoring to avoid a perf hit.

Problem / current behavior:

Drupal FAPI doesn't support visually grouping fields without affecting the field / form data structure as it requires nesting the fields, which is then reflected in the data structure:

    $form['contact'] = [
      '#type' => 'details',
      '#title' => t('Contact details'),
      '#open' => TRUE,
    ];    

    $form['contact']['name'] = [
      '#title' => $this->t('Name'),
      '#description' => $this->t('Enter your name.'),
      '#type' => 'textfield',
      '#default_value' => $this->options['name'],
    ];

Proposed solution:

Additonally, allow to group FAPI elements only visually, without affecting the field / form data structure. So that both ways are supported:

    $form['contact'] = [
      '#type' => 'details',
      '#title' => t('Contact details'),
      '#open' => TRUE,
    ];    

    $form['name'] = [
      '#title' => $this->t('Name'),
      '#description' => $this->t('Enter your name.'),
      '#type' => 'textfield',
      '#group' => 'contact',
      '#default_value' => $this->options['name'],
    ];

TODO:

  1. Implement and write tests
  2. Update documentation from https://www.drupal.org/node/262758 β†’ (Drupal 6) for Drupal 8+
πŸ“Œ Task
Status

Needs work

Version

11.0 πŸ”₯

Component
FormΒ  β†’

Last updated 2 days ago

Created by

πŸ‡«πŸ‡·France yched

Live updates comments and jobs are added and updated live.
  • Needs documentation

    A documentation change is requested elsewhere. For Drupal core (and possibly other projects), once the change has been committed, this status should be recorded in a change record node.

Sign in to follow issues

Comments & Activities

Not all content is available!

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

  • πŸ‡ͺπŸ‡ΈSpain Carlitus

    Tested and for the moment it works well.

    Without a Checkbox, for example, works well with a details parent but not a Radios with a details parent.

    With the #20 patch it works well with the Radios child.

    I think getting this is a big step for Drupal, since as @Anybody says many times you can't modify the form structure but you do need to be able to group it.

  • πŸ‡©πŸ‡ͺGermany Anybody Porta Westfalica

    @Grimreaper and @Pancho any ideas how we can take this issue forward and perhaps get some frontend framework manager feedback on this?

    I think before we make the next coding steps here, we should get some feedback from the core team, what they think about the key aspects?

  • πŸ‡ͺπŸ‡ΈSpain Carlitus

    I've see that this doesn't work with managed_file and i don't know why. The processGroup and preRenderGroup is added like in the other form elements.

  • πŸ‡ͺπŸ‡ΈSpain Carlitus

    New patch to support managedFile and Ajax Callback.

    I think this is not the best way to do it, but now al least works in my manual tests. I am sure that a person with more control of the core can do it in the most appropriate way.

    I've see that the problem was when in the ajax callback to upload the file, the preRenderGroup was not needed and i've make a unset of the #groups in uploadAjaxCallback to skip this function.

  • πŸ‡ͺπŸ‡ΈSpain Carlitus

    New patch, the last one was wrong

Production build 0.71.5 2024