Search code examples
drupaldrupal-6form-submit

Drupal - Add class to all submit buttons


I have several forms in my webpage, and I want to be able to add a class to all my submit buttons (right now I have form-submit as standard). And also, in some forms I have button which uses AHAH in order to show something, but I don't want these to have the new class, only those button who does the final submit on the form.

Thanks in advance!


Solution

  • Or you can overwrite theme_button which is responsible for the submit buttons. Place this in the template.php file and make sure to clear the cache after you make the changes. You can var_dump the $element and see how you can differentiate between the submit buttons.

    /**
     * Overwrite theme_button()
     * @file template.php
     * !replace my_theme with the name of your active theme
     */
    function my_theme_button($element) {
    
      // Add some extra conditions to make sure we're only adding
      // the classto the right submit button
      if ($element['#id'] == 'edit-submit') {
        // Now add our custom class
        if (isset($element['#attributes']['class'])) {
          $element['#attributes']['class'] .= ' extra-class';
        }
        else {
          $element['#attributes']['class'] = 'extra-class';
        }
      }
    
      return theme_button($element);
    }