theme_fieldset() claims to use

element: An associative array containing the properties of the element. Properties used: #attributes, #children, #collapsed, #collapsible, #description, #id, #title, #value.

However #collapsed and #collapsible are never actually used.

Instead, form_process_fieldset() converts #collapsed and #collapsible into classes in #attributes and adds the necessary js to #attached.

Recommendation

Change the documentation for theme_fieldset() to say that it does not use #collapsed and #collapsible, and perhaps mention that creating a collapsible fieldset should be done through creating a render array instead of calling theme('fieldset', ...);

Comments

Status: Needs review » Needs work

The last submitted patch, form.inc_.patch, failed testing.

dj1999’s picture

Version: 7.0 » 7.x-dev
Status: Needs work » Needs review
StatusFileSize
new733 bytes

Again ...

Status: Needs review » Needs work

The last submitted patch, form.inc_.patch, failed testing.

salvis’s picture

Status: Needs work » Needs review
StatusFileSize
new648 bytes

Good work, thanks!

Let's try this...

chalee’s picture

It works for me too. But why is such an important patch not yet committed?

bfroehle’s picture

Category: bug » support
Status: Needs review » Postponed (maintainer needs more info)

All of this work already takes place in form_process_fieldset().

Are you directly calling theme('fieldset', ...) ? I think you'd be better off using drupal_render($element).

bfroehle’s picture

Category: support » bug
Status: Postponed (maintainer needs more info) » Active

Okay, on second thought we should really fix up the docs on theme_fieldset() to say that it doesn't use #collapsed and #collapsible, but instead those should already be set as '#attributes' => array('class' => array('collapsed', 'collapsible')) and that one should generally create a fieldset using drupal_render().

bfroehle’s picture

Title: theme_fieldset() does not handle '#collapsible', '#collapsed' arguments on D7 » theme_fieldset() does not handle '#collapsible', '#collapsed' arguments as claimed
Version: 7.x-dev » 8.x-dev
Issue tags: -form, -collapsed, -fieldset, -collapsible +Documentation, +Needs backport to D7

And let's get the tags right while we are at it...

dqd’s picture

still in the docs ... can't edit ...

geerlingguy’s picture

Subscribe. I was simply trying to use drupal_render() to render a full page, and I created a few fieldsets in the page array, expecting #collapsible and #collapsed to have some effect (they didn't). My original page building code:

  $page['members'] = array(
    '#type' => 'fieldset',
    '#title' => t('Members'),
    '#collapsible' => TRUE, // Doesn't work for regular page arrays.
  );
...
  drupal_render($page);

I had to modify the code to add the library to the page manually, and then set classes on the fieldsets rather than use the fieldsets' actual properties for collapsible/collapsed... surely there's a better way?

  // Allow collapsible form elements.
  drupal_add_library('system', 'drupal.collapse');

  $page['members'] = array(
    '#type' => 'fieldset',
    '#title' => t('Members'),
    '#attributes' => array('class' => array('collapsible')),
  );
...
  drupal_render($page);
chi’s picture

Status: Active » Reviewed & tested by the community

I think we need make theme('fieldset', ...) work properly as the documentation states.

chx’s picture

Assigned: Unassigned » johnalbin
Status: Reviewed & tested by the community » Active

Let me see whether I can get JohnAlbin in here to decide -- do you like collapsible/collapsed processed into classes before theming or should we ask anyone overriding to handle those attributes themselves whether they want classes or not etc?

johnalbin’s picture

Assigned: johnalbin » Unassigned
Status: Active » Needs work

Sorry it took so long to get back to this. I kept trying to find the relevant history for this during random free time. Which I just found. (Thanks for passing this one by me, chx! I think it will save the people affected by this some grief.)

#859602: #cache does not record the #attached declared in child elements shows that you can't use drupal_add_css() and drupal_add_js() inside a theme function because it won't cache properly in the render API cache. The fix in that issue, rightly, did not break an API in D7. Which means the proper fix for this issue in D7 is to correct the documentation for theme_fieldset().

For D8, I would like to see a follow-up issue that deals with the underlying awkwardness. Maybe hook_theme() should register attached js and css?

Also, when is it safe to call drupal_add_css() and drupal_add_js()? How does a themer using a preprocess/process/theme function know if its content will become part of a larger Render API cache? ugh.

avpaderno’s picture

StatusFileSize
new598 bytes

This is the patch that fixes the documentation of the function.

avpaderno’s picture

Status: Needs work » Needs review
avpaderno’s picture

StatusFileSize
new707 bytes

This is the patch for using the #collapsible, and the #collapsed attributes in the code, making the code match the documentation.

I am more in favor of the other patch, as this patch just allows the code to use '#collapsible' => TRUE, instead of '#attributes' => array('class' => array('collapsible')). It would make sense if the CSS classes necessary to make a fieldset collapsible would be changed in the future, but I am not really sure there is the necessity of such code.

andreicio’s picture

but I am not really sure there is the necessity of such code

The fieldset element has the ability to be rendered as collapsible. It's not a behavior that is linked to forms, so why only attach the functionality if the fieldset is within a form?
Also, if you choose, at a later date, to change the method that creates collapsibility, it should be handled by the theming function.

And last but not least: does this patch have a chance of making it into core? I know more changes are needed (the process_fieldset function should no longer deal with collapsibility), but it's a change that does make sense to me.

avpaderno’s picture

It's not a behavior that is linked to forms, so why only attach the functionality if the fieldset is within a form?

Any form field is usually associated with a form, but there isn't anything that says if that is the case, or not.

What I meant is that '#collapsible' => TRUE makes sense when the classes to use to make a fieldset collapsible can change in future. As the code that handles '#collapsible' is not able to add the necessary JavaScript to the page being served, it makes few sense, to use such property.

arnested’s picture

Attached is trivial backport to Drupal 7 that works for me.

arnested’s picture

Well... My naïve Drupal 7 backport i #19 was a bit too naïve...

Although it made fieldsets collapsible it also made fieldsets in vertical_tabs collapsible with the result that collapsed fieldsets in vertical_tabs would be hidden.

The new patch handles that problem by removing #collapsible and #collapsed from elements which are children of a vertical_tabs. That seems kind of messy and is probably not the correct way to handle it.

sun’s picture

Version: 8.x-dev » 7.x-dev
salvis’s picture

salvis’s picture

Status: Needs review » Needs work
Issue tags: -Needs backport to D7

#4 still works, but JohnAlbin writes in #13:

the proper fix for this issue in D7 is to correct the documentation for theme_fieldset()

So this is working as designed and the work-around is given in the summary.

rooby’s picture

It is also worth noting in the doco that another work around is to pass in the collapsible classes like:

$element['#attributes']['class'][] = 'collapsible';
$element['#attributes']['class'][] = 'collapsed';
rooby’s picture

Issue summary: View changes

Add issue summary

arnested’s picture

Issue summary: View changes
DrCord’s picture

So in some cases it needs both '#collapsible' => TRUE and

$element['#attributes']['class'][] = 'collapsible';
$element['#attributes']['class'][] = 'collapsed';

Status: Needs work » 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.