Created
June 29, 2012 15:44
-
-
Save baisong/3018724 to your computer and use it in GitHub Desktop.
A little helper function for Drupal 6 form manipulation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Adds a class to a form element. | |
| */ | |
| function _custom_attributes_add_class($element, $class) { | |
| if ((!is_array($element)) || ((int)strlen($class) === (int)0)) return $element; | |
| if (isset($element['#attributes'])) { | |
| if (!is_array($element['#attributes'])) return $element; | |
| if (isset($element['#attributes']['class'])) { | |
| $element['#attributes']['class'] .= ' ' . $class; | |
| } | |
| else { | |
| $element['#attributes']['class'] = $class; | |
| } | |
| } | |
| else { | |
| $element['#attributes'] = array(); | |
| $element['#attributes']['class'] = $class; | |
| } | |
| return $element; | |
| } | |
| /** | |
| * Implements HOOK_form_alter. | |
| * Example usage. | |
| */ | |
| function mymodule_form_alter(&$form, $form_state, $form_id) { | |
| $form['buttons']['submit'] = _custom_attributes_add_class($form['buttons']['submit'], 'custom'); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment