Problem/Motivation

After upgrading from 8.x-1.x to 8.x-3.x empty field groups are now displaying.

Steps to reproduce

  1. Set up form display to have tabs
  2. Create a tab and give it a field group label and a description
  3. Make sure 'Display element also when empty' is unchecked.
  4. Put a fieldset in the tab
  5. Put a field in the fieldset
  6. Install the field_permissions module.
  7. Edit the field that you put in the tab and change the field visibility and permissions to 'Custom'.
  8. Change all permissions on the field to have the checkboxes checked for the administrator.
  9. Login as a non-admin who has permission to use the form and visit it

Expected results:
Since the field isn't shown, this tab group is empty, therefore it should be hidden (this was the behaviour in 8.x-1.x).

Actual results:
The tab is shown and has nothing in it.

Cause

This bug was introduced in #2994053: Field Group not working on node edit inside paragraph (Version 8.3), specifically this commit: https://git.drupalcode.org/project/field_group/-/commit/ef9a94f222f33ff4...

The previous behaviour was to call field_group_remove_empty_form_groups() if the child is a group, and then the group will have its children checked and if there aren't any, its access will be set to FALSE.

The new code doesn't do this, which means the only check is this:

$empty_element = !(isset($element[$childname]['#type']) || isset($element[$childname]['#markup']) || in_array($exception, $exceptions));

So if the child element has a '#type' then it will be considered not empty. This is always true. A tab has a type of 'details' so it can never be empty. As such, the tab will always be shown regardless of whether there is anything in it.

Proposed resolution

Shouldn't this code have some kind of check to see whether the tab has visible children in it?

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

Rob230 created an issue. See original summary.

rob230’s picture

What the code is currently trying to do seems to be assume the group is empty, and then look at the children and if it finds one, mark that group as not empty.

I've realised why the issue occurs, and the steps to reproduce won't work. The issue occurs when groups are nested. So in my example, a tab contains a field set. The field set is a visible element so gets found by Element::getVisibleChildren() (even if its children all have '#access' set to FALSE). Then because its '#type' is 'fieldset' it sets the group containing it to not empty.

I think the only way to solve this is to make the function recursive again.

rob230’s picture

Issue summary: View changes

Updated steps to reproduce.

rob230’s picture

This is tricky to fix because $element is a flat list rather than nested. But I've done it with a separate recursive function that determines if a group is empty or not. It checks if a child is in the list of groups, and if so, looks at all the elements which are listed as a child of that group and sees whether they are empty.

socialnicheguru’s picture

Status: Active » Needs review
ded’s picture

Status: Needs review » Needs work

I've hit a similar issue with a profile element nested in a Tab. Unfortunately, the patch doesn't work.

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

refs’s picture

I refactored the code from @Rob320 a bit and reduced the loops.

The structure of the check is now also more similar to field_group_remove_empty_display_groups.

I tested it with nested paragraphs, each of which has nested field groups.

refs’s picture

Status: Needs work » Needs review
edde20’s picture

anybody’s picture

Version: 8.x-3.x-dev » 4.x-dev
ady1503’s picture

Hello.

It still doesn't work.
With the latest update it does not work to hide the group of fields if they are empty. I have tried both versions, the stable one and the dev version with the patch.
The empty group fields continues to be seen.

Thank you

mjpa’s picture

Added a re-roll to apply to 8.x-3.6 based on patch in #4

weseze’s picture

Patch #14 is not working for us on version 3.6. All field groups are always empty...
Tried without the patch and it seems to be working just fine. Not sure why the patch is still needed?

mjpa’s picture

The patch in #14 was incorrect, a bad re-roll on my part. I've fixed this - as to whether the patch is needed or not, I don't know - I'm just updating a project using it so better to keep the change in, than removing it.

The field permissions part of the STR to might be key though?

mjpa’s picture

refs’s picture

Rebased MR on current main. The rebase reverts code changes done in #3098550: Nested groups render on other entity forms (or when fields are inaccessible) when they shouldn't.
The added test still passes.

#3098550: Nested groups render on other entity forms (or when fields are inaccessible) when they shouldn't introduced also a check for empty field groups

// If the element is a group, check if it is empty or not.
$empty_element = in_array($child_name, $groups) ? $empty_groups_indication[$child_name] : $empty_element;

I don't know if this Issue is still needed. 3098550 sorts the children and first checks the fields and then the groups if they are empty. This MR checks the fields and groups recursive using the group hierarchy.

I will update my projects to field_groups 4 soon and then check them without the patch from this MR.

anybody’s picture

refs’s picture

Finally updated to 4.0 and my empty groups are hidden without the patch.