| 5 form.inc | theme_form_element($element, $value) |
| 6 form.inc | theme_form_element( |
| 7 form.inc | theme_form_element($variables) |
| 8 form.inc | theme_form_element($variables) |
Return a themed form element.
Parameters
element: An associative array containing the properties of the element. Properties used: title, description, id, required
$value: The form element's data.
Return value
A string representing the form element.
Related topics
File
- includes/
form.inc, line 2203
Code
<?php
function theme_form_element($element, $value) {
// This is also used in the installer, pre-database setup.
$t = get_t();
$output = '<div class="form-item"';
if (!empty($element['#id'])) {
$output .= ' id="' . $element['#id'] . '-wrapper"';
}
$output .= ">\n";
$required = !empty($element['#required']) ? '<span class="form-required" title="' . $t('This field is required.') . '">*</span>' : '';
if (!empty($element['#title'])) {
$title = $element['#title'];
if (!empty($element['#id'])) {
$output .= ' <label for="' . $element['#id'] . '">' . $t('!title: !required', array('!title' => filter_xss_admin($title), '!required' => $required)) . "</label>\n";
}
else {
$output .= ' <label>' . $t('!title: !required', array('!title' => filter_xss_admin($title), '!required' => $required)) . "</label>\n";
}
}
$output .= " $value\n";
if (!empty($element['#description'])) {
$output .= ' <div class="description">' . $element['#description'] . "</div>\n";
}
$output .= "</div>\n";
return $output;
}
?> Login or register to post comments
Comments
Make each 'form-item' unique class
If you want unique class for each 'form-item'
1) copy the whole code above to template.php
2) replace 'theme' with your theme name
function yourthemename_form_element($element, $value) {3) replace this line:
$output = '<div class="form-item"';with this line
$output = '<div class="form-item-' . substr(strtolower(strtr($element['#title'], array(' ' => '-', '_' => '-', '[' => '-', ']' => '', '(' => '', ')' => '', ':' => ''))) , 0, 15) . '"';This will give you a class='form-item-title-goes-here'
A bit cumbersome with long classes (form-item+15 characters) but still a lot easier.
Not that this approach will
Not that this approach will break your class if the element's title changes, which happens if you have a multilingual site or you change the title string using the translation interface. It's best to use the element's name or ID attribute for this.
Common AND unique class
If you would like to keep the common class as well as a unique class, modify the snippet in 3 above by adding "form-item form-item-". The first bit applies the common class, the second gives the unique class. The whole $output would be:
$output = '<div class="form-item form-item-' . substr(strtolower(strtr($element['#title'], array(' ' => '-', '_' => '-', '[' => '-', ']' => '', '(' => '', ')' => '', ':' => ''))) , 0, 15) . '"';