I have a lot of webforms on my site and I need to add a custom class to them. Hook_form_alter works great if I want to override single form, targeting it by id, but how would I do it for lots of forms that have the same class?

Comments

graytoby’s picture

I'm going to answer my own question. Instead of targeting class, I went with regex and targeted all ids (I'm targeting Webform module forms only). In case someone else needs it, see below - code goes in template.php in theme folder.

function mytheme_form_alter(&$form, &$form_state, $form_id) {
     if (preg_match('/webform-client-form-(.*)$/i', $form['#id'])) {
         $form['#attributes']['class'][] = 'custom-class';
     }
}