Hide group if all fields are hidden by javascript

Created on 11 June 2018, over 6 years ago
Updated 1 June 2023, over 1 year ago

Hi,

Problem/Motivation

Faced with issue that when add conditional fields dependency to hide/show fields inside the group, group is always visible.

Would be grate to have a feature that hide group when all fileds inside of it are hidden.

🐛 Bug report
Status

Needs review

Version

3.0

Component

Miscellaneous

Created by

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.

  • Open in Jenkins → Open on Drupal.org →
    Core: 10.1.x + Environment: PHP 8.1 & MySQL 5.7
    last update over 1 year ago
    17 pass
  • 🇫🇮Finland sokru

    This should resolve the Drupal 10 issues.

  • 🇧🇪Belgium DuneBL

    This patch is nice and working well except if we are adding fields in an empty group in the hook form_alter.
    I am using the following function to add $form[$field_name] to a group (id=$group_name):

    function addToGroup(array &$form, string $field_name, string $group_name)
    {
      /** @var EntityFormDisplay $form_display */
      $form_display = $form['#process'][1][0];
      $form[$field_name]['#group'] = $group_name;
      $field_group = $form_display->getThirdPartySettings('field_group')[$group_name];
      $field_group['children'][] = $field_name;
      $form_display->setThirdPartySetting('field_group', $group_name, $field_group);
      }
    }
    

    If I add $form['field_to_add'] in a group which is empty but which have the option "Display element also when empty" checked; then the group is not displayed.

    To summarize, there are 2 issues:
    1-The patch doesn't take into account the option Display element also when empty
    2-The patch doesn't take into account any element added to the group in the form_alter hook

  • 🇫🇷France prudloff Lille

    The patch works correcly on initial load, but it does not seems to work correctly when detecting a change.
    We have a fieldset containing 3 fields that can be shown/hidden depending on when another checkbox field is checked (we use conditional_fields for this).
    The field group is initially hidden but if I make the fields visible then hide them, the field group is not hidden and is displayed empty.

    This seems to happen because when Drupal.FieldGroup.hideGroupIfEmpty() evaluate the display property of the fields, the last does not have none yet. So it might be some kind of race condition that makes it check the field too soon.
    If I do something like this, then it works correctly:

    diff --git a/formatters/fieldset/fieldset.js b/formatters/fieldset/fieldset.js
    index 7349be4..3a2f052 100644
    --- a/formatters/fieldset/fieldset.js
    +++ b/formatters/fieldset/fieldset.js
    @@ -34,7 +34,9 @@
             if ($(e.target).hasClass('field-group-child-field')) {
               let $group = $(e.target).closest('.field-group-fieldset');
               if ($group.length !== 0) {
    -            Drupal.FieldGroup.hideGroupIfEmpty($group, null, true);
    +            setTimeout(function () {
    +              Drupal.FieldGroup.hideGroupIfEmpty($group, null, true);
    +            }, 500);
               }
             }
           });
    

    (This is obviously not the way to the fix the problem, but it helps demonstrate it.)

  • Status changed to Needs work 1 day ago
  • 🇫🇷France MΙΧALIΣ Nantes

    Patch failed on field_group 3.6 and Drupal 10.3

Production build 0.71.5 2024