4.6.x theme.inctheme_form_element($title, $value, $description = NULL, $id = NULL, $required = FALSE, $error = FALSE)
4.7.x theme.inctheme_form_element($title, $value, $description = NULL, $id = NULL, $required = FALSE, $error = FALSE)
5.x form.inctheme_form_element($element, $value)
6.x form.inctheme_form_element($element, $value)
7.x form.inctheme_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

12 theme calls to theme_form_element()

File

 

includes/form.inc, line 2322
 

 

Code

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;
}

Comments

Alauddin’s picture

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.

Xano’s picture

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.

mariagwyn’s picture

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) . '"';


来自 https://api.drupal.org/api/drupal/includes%21form.inc/function/theme_form_element/6.x