Problem/Motivation

After updating to v4.0, one of the nested field groups on a node does not display even though it is not empty.

After reviewing the differences between v4.0.0 and v8.x-3.6, I found that the changes in Nested groups render on other entity forms (or when fields are inaccessible) when they shouldn't were the cause.

The code loops through an array that starts with the fields, then groups. For each of them it checks if it's in the group list that was passed. If it is, it checks the $empty_groups_indication array to see if that group is set as an empty element. In order for a group to not be an empty element, one of its children (field or group) needs to have come before it in the list. In the case of nested groups, if a group from the middle of the nest comes first, none of it's children will have set its $empty_groups_indication entry to false. Then, the next part of the code that would set its parent group to false (not empty) does not run field_group.module, line 1063. That parent then is never set to not empty, causing it and its children not to be displayed

Example structure:

Tabs Group 1
--Child Group (Tab)
----Field
--Child Group (Tab) <--- At the end of the process this group considered empty, but it has children with fields
----Tabs Group 2 <--- After all the fields, this group is checked first. It's 2 child groups have not run yet, so it is treated as empty. The next bit of code to set the parent to not empty doesn't run.
------Child Group (Tab)
--------Field
--------Field
------Child Group (Tab)
--------Field
--------Field
--Child Group (Tab)
----Field

The order the field groups are processed in is determined by the array returned by Element::getVisibleChildren($element). Most of the time, it returns the children in an order that works with the process of marking groups as empty. Sometimes it does not.

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

redneko created an issue. See original summary.

alorenc made their first commit to this issue’s fork.

alorenc’s picture

Assigned: Unassigned » alorenc

alorenc’s picture

I was able to replicate the issue consistently.

I created the following nested group hierarchy:
- Level 1 (parent of Level 2)
- Level 2 (parent of Level 3)
- Level 3 (contains the body field)

I then reorganized the structure using the Field Group UI:
- Level 3 becomes the top-level group
- It contains Level 2
- Which contains Level 1
- And Level 1 contains the body field

I exported the configuration and noticed. The weight values were correctly updated. However, the field order in the YAML config remained unchanged, still reflecting the original nesting order.

It looks like the Field Group module builds the field group hierarchy based on the order of items in the YAML configuration file, instead of relying solely on the weight and parent_name properties.

This becomes problematic when reversing the hierarchy (e.g., changing a parent into a child or vice versa), because it can impact the visibility of the element

alorenc’s picture

The function field_group_remove_empty_form_groups expects that groups are ordered from parent to child
Added an additional function which orders groups.

alorenc’s picture

Assigned: alorenc » Unassigned
Status: Active » Needs review
redneko’s picture

Thank you for the work alorenc.

I tested the fix on the site were I found the issue. The missing field group and its children are now displaying, and I did not find any evidence that this fix caused any new issues. I tried making new nodes with different configurations of field groups and found everything to work as expected.

The coding itself looked good to me, but I am not confident in this area. I will leave this issue in 'Needs review' so someone else can check that, and also corroborate that the fix works.

weseze’s picture

Can confirm this issue.
When updating our setup to 4.x a lot of our field groups disappeared.
The MR fixes this mostly, but not entirely. 1 edge case remains for us. (this was working in the previous version)

Consider this setup:

- field_group1
  - field_subgroup1 (render when empty is checked here)

This will not render anything, while I expected it to render fully since the subgroup is marked to render if it is empty (which it is).
It might look like a weird setup, but we use this in combination with the option to render a custom html-tag instead of an actual "group of fields". Also this is an extremely simplified example. Our actual setup contains dozens of nested field groups and a lot of custom classes and javascript processing...

weseze’s picture

I have tried to fix this locally, but I am not sure this is entirely correct...

If you change this:

      $show_empty_fields = isset($element[$child_name]['#show_empty_fields']) && $element[$child_name]['#show_empty_fields'];
      if ($show_empty_fields) {
        $empty_groups_indication[$child_name] = FALSE;
      }

to this:

      $show_empty_fields = isset($element[$child_name]['#show_empty_fields']) && $element[$child_name]['#show_empty_fields'];
      if ($show_empty_fields) {
        $empty_groups_indication[$child_name] = FALSE;

        // If one of the parents is a group, mark that one as not empty as well.
        if ($element[$child_name]['#group']) {
          $parents = explode('][', $element[$child_name]['#group']);
          if (is_array($parents)) {
            foreach ($parents as $parent_name) {
              if (in_array($parent_name, $groups)) {
                $empty_groups_indication[$parent_name] = FALSE;
              }
            }
          }
        }
      }

it seems to work correctly (for me)...

steinmb’s picture

If we define #9 as a valid config, then the tests needs to be updated to fail on this type of config. Personally I have I never created such a config and my knee jerk reaction to it is, its sort of a hack.

weseze’s picture

StatusFileSize
new7.26 KB

I understand it is a bit weird, but perfectly possible... And, for us at least, it is very very handy feature to be able to put html tags with classes in the form without custom code.

Attached is an updated patch that fixes this use case.

refs’s picture

After reading through the issue, I believe that the MR for 3266900 - Hiding empty field groups in form display is not working should resolve the problem. Probably needs a rebase.

The code from 3266900 is based on field_group_remove_empty_display_groups() and also takes show_empty_fields into account.

benstallings’s picture

Status: Needs review » Closed (duplicate)
Related issues: +#3266900: Hiding empty field groups in form display is not working

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

le72’s picture

I have exactly same situation as described in "Problem/Motivation". Still see the problem. The nested fieldgroups dissapear. Why this issue is closed? Where is the fix?

le72’s picture

Status: Closed (duplicate) » Needs work
artyom hovasapyan’s picture