Projet

Général

Profil

Paste
Télécharger (6,79 ko) Statistiques
| Branche: | Révision:

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

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
  $checkbox = $type && $type === 'checkbox';
63
  $radio = $type && $type === 'radio';
64

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

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

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

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

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

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

    
111
  // Create a render array for the form element.
112
  $build = array(
113
    '#theme_wrappers' => array('container__form_element'),
114
    '#attributes' => $wrapper_attributes,
115
  );
116

    
117
  // Render the label for the form element.
118
  $build['label'] = array(
119
    '#markup' => theme('form_element_label', $variables),
120
    '#weight' => $element['#title_display'] === 'before' ? 0 : 2,
121
  );
122

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

    
136
      // Add a wrapping container around the elements.
137
      $input_group_attributes = &_bootstrap_get_attributes($element, 'input_group_attributes');
138
      $input_group_attributes['class'][] = 'input-group';
139
      $prefix = '<div' . drupal_attributes($input_group_attributes) . '>' . $prefix;
140
      $suffix .= '</div>';
141
    }
142

    
143
    // Build the form element.
144
    $build['element'] = array(
145
      '#markup' => $element['#children'],
146
      '#prefix' => !empty($prefix) ? $prefix : NULL,
147
      '#suffix' => !empty($suffix) ? $suffix : NULL,
148
      '#weight' => 1,
149
    );
150
  }
151

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

    
164
  // Render the form element build array.
165
  return drupal_render($build);
166
}