This wee patch assigns an id to a fieldset that is equal to its #title attribute. That makes it easier to latch onto it for CSSing if you only want to modify a single fieldset.

For instance, I have several multi-select taxonomy categories and want them to line up horizontally to save vertical screen space, but don't want to muck up any other fieldsets. This would allow a simple CSS rule:

#Categories .form-item { float: left; }

I'm pretty sure it doesn't break anything else. :-)

Comments

drumm’s picture

Status: Needs review » Needs work

"ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".")." http://www.w3.org/TR/html4/types.html#type-name

I don't think #title will consistently fulfill that at all.

Crell’s picture

Version: x.y.z » 6.x-dev
Status: Needs work » Closed (won't fix)

You know, I'm pretty sure this can be done with form_alter these days, so let's just do that and be done with it.

ultimateboy’s picture

Version: 6.x-dev » 7.x-dev
Assigned: Crell » Unassigned
Category: task » feature
Status: Closed (won't fix) » Needs review
StatusFileSize
new916 bytes

Crell #2: I actually think that this is an issue.. almost every form-item has an id, except for fieldsets, and this is not very consistent.

drumm #1: I have re-written the patch using form_clean_id so that the string is of the correct format.

robloach’s picture

Should the drupal_strtolower call become part of the form_clean_id() function?

ultimateboy’s picture

StatusFileSize
new1.22 KB

I believe it should. Although the w3c does not specifically state so, it has become a standard throughout drupal and the web development community in general. I have attached a patch which still adds ids to fieldsets but also alters the form_clean_id function.

Zarabadoo’s picture

I agree with UltimateBoy. It looks to be an unsaid standard in Drupal anyway, so this will just give things that extra level of consistency.

robloach’s picture

StatusFileSize
new1.25 KB

I thought we should be nice to both $element['#id'] and $element['#attributes']['id'] if they already exist.

ultimateboy’s picture

Status: Needs review » Reviewed & tested by the community

Ah yes.. good point. Applies cleanly and gets the job done. This is a simple enough patch, so I am marking as rtbc.

robloach’s picture

Status: Reviewed & tested by the community » Needs work

Shouldn't it be using the fieldset name instead of the title? Since the title can be translated, it makes the ID kind of useless.

robloach’s picture

Status: Needs work » Needs review
StatusFileSize
new1.23 KB

This adds a #key property to all the form elements, and then uses that to construct the ID of fieldsets. I was having a look at #name and #id, but we can't really use those because they're required special use cases for input elements.

ultimateboy’s picture

I like this patch, but is there a reason why you removed the drupal_strtolower call from form_clean_id()?

robloach’s picture

I removed the call to drupal_strtolower from the previous patch because now it's using the element's actual key rather then the element's title. So, there won't be any capitals being passed through it anyway. The call therefore isn't required.

The form element's key is what it's called when using $form['myelement']....The key for $form['myelement'] is "myelement".

Status: Needs review » Needs work

The last submitted patch failed testing.

dalad’s picture

Linking the fieldset id to the title is not recommended IMO. What if the site is multilanguage? A better approach would be using a static variable in the theme_fieldset method that increments itself every time a fieldset is placed. This is what I came out with (tested D6)

function theme_fieldset($element) {
  static $fieldsetNum = 0;
  if (!empty($element['#collapsible'])) {
    drupal_add_js('misc/collapse.js');

    if (!isset($element['#attributes']['class'])) {
      $element['#attributes']['class'] = '';
    }

    $element['#attributes']['class'] .= ' collapsible';
    if (!empty($element['#collapsed'])) {
      $element['#attributes']['class'] .= ' collapsed';
    } 
  }
  
  $element['#attributes']['class'] .= " fieldset-".$fieldsetNum++;

  return '<fieldset'. drupal_attributes($element['#attributes']) .'>'. ($element['#title'] ? '<legend>'. $element['#title'] .'</legend>' : '') . (isset($element['#description']) && $element['#description'] ? '<div class="description">'. $element['#description'] .'</div>' : '') . (!empty($element['#children']) ? $element['#children'] : '') . (isset($element['#value']) ? $element['#value'] : '') ."</fieldset>\n";
}

Cheers

robloach’s picture

The patch I posted does not use the title, it uses the form's key name instead, which is always the same no matter if the site is multilingual. The form_clean_id function that is used in the patch manages that static variable numbering that you're talking about for us.

ultimateboy’s picture

Status: Needs work » Needs review
StatusFileSize
new1.3 KB

Re-roll.

moshe weitzman’s picture

Status: Needs review » Reviewed & tested by the community

looks nice to me. bot is happy so rtbc

webchick’s picture

Status: Reviewed & tested by the community » Needs review

I wonder if it makes sense to prefix the ID with something. I imagine if you named your fieldset 'messages' or 'content' or something similar we'd end up with duplicate IDs on the page.

I'd also love to get a FAPI guru to ensure the additions to form_builder() are okay.

moshe weitzman’s picture

form_clean_id() ensures uniqueness and is used in this patch.

chx’s picture

Status: Needs review » Needs work

That's some mighty convoluted patch. Why are we not setting #id right where this patch hands down key instead? This would give all #input FALSE an #id.

casey’s picture

robloach’s picture

Status: Needs work » Fixed

form_process_fieldset injects:

  $element['#attributes']['id'] = $element['#id'];

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

zualas’s picture

Not sure which patch had a green light, but what I did is put the div id equal to class. To do that, I inserted the following code in lines 1515-1517 in form.inc:

if (!isset($element['#attributes']['id'])) {
      $element['#attributes']['id'] = 'fieldset-' . $element['#attributes']['class'];
}

I'm not a programmer so I'm not sure if it's the best way, but it doesn't rely on the #title so there will be no problems with i18n.

dunx’s picture

A bit of info if you're trying to add an id to a location fieldset. You can't add the id to the locations part of the $form, but instead have to add it to the location array under it. So:

$form['locations'][0]['#attributes']['id'] = t('location');

Rather than what I thought was more obvious:

$form['locations']['#attributes']['id'] = t('location');