pre-render.inc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * @file
  4. * pre-render.inc
  5. *
  6. * Contains various implementations for #pre_render callbacks on elements.
  7. */
  8. /**
  9. * Implements hook_pre_render().
  10. */
  11. function bootstrap_pre_render($element) {
  12. if (!empty($element['#bootstrap_ignore_pre_render'])) {
  13. return $element;
  14. }
  15. // Only add the "form-control" class to supported theme hooks.
  16. $theme_hooks = array(
  17. 'password',
  18. 'select',
  19. 'textarea',
  20. 'textfield',
  21. );
  22. // Additionally, support some popular 3rd-party modules that don't follow
  23. // standards by creating custom theme hooks to use in their element types.
  24. // Go ahead and merge in the theme hooks as a start since most elements mimic
  25. // their theme hook counterparts as well.
  26. $types = array_merge($theme_hooks, array(
  27. // Elements module (HTML5).
  28. 'date',
  29. 'datefield',
  30. 'email',
  31. 'emailfield',
  32. 'number',
  33. 'numberfield',
  34. 'range',
  35. 'rangefield',
  36. 'search',
  37. 'searchfield',
  38. 'tel',
  39. 'telfield',
  40. 'url',
  41. 'urlfield',
  42. // Webform module.
  43. 'webform_email',
  44. 'webform_number',
  45. ));
  46. // Determine element theme hook.
  47. $theme = !empty($element['#theme']) ? $element['#theme'] : FALSE;
  48. // Handle array of theme hooks, just use first one (rare, but could happen).
  49. if (is_array($theme)) {
  50. $theme = array_shift($theme);
  51. }
  52. // Remove any suggestions.
  53. $parts = explode('__', $theme);
  54. $theme = array_shift($parts);
  55. // Determine element type.
  56. $type = !empty($element['#type']) ? $element['#type'] : FALSE;
  57. // Add necessary classes for specific element types/theme hooks.
  58. if (($theme && in_array($theme, $theme_hooks)) || ($type && in_array($type, $types)) || ($type === 'file' && empty($element['#managed_file']))) {
  59. $element['#attributes']['class'][] = 'form-control';
  60. }
  61. if ($type === 'machine_name') {
  62. $element['#wrapper_attributes']['class'][] = 'form-inline';
  63. }
  64. // Add smart descriptions to the element, if necessary.
  65. bootstrap_element_smart_description($element);
  66. // Return the modified element.
  67. return $element;
  68. }
  69. /**
  70. * Implements hook_pre_render_HOOK().
  71. */
  72. function bootstrap_pre_render_fieldset($element) {
  73. // Fieldsets may be rendered outside of a Form API context.
  74. if (!empty($element['#bootstrap_ignore_pre_render']) || !isset($element['#parents']) || !isset($element['#groups'])) {
  75. return $element;
  76. }
  77. // Inject group member elements belonging to this group.
  78. $parents = implode('][', $element['#parents']);
  79. $children = element_children($element['#groups'][$parents]);
  80. if (!empty($children)) {
  81. if (empty($element['#default_tab'])) {
  82. $children_keys = array_values($children);
  83. $element['#default_tab'] = $element['#groups'][$parents][array_shift($children_keys)]['#id'];
  84. }
  85. foreach ($children as $key) {
  86. // Break references and indicate that the element should be rendered as
  87. // group member.
  88. $child = (array) $element['#groups'][$parents][$key];
  89. $child['#attributes']['id'] = $child['#id'];
  90. $child['#group_fieldset'] = TRUE;
  91. // Inject the element as new child element.
  92. $element[] = $child;
  93. $sort = TRUE;
  94. }
  95. // Re-sort the element's children if we injected group member elements.
  96. if (isset($sort)) {
  97. $element['#sorted'] = FALSE;
  98. }
  99. }
  100. if (isset($element['#group'])) {
  101. $group = $element['#group'];
  102. // If this element belongs to a group, but the group-holding element does
  103. // not exist, we need to render it (at its original location).
  104. if (!isset($element['#groups'][$group]['#group_exists'])) {
  105. // Intentionally empty to clarify the flow; we simply return $element.
  106. }
  107. // If we injected this element into the group, then we want to render it.
  108. elseif (!empty($element['#group_fieldset'])) {
  109. // Intentionally empty to clarify the flow; we simply return $element.
  110. }
  111. // Otherwise, this element belongs to a group and the group exists, so we do
  112. // not render it.
  113. elseif (element_children($element['#groups'][$group])) {
  114. $element['#printed'] = TRUE;
  115. }
  116. }
  117. return $element;
  118. }