Projet

Général

Profil

Paste
Télécharger (3,26 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / themes / bootstrap / templates / system / form-element-label.func.php @ 749b8a23

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

    
7
/**
8
 * Returns HTML for a form element label and required marker.
9
 *
10
 * Form element labels include the #title and a #required marker. The label is
11
 * associated with the element itself by the element #id. Labels may appear
12
 * before or after elements, depending on theme_form_element() and
13
 * #title_display.
14
 *
15
 * This function will not be called for elements with no labels, depending on
16
 * #title_display. For elements that have an empty #title and are not required,
17
 * this function will output no label (''). For required elements that have an
18
 * empty #title, this will output the required marker alone within the label.
19
 * The label will use the #id to associate the marker with the field that is
20
 * required. That is especially important for screenreader users to know
21
 * which field is required.
22
 *
23
 * @param array $variables
24
 *   An associative array containing:
25
 *   - element: An associative array containing the properties of the element.
26
 *     Properties used: #required, #title, #id, #value, #description.
27
 *
28
 * @return string
29
 *   The constructed HTML.
30
 *
31
 * @see theme_form_element_label()
32
 *
33
 * @ingroup theme_functions
34
 */
35
function bootstrap_form_element_label(&$variables) {
36
  $element = $variables['element'];
37

    
38
  // Extract variables.
39
  $output = '';
40
  $title = isset($element['#title']) ? filter_xss_admin($element['#title']) . ' ' : '';
41
  $required = !empty($element['#required']) ? theme('form_required_marker', array('element' => $element)) : '';
42
  if ($required) {
43
    $title .= $required;
44
  }
45
  $display = isset($element['#title_display']) ? $element['#title_display'] : 'before';
46
  $type = !empty($element['#type']) ? $element['#type'] : FALSE;
47
  $checkbox = $type && $type === 'checkbox';
48
  $radio = $type && $type === 'radio';
49

    
50
  // Immediately return if the element is not a checkbox or radio and there is
51
  // no label to be rendered.
52
  if (!$checkbox && !$radio && ($display === 'none' || (!$title && !$required))) {
53
    return '';
54
  }
55

    
56
  // Retrieve the label attributes array.
57
  $attributes = &_bootstrap_get_attributes($element, 'label_attributes');
58

    
59
  // Add Bootstrap label class.
60
  $attributes['class'][] = 'control-label';
61

    
62
  // Add the necessary 'for' attribute if the element ID exists.
63
  if (!empty($element['#id'])) {
64
    $attributes['for'] = $element['#id'];
65
  }
66

    
67
  // Checkboxes and radios must construct the label differently.
68
  if ($checkbox || $radio) {
69
    if ($display === 'before') {
70
      $output .= $title;
71
    }
72
    elseif ($display === 'none' || $display === 'invisible') {
73
      $output .= '<span class="element-invisible">' . $title . '</span>';
74
    }
75
    // Inject the rendered checkbox or radio element inside the label.
76
    if (!empty($element['#children'])) {
77
      $output .= $element['#children'];
78
    }
79
    if ($display === 'after') {
80
      $output .= $title;
81
    }
82
  }
83
  // Otherwise, just render the title as the label.
84
  else {
85
    // Show label only to screen readers to avoid disruption in visual flows.
86
    if ($display === 'invisible') {
87
      $attributes['class'][] = 'element-invisible';
88
    }
89
    $output .= $title;
90
  }
91

    
92
  // The leading whitespace helps visually separate fields from inline labels.
93
  return ' <label' . drupal_attributes($attributes) . '>' . $output . "</label>\n";
94
}