Adding IDs to all Fieldsets

A themer on a project I was recently working on needed a lot of CSS IDs added to fieldsets throughout the site.

Fortunately, there's a theme_fieldset() function in includes/forms.inc

I was able to copy that function into the phptemplate.php file, obviously change theme_ to phptempalte_ in the function name, and then add logic that adds an ID based on the fieldset name if an id does not already exist.

Here's the code:

  1. /**
  2.  * Format a group of form items.
  3.  *
  4.  * @param $element
  5.  * An associative array containing the properties of the element.
  6.  * Properties used: attributes, title, value, description, children, collapsible, collapsed
  7.  * @return
  8.  * A themed HTML string representing the form item group.
  9.  *
  10.  * @ingroup themeable
  11.  */
  12. function phptemplate_fieldset($element) {
  13. if (!empty($element['#collapsible'])) {
  14. drupal_add_js('misc/collapse.js');
  15.  
  16. if (!isset($element['#attributes']['class'])) {
  17. $element['#attributes']['class'] = '';
  18. }
  19.  
  20. $element['#attributes']['class'] .= ' collapsible';
  21. if (!empty($element['#collapsed'])) {
  22. $element['#attributes']['class'] .= ' collapsed';
  23. }
  24. }
  25.  
  26. if (empty($element['#attributes']['id'])) {
  27. $element['#attributes']['id'] = form_clean_id($element['#title']);
  28. }
  29.  
  30. 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";
  31. }

Oh my gosh! That is the

Oh my gosh! That is the delightful thing I've ever seen !!!! Sometimes to reach the PhD level you should buy online essays .