Problem/Motivation
After upgrading from 8.x-1.x to 8.x-3.x empty field groups are now displaying.
Steps to reproduce
- Set up form display to have tabs
- Create a tab and give it a field group label and a description
- Make sure 'Display element also when empty' is unchecked.
- Put a fieldset in the tab
- Put a field in the fieldset
- Install the field_permissions module.
- Edit the field that you put in the tab and change the field visibility and permissions to 'Custom'.
- Change all permissions on the field to have the checkboxes checked for the administrator.
- 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?
Issue fork field_group-3266900
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
Comment #2
rob230 commentedWhat 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 toFALSE). 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.
Comment #3
rob230 commentedUpdated steps to reproduce.
Comment #4
rob230 commentedThis is tricky to fix because
$elementis 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.Comment #5
socialnicheguru commentedComment #6
ded commentedI've hit a similar issue with a profile element nested in a Tab. Unfortunately, the patch doesn't work.
Comment #9
refsI 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.
Comment #10
refsComment #11
edde20 commentedComment #12
anybodyComment #13
ady1503 commentedHello.
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
Comment #14
mjpa commentedAdded a re-roll to apply to 8.x-3.6 based on patch in #4
Comment #15
weseze commentedPatch #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?
Comment #16
mjpa commentedThe 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?
Comment #17
mjpa commentedComment #18
refsRebased 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
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.
Comment #19
anybodyComment #20
refsFinally updated to 4.0 and my empty groups are hidden without the patch.