file form-element-label.func.php

Stub file for bootstrap_form_element_label().

  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(array &$variables) {
  35. $element = $variables['element'];
  36. // Extract variables.
  37. $output = '';
  38. $title = !empty($element['#title']) ? filter_xss_admin($element['#title']) : '';
  39. // Only show the required marker if there is an actual title to display.
  40. $marker = array('#theme' => 'form_required_marker', '#element' => $element);
  41. if ($title && $required = !empty($element['#required']) ? drupal_render($marker) : '') {
  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. // Immediately return if the element is not a checkbox or radio and there is
  49. // no label to be rendered.
  50. if (!$checkbox && !$radio && ($display === 'none' || !$title)) {
  51. return '';
  52. }
  53. // Retrieve the label attributes array.
  54. $attributes = &_bootstrap_get_attributes($element, 'label_attributes');
  55. // Add Bootstrap label class.
  56. $attributes['class'][] = 'control-label';
  57. // Add the necessary 'for' attribute if the element ID exists.
  58. if (!empty($element['#id'])) {
  59. $attributes['for'] = $element['#id'];
  60. }
  61. // Checkboxes and radios must construct the label differently.
  62. if ($checkbox || $radio) {
  63. if ($display === 'before') {
  64. $output .= $title;
  65. }
  66. elseif ($display === 'none' || $display === 'invisible') {
  67. $output .= '<span class="element-invisible">' . $title . '</span>';
  68. }
  69. // Inject the rendered checkbox or radio element inside the label.
  70. if (!empty($element['#children'])) {
  71. $output .= $element['#children'];
  72. }
  73. if ($display === 'after') {
  74. $output .= $title;
  75. }
  76. }
  77. // Otherwise, just render the title as the label.
  78. else {
  79. // Show label only to screen readers to avoid disruption in visual flows.
  80. if ($display === 'invisible') {
  81. $attributes['class'][] = 'element-invisible';
  82. }
  83. $output .= $title;
  84. }
  85. // The leading whitespace helps visually separate fields from inline labels.
  86. return ' <label' . drupal_attributes($attributes) . '>' . $output . "</label>\n";
  87. }

Functions

Name Description
bootstrap_form_element_label Returns HTML for a form element label and required marker.