Change that Form Element - Give a fieldset a class

04 Feb in code, drupal, php, theme
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

You should really wrap your

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

Good Point, My Final Result Is Actually

When I put it all together I like using a switch statement because I usually end up with several form alters....


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



This way I just keep adding, and adding, and adding....

Branda

Super Blog Web Master Thank You

louis vuitton

The etruereligionjeans-storemoncler Jacket online Shop.com is one of the most professional online stores. It has specialized in True Religion Brand jeansmoncler Jack online trading for years. As a professional True Religion Jeans outlet, we offer our honorable customers a variety of True Religion jeans for men and women. moncler SteifelFine workmanship, exquisite material, high quality is guaranteed by etruereligionjeans-store.com. Buy fine and cheap True Religion Jeans, here will be your best choice.

Post new comment