form-element-label.func.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * @file
  4. * Stub file for bootstrap_form_element_label().
  5. */
  6. /**
  7. * Returns HTML for a form element label and required marker.
  8. *
  9. * Form element labels include the #title and a #required marker. The label is
  10. * associated with the element itself by the element #id. Labels may appear
  11. * before or after elements, depending on theme_form_element() and
  12. * #title_display.
  13. *
  14. * This function will not be called for elements with no labels, depending on
  15. * #title_display. For elements that have an empty #title and are not required,
  16. * this function will output no label (''). For required elements that have an
  17. * empty #title, this will output the required marker alone within the label.
  18. * The label will use the #id to associate the marker with the field that is
  19. * required. That is especially important for screenreader users to know
  20. * which field is required.
  21. *
  22. * @param array $variables
  23. * An associative array containing:
  24. * - element: An associative array containing the properties of the element.
  25. * Properties used: #required, #title, #id, #value, #description.
  26. *
  27. * @return string
  28. * The constructed HTML.
  29. *
  30. * @see theme_form_element_label()
  31. *
  32. * @ingroup theme_functions
  33. */
  34. function bootstrap_form_element_label(&$variables) {
  35. $element = $variables['element'];
  36. // Extract variables.
  37. $output = '';
  38. $title = isset($element['#title']) ? filter_xss_admin($element['#title']) . ' ' : '';
  39. if ($title && ($required = !empty($element['#required']) ? theme('form_required_marker', array('element' => $element)) : '')) {
  40. $title .= $required;
  41. }
  42. $display = isset($element['#title_display']) ? $element['#title_display'] : 'before';
  43. $type = !empty($element['#type']) ? $element['#type'] : FALSE;
  44. $checkbox = $type && $type === 'checkbox';
  45. $radio = $type && $type === 'radio';
  46. // Immediately return if the element is not a checkbox or radio and there is
  47. // no label to be rendered.
  48. if (!$checkbox && !$radio && ($display === 'none' || !$title)) {
  49. return '';
  50. }
  51. // Retrieve the label attributes array.
  52. $attributes = &_bootstrap_get_attributes($element, 'label_attributes');
  53. // Add Bootstrap label class.
  54. $attributes['class'][] = 'control-label';
  55. // Add the necessary 'for' attribute if the element ID exists.
  56. if (!empty($element['#id'])) {
  57. $attributes['for'] = $element['#id'];
  58. }
  59. // Checkboxes and radios must construct the label differently.
  60. if ($checkbox || $radio) {
  61. if ($display === 'before') {
  62. $output .= $title;
  63. }
  64. elseif ($display === 'none' || $display === 'invisible') {
  65. $output .= '<span class="element-invisible">' . $title . '</span>';
  66. }
  67. // Inject the rendered checkbox or radio element inside the label.
  68. if (!empty($element['#children'])) {
  69. $output .= $element['#children'];
  70. }
  71. if ($display === 'after') {
  72. $output .= $title;
  73. }
  74. }
  75. // Otherwise, just render the title as the label.
  76. else {
  77. // Show label only to screen readers to avoid disruption in visual flows.
  78. if ($display === 'invisible') {
  79. $attributes['class'][] = 'element-invisible';
  80. }
  81. $output .= $title;
  82. }
  83. // The leading whitespace helps visually separate fields from inline labels.
  84. return ' <label' . drupal_attributes($attributes) . '>' . $output . "</label>\n";
  85. }