Putting markup in a form in a collapsible fieldset doesn't work well unless it's wrapped in a <p> or <div> (apparently). If I put just text, the text falls outside the collapsed region and still shows when the fieldset is collapsed.

Sample code:

function mymodule_form_alter(&$form, $form_state, $form_id) {
  if (isset($form['#node']) && $form['#node']->type .'_node_form' == $form_id) {
    $form['ggg'] = array(
      '#type' => 'fieldset',
      '#title' => t('Test'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#tree' => TRUE,
      '#weight' => -2,
    );    
    $form['ggg']['node_type'] = array(
      '#type' => 'select',
      '#title' => t('Node type'),
      '#options' => node_get_types('names'),
      '#suffix' => 'hello!',
      );    
    $form['ggg']['hoho'] = array(
      '#value' => 'hohoho',
      );
  }
}

resulting HTML page source:

<fieldset class=" collapsible collapsed"><legend>Test</legend><div class="form-item" id="edit-ggg-node-type-wrapper">
 <label for="edit-ggg-node-type">Node type: </label>
 <select name="ggg[node_type]" class="form-select" id="edit-ggg-node-type" ><option value="booo">Book page</option><option value="page">Page</option><option value="story">Story</option></select>
</div>
hello!hohoho</fieldset>

The problem may be this code in collapse.js:

.after($('<div class="fieldset-wrapper"></div>').append(fieldset.children(':not(legend)')));

but in mucking around, I haven't figured out how to make it work

Comments

keith.smith’s picture

Title: collapsible fieldset probelms with #prefix, #suffix, markup rendering » collapsible fieldset problems with #prefix, #suffix, markup rendering
soxofaan’s picture

Subscribing.
Issue also exists in Drupal 5.

Workaround (from http://drupal.org/node/37070): use '#type' => 'item' instead of 'markup' (the default)

theborg’s picture

Status: Active » Needs review
StatusFileSize
new624 bytes

The code that pwolanin told about was the cause, children() function doesn't include text nodes.
Changed to use contents().

gábor hojtsy’s picture

From the jQuery docs, this indeed looks logical. Need some testing.

theborg’s picture

Version: 6.x-dev » 7.x-dev
StatusFileSize
new624 bytes

Still applies. Moved to d7.

cakka’s picture

Hi, any demo for this ?
Thanks

Anonymous’s picture

Status: Needs review » Needs work

The last submitted patch failed testing.

janusman’s picture

Subscribing

alienzed’s picture

subscribing@

cwgordon7’s picture

Status: Needs work » Needs review
Issue tags: +Quick fix
StatusFileSize
new732 bytes

Marked #570010: Text nodes within collapsible fieldsets don't collapse a duplicate of this.

Here's an updated patch. Note that .contents() does not have an expression parameter as .children() does, as it makes little sense to use jQuery to filter out text nodes. So I used the .filter() function in addition to the .contents() function to accomplish this.

Rfay recently convinced me to use git for core patches, so I'm trying it out and this is my first patch created using git, so sorry if it doesn't apply properly, I'm still learning how to use it. Assuming the test bot passes it, however, this should be good to go. Marking this as a quick fix as well.

kwinters’s picture

I don't know enough jQuery to say for sure this is the best way to do it, but I can confirm that it works as advertised.

mattyoung’s picture

subscribe

chx’s picture

Status: Needs review » Needs work

shorter return this.nodeType != 1 || $(this).is(':not(legend):not(.action)'); and the code comment does not help much, I clarified with cwgordon7 that he wanted to say If this is an HTML element, not a text node

kkaefer’s picture

The code comment should be something like: “jQuery’s .children() function only considers element DOM nodes but ignores text nodes completely. Therefore, we have to filter manually to include all non-element DOM nodes”.

cwgordon7’s picture

Status: Needs work » Needs review
StatusFileSize
new887 bytes

Thanks, rerolled with more comments and shorter logic.

kkaefer’s picture

Status: Needs review » Needs work

I’m getting this error in Safari 4: "Error: HIERARCHY_REQUEST_ERR: DOM Exception 3"

casey’s picture

Version: 7.x-dev » 6.x-dev
kwinters’s picture

Status: Needs work » Closed (duplicate)

Since this was already fixed in another issue, marking this duplicate.

I honestly don't think backporting this to D6 is going to be feasible, but if that's what you want to do then mark the other issue as patch (to be ported).

lyricnz’s picture

Version: 6.x-dev » 7.x-dev
Status: Closed (duplicate) » Active
Issue tags: -Quick fix
StatusFileSize
new36.38 KB
new14.05 KB

This is not fixed. If a collapsed fieldset just contains item/markup contents, then it is not shown correctly. For example, the following $form does not display correctly, but if the #collapsed line is commented out, it works fine.

  $form = array (
    'start' => array (    
      '#tree' => true, 
      '#type' => 'fieldset',
      '#collapsed' => true, // change this line
      '#collapsible' => true,  
      'system' => array (      
        '#type' => 'item',
        '#title' => 'system module',
        '#markup' => '<em>system</em> module can not be updated. Its schema version is 0. Updates up to and including 7069 have been removed in this release. In order to update <em>system</em> module, you will first <a href="http://drupal.org/upgrade">need to upgrade</a> to the last version in which these updates were available.',
        '#prefix' => '<div class="messages warning">',
        '#suffix' => '</div>',
      ),
    ),
  );

Still happens with D8 also.

mgifford’s picture

Version: 7.x-dev » 8.0.x-dev
Issue tags: +Needs backport to D7
Related issues: +#1159160: Error messages from update.php sometimes hidden

I assume this is an issue still. It is referenced in a todo in a patch here.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev
quietone’s picture

Version: 9.3.x-dev » 7.x-dev
Issue tags: -Needs backport to D7 +Bug Smash Initiative

Tested on Drupal 9.4.x, standard install and I was not able to reproduce the problem. I tested with the code in the Issue Summary and #19, using '#type' => 'details'. This is still a problem on Drupal 7 and I didn't find a duplicate issue, so changing version.

Cheers,

.

quietone’s picture

I seemed to have omitted that testing was done with '#type' => 'fieldset' as well.

Status: Active » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.