Field layouts doesn't seem to work for embedded forms

Created on 9 August 2018, over 6 years ago
Updated 21 July 2023, over 1 year ago

Problem

The Field Layout module currently puts the regions in renderable groups keyed by the region's machine name.

However, Drupal renders groups using a #group key that is based on imploded #parents of the form elements. This means that the Field Layout will work fine in a standalone entity form, but (as far as I can see) will never work in an embedded form such as the ones used by Inline Entity Form and Paragraphs.

Motivation

  • In FieldLayoutBuilder::buildForm():110 the group key is set to the region machine name. $build[$name]['#group'] = $field['region'];.
  • In RenderElement::processGroup():436 A new ‘#groups’ key is added to the form element that holds all possible groups in the form keyed by the imploded parents of the element.
    $parents = implode('][', $element['#parents']);
    
        // Each details element forms a new group. The #type 'vertical_tabs' basically
        // only injects a new details element.
        $groups = &$form_state->getGroups();
        $groups[$parents]['#group_exists'] = TRUE;
        $element['#groups'] = &$groups;
    

    After that form elements that should go in that group are added by reference, but this time by looking up the ‘#group’ key defined in the previous step.

     // Process vertical tabs group member details elements.
        if (isset($element['#group'])) {
          // Add this details element to the defined group (by reference).
          $group = $element['#group'];
          $groups[$group][] = &$element;
        }
    
  • In RenderElement::preRenderGroup():160 the groups children are rendered based on the imploded parents of the parent element.
    $parents = implode('][', $element['#parents']);
    $children = Element::children($element['#groups'][$parents]);
    

Conclusion: since not the imploded parents are used as #group key, the fields will never be rendered when there are parents (e.g. when they are an embedded entity form).

Proposed resolution

Change the ‘#group’ key to the imploded parents + the region name.

Remaining tasks

- Write a patch
- Add tests

🐛 Bug report
Status

Fixed

Version

10.1

Component
Field layout 

Last updated 15 days ago

No maintainer
Created by

🇪🇸Spain nuez Madrid, Spain

Live updates comments and jobs are added and updated live.
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.

Production build 0.71.5 2024