Problem/Motivation
When all the fields of a "Component" field group are empty, the group is still rendered, even with "Display element also when empty" unchecked. Its parent groups are not removed either, so an empty wrapper (label, heading...) is displayed.
field_group removes empty groups in field_group_remove_empty_display_groups(), after the formatters preRender(). This function checks the children of the group. But ComponentFormatter::preRender() moves the children into the component render array, so the group always has a non-empty component child and field_group cannot see that it is empty.
Steps to reproduce
- Install field_group, ui_patterns 2 and ui_patterns_field_group 2.
- On a content type display, add a "Details" or "HTML element" group, and inside it a "Component" group.
- Map a field to a slot of the component with the "Field group child" source.
- Keep "Display element also when empty" unchecked on both groups.
- View a node where this field is empty.
- The component and the parent group are rendered, without content.
Proposed resolution
In ComponentFormatter::preRender(), when "Display element also when empty" is unchecked and the group has no visible, non-empty children, return before the component is built. The group keeps its empty children, so field_group removes it and its empty parents as expected.
The new isEmptyGroup() method uses the same check as field_group_remove_empty_display_groups() (recursive on child groups, #weight ignored).
The MR adds a kernel test, EmptyComponentGroupTest: a component group inside an HTML element group, rendered with and without field value. Without the fix, the test fails because the empty groups are rendered.
Remaining tasks
- Review the MR.
Issue fork ui_patterns_field_group-3633464
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 #3
julien tekrane commented