coder1.com

  • home
  • drupal articles
  • contact
Home

Adding IDs to all Fieldsets

Mike Milano — February 5, 2010 - 11:42am

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. }

  • Drupal
  • Fieldsets

Thanks Mike, Your functions

Nehal Rupani (not verified) — June 11, 2010 - 6:42am

Thanks Mike,

Your functions work like a charm for me.

I wish i could write down this kind of some function one day on theme template on drupal.

  • reply

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <img> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <h3> <h4> <h5> <h6> <h7>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. The supported tag styles are: <foo>, [foo].

More information about formatting options

Poll

Have you checked out Drupal 7 yet? (still alpha):

User login

  • Request new password

Navigation

  • Recent posts
  • home
  • drupal articles
  • contact