Projet

Général

Profil

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

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

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
  if ($title && ($required = !empty($element['#required']) ? theme('form_required_marker', array('element' => $element)) : '')) {
42
    $title .= $required;
43
  }
44
  $display = isset($element['#title_display']) ? $element['#title_display'] : 'before';
45
  $type = !empty($element['#type']) ? $element['#type'] : FALSE;
46
  $checkbox = $type && $type === 'checkbox';
47
  $radio = $type && $type === 'radio';
48

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

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

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

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

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

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