Projet

Général

Profil

Paste
Télécharger (4,16 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / themes / bootstrap / theme / system / form-element.func.php @ 87dbc3bf

1
<?php
2
/**
3
 * @file
4
 * form-element.func.php
5
 */
6

    
7
/**
8
 * Overrides theme_form_element().
9
 */
10
function bootstrap_form_element(&$variables) {
11
  $element = &$variables['element'];
12
  $is_checkbox = FALSE;
13
  $is_radio = FALSE;
14

    
15
  // This function is invoked as theme wrapper, but the rendered form element
16
  // may not necessarily have been processed by form_builder().
17
  $element += array(
18
    '#title_display' => 'before',
19
  );
20

    
21
  // Add element #id for #type 'item'.
22
  if (isset($element['#markup']) && !empty($element['#id'])) {
23
    $attributes['id'] = $element['#id'];
24
  }
25

    
26
  // Check for errors and set correct error class.
27
  if (isset($element['#parents']) && form_get_error($element)) {
28
    $attributes['class'][] = 'error';
29
  }
30

    
31
  if (!empty($element['#type'])) {
32
    $attributes['class'][] = 'form-type-' . strtr($element['#type'], '_', '-');
33
  }
34
  if (!empty($element['#name'])) {
35
    $attributes['class'][] = 'form-item-' . strtr($element['#name'], array(
36
        ' ' => '-',
37
        '_' => '-',
38
        '[' => '-',
39
        ']' => '',
40
      ));
41
  }
42
  // Add a class for disabled elements to facilitate cross-browser styling.
43
  if (!empty($element['#attributes']['disabled'])) {
44
    $attributes['class'][] = 'form-disabled';
45
  }
46
  if (!empty($element['#autocomplete_path']) && drupal_valid_path($element['#autocomplete_path'])) {
47
    $attributes['class'][] = 'form-autocomplete';
48
  }
49
  $attributes['class'][] = 'form-item';
50

    
51
  // See http://getbootstrap.com/css/#forms-controls.
52
  if (isset($element['#type'])) {
53
    if ($element['#type'] == "radio") {
54
      $attributes['class'][] = 'radio';
55
      $is_radio = TRUE;
56
    }
57
    elseif ($element['#type'] == "checkbox") {
58
      $attributes['class'][] = 'checkbox';
59
      $is_checkbox = TRUE;
60
    }
61
    else {
62
      $attributes['class'][] = 'form-group';
63
    }
64
  }
65

    
66
  $description = FALSE;
67
  $tooltip = FALSE;
68
  // Convert some descriptions to tooltips.
69
  // @see bootstrap_tooltip_descriptions setting in _bootstrap_settings_form()
70
  if (!empty($element['#description'])) {
71
    $description = $element['#description'];
72
    if (theme_get_setting('bootstrap_tooltip_enabled') && theme_get_setting('bootstrap_tooltip_descriptions') && $description === strip_tags($description) && strlen($description) <= 200) {
73
      $tooltip = TRUE;
74
      $attributes['data-toggle'] = 'tooltip';
75
      $attributes['title'] = $description;
76
    }
77
  }
78

    
79
  $output = '<div' . drupal_attributes($attributes) . '>' . "\n";
80

    
81
  // If #title is not set, we don't display any label or required marker.
82
  if (!isset($element['#title'])) {
83
    $element['#title_display'] = 'none';
84
  }
85

    
86
  $prefix = '';
87
  $suffix = '';
88
  if (isset($element['#field_prefix']) || isset($element['#field_suffix'])) {
89
    // Determine if "#input_group" was specified.
90
    if (!empty($element['#input_group'])) {
91
      $prefix .= '<div class="input-group">';
92
      $prefix .= isset($element['#field_prefix']) ? '<span class="input-group-addon">' . $element['#field_prefix'] . '</span>' : '';
93
      $suffix .= isset($element['#field_suffix']) ? '<span class="input-group-addon">' . $element['#field_suffix'] . '</span>' : '';
94
      $suffix .= '</div>';
95
    }
96
    else {
97
      $prefix .= isset($element['#field_prefix']) ? $element['#field_prefix'] : '';
98
      $suffix .= isset($element['#field_suffix']) ? $element['#field_suffix'] : '';
99
    }
100
  }
101

    
102
  switch ($element['#title_display']) {
103
    case 'before':
104
    case 'invisible':
105
      $output .= ' ' . theme('form_element_label', $variables);
106
      $output .= ' ' . $prefix . $element['#children'] . $suffix . "\n";
107
      break;
108

    
109
    case 'after':
110
      if ($is_radio || $is_checkbox) {
111
        $output .= ' ' . $prefix . $element['#children'] . $suffix;
112
      }
113
      else {
114
        $variables['#children'] = ' ' . $prefix . $element['#children'] . $suffix;
115
      }
116
      $output .= ' ' . theme('form_element_label', $variables) . "\n";
117
      break;
118

    
119
    case 'none':
120
    case 'attribute':
121
      // Output no label and no required marker, only the children.
122
      $output .= ' ' . $prefix . $element['#children'] . $suffix . "\n";
123
      break;
124
  }
125

    
126
  if ($description && !$tooltip) {
127
    $output .= '<p class="help-block">' . $element['#description'] . "</p>\n";
128
  }
129

    
130
  $output .= "</div>\n";
131

    
132
  return $output;
133
}