Projet

Général

Profil

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

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

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

    
41
  $title = !empty($element['#title']) ? filter_xss_admin($element['#title']) : '';
42

    
43
  // Only show the required marker if there is an actual title to display.
44
  if ($title && $required = !empty($element['#required']) ? theme('form_required_marker', array('element' => $element)) : '') {
45
    $title .= ' ' . $required;
46
  }
47

    
48
  $display = isset($element['#title_display']) ? $element['#title_display'] : 'before';
49
  $type = !empty($element['#type']) ? $element['#type'] : FALSE;
50
  $checkbox = $type && $type === 'checkbox';
51
  $radio = $type && $type === 'radio';
52

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

    
59
  // Retrieve the label attributes array.
60
  $attributes = &_bootstrap_get_attributes($element, 'label_attributes');
61

    
62
  // Add Bootstrap label class.
63
  $attributes['class'][] = 'control-label';
64

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

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

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