Change that Form Element - Give a fieldset a class

I looked for this a few times and didn't find it so I decided to write a quick blog post for future themers to discover. You see Drupal is full of forms and fields. There are any number of reasons that you may want an element on a form to be identified by a unique class or ID. I ran into this today when I wanted to theme a fieldset, provided by a module, that didn't have a class on the fieldset. Now there are many ways to theme a fieldset by using the divs that contain the fieldset. However, sometimes it's nice to just have a theme set for a fieldset, and apply that class when and where you need it.

 

Here is the basic syntax you are looking for is when using hook_form_alter.

$form['subscriptions']['#attributes'] = array('class' => 'subscription-set');

 

The whole thing would look something like this.

function [NAME-OF-MODULE]_form_alter(&$form, &$form_state, $form_id) {

  $form['subscriptions']['#title'] = NULL;

  $form['subscriptions']['#attributes'] = array(

    'class' => 'subscription-set');

  }

Happy Theming!

Comments

2

You should really wrap your modifications in an if statement: <code> if ( $form_id == 'foo') { // make your modifications for the foo form. } elseif ($form_id == 'bar') { // make your modifications for the bar form. } </code> Hmmm, your input format doesn't handle code very well.

When I put it all together I like using a switch statement because I usually end up with several form alters.... <br/> <code> function [MYTHEMENAME]_form_alter(&$form, &$form_state, $form_id) { //dsm($form_id); //dsm($form); switch ($form_id) { // Views Exposed Filters case 'views_exposed_form': $form['submit']['#value'] = t('Go For It'); // default is 'apply' break; // Node Edit Form case 'node_type_form': // Allow Content Body Titles to be 400 characters. $form['submission']['body_label']['#maxlength'] = 200; break; } } </code> <br/> This way I just keep adding, and adding, and adding....

Add new comment

By submitting this form, you accept the Mollom privacy policy.