If you need to change the content of the variables passed to the theme function, then you can simply implement a preprocess function for that theme, which in Drupal 7 is invoked for every theme function, including the ones not using a template file.
In the case of theme_form_element()
the preprocess function for your theme is MYTHEME_preprocess_form_element()
.
You could also avoid duplicating the code of theme_form_element()
, and call it before, but in that case you should call it directly as theme_form_element()
, not as theme('form_element')
(which would cause an infinite loop).
The problem with theme_form_element()
is that it doesn't use $element['#attributes']['class']
to set the attributes of the form element. It uses a local variable that is initialized with the following code, which passes its value to drupal_attributes()
.
$attributes['class'] = array('form-item');
if (!empty($element['#type'])) {
$attributes['class'][] = 'form-type-' . strtr($element['#type'], '_', '-');
}
if (!empty($element['#name'])) {
$attributes['class'][] = 'form-item-' . strtr($element['#name'], array(' ' => '-', '_' => '-', '[' => '-', ']' => ''));
}
// Add a class for disabled elements to facilitate cross-browser styling.
if (!empty($element['#attributes']['disabled'])) {
$attributes['class'][] = 'form-disabled';
}
$output = '<div' . drupal_attributes($attributes) . '>' . "\n";
To add a new class to the form element, you have two alternatives:
- Copying the code from
theme_form_element()
to your theme function, and editing it
- Call
theme_form_element()
from your theme function, and add a new class to the output returned from theme_form_element()
using preg_replace().