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
Comment #1
keith.smith commentedComment #2
soxofaan commentedSubscribing.
Issue also exists in Drupal 5.
Workaround (from http://drupal.org/node/37070): use '#type' => 'item' instead of 'markup' (the default)
Comment #3
theborg commentedThe code that pwolanin told about was the cause,
children()function doesn't include text nodes.Changed to use
contents().Comment #4
gábor hojtsyFrom the jQuery docs, this indeed looks logical. Need some testing.
Comment #5
theborg commentedStill applies. Moved to d7.
Comment #6
cakka commentedHi, any demo for this ?
Thanks
Comment #7
Anonymous (not verified) commentedThe last submitted patch failed testing.
Comment #8
janusman commentedSubscribing
Comment #9
alienzed commentedsubscribing@
Comment #10
cwgordon7 commentedMarked #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.
Comment #11
kwinters commentedI 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.
Comment #12
mattyoung commentedsubscribe
Comment #13
chx commentedshorter
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 nodeComment #14
kkaefer commentedThe 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”.
Comment #15
cwgordon7 commentedThanks, rerolled with more comments and shorter logic.
Comment #16
kkaefer commentedI’m getting this error in Safari 4: "Error: HIERARCHY_REQUEST_ERR: DOM Exception 3"
Comment #17
casey commentedFixed in D7: #676800: Fieldsets break design badly
Comment #18
kwinters commentedSince 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).
Comment #19
lyricnz commentedThis 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.
Still happens with D8 also.
Comment #20
mgiffordI assume this is an issue still. It is referenced in a todo in a patch here.
Comment #31
quietone commentedTested 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,
.
Comment #32
quietone commentedI seemed to have omitted that testing was done with
'#type' => 'fieldset'as well.