Projet

Général

Profil

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

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

1
<?php
2

    
3
/**
4
 * @file
5
 * Stub file for bootstrap_form_element_label().
6
 */
7

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

    
39
  // Extract variables.
40
  $output = '';
41

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

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

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

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

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

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

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

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

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