Skip to content

Instantly share code, notes, and snippets.

@baisong
Created June 29, 2012 15:44
Show Gist options
  • Select an option

  • Save baisong/3018724 to your computer and use it in GitHub Desktop.

Select an option

Save baisong/3018724 to your computer and use it in GitHub Desktop.
A little helper function for Drupal 6 form manipulation
<?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