Projet

Général

Profil

Paste
Télécharger (7,05 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / themes / bootstrap / templates / system / form-element.func.php @ 05237dd8

1
<?php
2
/**
3
 * @file
4
 * Stub file for bootstrap_form_element().
5
 */
6

    
7
/**
8
 * Returns HTML for a form element.
9
 *
10
 * Each form element is wrapped in a DIV container having the following CSS
11
 * classes:
12
 * - form-item: Generic for all form elements.
13
 * - form-type-#type: The internal element #type.
14
 * - form-item-#name: The internal form element #name (usually derived from the
15
 *   $form structure and set via form_builder()).
16
 * - form-disabled: Only set if the form element is #disabled.
17
 *
18
 * In addition to the element itself, the DIV contains a label for the element
19
 * based on the optional #title_display property, and an optional #description.
20
 *
21
 * The optional #title_display property can have these values:
22
 * - before: The label is output before the element. This is the default.
23
 *   The label includes the #title and the required marker, if #required.
24
 * - after: The label is output after the element. For example, this is used
25
 *   for radio and checkbox #type elements as set in system_element_info().
26
 *   If the #title is empty but the field is #required, the label will
27
 *   contain only the required marker.
28
 * - invisible: Labels are critical for screen readers to enable them to
29
 *   properly navigate through forms but can be visually distracting. This
30
 *   property hides the label for everyone except screen readers.
31
 * - attribute: Set the title attribute on the element to create a tooltip
32
 *   but output no label element. This is supported only for checkboxes
33
 *   and radios in form_pre_render_conditional_form_element(). It is used
34
 *   where a visual label is not needed, such as a table of checkboxes where
35
 *   the row and column provide the context. The tooltip will include the
36
 *   title and required marker.
37
 *
38
 * If the #title property is not set, then the label and any required marker
39
 * will not be output, regardless of the #title_display or #required values.
40
 * This can be useful in cases such as the password_confirm element, which
41
 * creates children elements that have their own labels and required markers,
42
 * but the parent element should have neither. Use this carefully because a
43
 * field without an associated label can cause accessibility challenges.
44
 *
45
 * @param array $variables
46
 *   An associative array containing:
47
 *   - element: An associative array containing the properties of the element.
48
 *     Properties used: #title, #title_display, #description, #id, #required,
49
 *     #children, #type, #name.
50
 *
51
 * @return string
52
 *   The constructed HTML.
53
 *
54
 * @see theme_form_element()
55
 *
56
 * @ingroup theme_functions
57
 */
58
function bootstrap_form_element(&$variables) {
59
  $element = &$variables['element'];
60
  $name = !empty($element['#name']) ? $element['#name'] : FALSE;
61
  $type = !empty($element['#type']) ? $element['#type'] : FALSE;
62
  $wrapper = isset($element['#form_element_wrapper']) ? !!$element['#form_element_wrapper'] : TRUE;
63
  $form_group = isset($element['#form_group']) ? !!$element['#form_group'] : $wrapper && $type && $type !== 'hidden';
64
  $checkbox = $type && $type === 'checkbox';
65
  $radio = $type && $type === 'radio';
66

    
67
  // Create an attributes array for the wrapping container.
68
  if (empty($element['#wrapper_attributes'])) {
69
    $element['#wrapper_attributes'] = array();
70
  }
71
  $wrapper_attributes = &$element['#wrapper_attributes'];
72

    
73
  // This function is invoked as theme wrapper, but the rendered form element
74
  // may not necessarily have been processed by form_builder().
75
  $element += array(
76
    '#title_display' => 'before',
77
  );
78

    
79
  // Add wrapper ID for 'item' type.
80
  if ($type && $type === 'item' && !empty($element['#markup']) && !empty($element['#id'])) {
81
    $wrapper_attributes['id'] = $element['#id'];
82
  }
83

    
84
  // Check for errors and set correct error class.
85
  if ((isset($element['#parents']) && form_get_error($element) !== NULL) || (!empty($element['#required']) && bootstrap_setting('forms_required_has_error'))) {
86
    $wrapper_attributes['class'][] = 'has-error';
87
  }
88

    
89
  // Add necessary classes to wrapper container.
90
  $wrapper_attributes['class'][] = 'form-item';
91
  if ($name) {
92
    $wrapper_attributes['class'][] = 'form-item-' . drupal_html_class($name);
93
  }
94
  if ($type) {
95
    $wrapper_attributes['class'][] = 'form-type-' . drupal_html_class($type);
96
  }
97
  if (!empty($element['#attributes']['disabled'])) {
98
    $wrapper_attributes['class'][] = 'form-disabled';
99
  }
100
  if (!empty($element['#autocomplete_path']) && drupal_valid_path($element['#autocomplete_path'])) {
101
    $wrapper_attributes['class'][] = 'form-autocomplete';
102
  }
103

    
104
  // Checkboxes and radios do no receive the 'form-group' class, instead they
105
  // simply have their own classes.
106
  if ($checkbox || $radio) {
107
    $wrapper_attributes['class'][] = drupal_html_class($type);
108
  }
109
  elseif ($form_group) {
110
    $wrapper_attributes['class'][] = 'form-group';
111
  }
112

    
113
  // Create a render array for the form element.
114
  $build = array(
115
    '#form_group' => $form_group,
116
    '#attributes' => $wrapper_attributes,
117
  );
118

    
119
  if ($wrapper) {
120
    $build['#theme_wrappers'] = array('container__form_element');
121

    
122
    // Render the label for the form element.
123
    $build['label'] = array(
124
      '#markup' => theme('form_element_label', $variables),
125
      '#weight' => $element['#title_display'] === 'before' ? 0 : 2,
126
    );
127
  }
128

    
129
  // Checkboxes and radios render the input element inside the label. If the
130
  // element is neither of those, then the input element must be rendered here.
131
  if (!$checkbox && !$radio) {
132
    $prefix = isset($element['#field_prefix']) ? $element['#field_prefix'] : '';
133
    $suffix = isset($element['#field_suffix']) ? $element['#field_suffix'] : '';
134
    if ((!empty($prefix) || !empty($suffix)) && (!empty($element['#input_group']) || !empty($element['#input_group_button']))) {
135
      if (!empty($element['#field_prefix'])) {
136
        $prefix = '<span class="input-group-' . (!empty($element['#input_group_button']) ? 'btn' : 'addon') . '">' . $prefix . '</span>';
137
      }
138
      if (!empty($element['#field_suffix'])) {
139
        $suffix = '<span class="input-group-' . (!empty($element['#input_group_button']) ? 'btn' : 'addon') . '">' . $suffix . '</span>';
140
      }
141

    
142
      // Add a wrapping container around the elements.
143
      $input_group_attributes = &_bootstrap_get_attributes($element, 'input_group_attributes');
144
      $input_group_attributes['class'][] = 'input-group';
145
      $prefix = '<div' . drupal_attributes($input_group_attributes) . '>' . $prefix;
146
      $suffix .= '</div>';
147
    }
148

    
149
    // Build the form element.
150
    $build['element'] = array(
151
      '#markup' => $element['#children'],
152
      '#prefix' => !empty($prefix) ? $prefix : NULL,
153
      '#suffix' => !empty($suffix) ? $suffix : NULL,
154
      '#weight' => 1,
155
    );
156
  }
157

    
158
  // Construct the element's description markup.
159
  if (!empty($element['#description'])) {
160
    $build['description'] = array(
161
      '#type' => 'container',
162
      '#attributes' => array(
163
        'class' => array('help-block'),
164
      ),
165
      '#weight' => isset($element['#description_display']) && $element['#description_display'] === 'before' ? 0 : 20,
166
      0 => array('#markup' => filter_xss_admin($element['#description'])),
167
    );
168
  }
169

    
170
  // Render the form element build array.
171
  return drupal_render($build);
172
}