1
|
<?php
|
2
|
/**
|
3
|
* @file
|
4
|
* pre-render.inc
|
5
|
*
|
6
|
* Contains various implementations for #pre_render callbacks on elements.
|
7
|
*/
|
8
|
|
9
|
/**
|
10
|
* Implements hook_pre_render().
|
11
|
*/
|
12
|
function bootstrap_pre_render($element) {
|
13
|
if (!empty($element['#bootstrap_ignore_pre_render'])) {
|
14
|
return $element;
|
15
|
}
|
16
|
|
17
|
// Only add the "form-control" class for specific element input types.
|
18
|
$types = array(
|
19
|
// Core.
|
20
|
'password',
|
21
|
'password_confirm',
|
22
|
'machine_name',
|
23
|
'select',
|
24
|
'textarea',
|
25
|
'textfield',
|
26
|
|
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
|
|
43
|
// Webform module.
|
44
|
'webform_email',
|
45
|
'webform_number',
|
46
|
);
|
47
|
|
48
|
// Add necessary classes for specific types.
|
49
|
if ($type = !empty($element['#type']) ? $element['#type'] : FALSE) {
|
50
|
if (in_array($type, $types) || ($type === 'file' && empty($element['#managed_file']))) {
|
51
|
$element['#attributes']['class'][] = 'form-control';
|
52
|
}
|
53
|
if ($type === 'machine_name') {
|
54
|
$element['#wrapper_attributes']['class'][] = 'form-inline';
|
55
|
}
|
56
|
}
|
57
|
|
58
|
// Add smart descriptions to the element, if necessary.
|
59
|
bootstrap_element_smart_description($element);
|
60
|
|
61
|
// Return the modified element.
|
62
|
return $element;
|
63
|
}
|
64
|
|
65
|
/**
|
66
|
* Implements hook_pre_render_HOOK().
|
67
|
*/
|
68
|
function bootstrap_pre_render_fieldset($element) {
|
69
|
// Fieldsets may be rendered outside of a Form API context.
|
70
|
if (!empty($element['#bootstrap_ignore_pre_render']) || !isset($element['#parents']) || !isset($element['#groups'])) {
|
71
|
return $element;
|
72
|
}
|
73
|
// Inject group member elements belonging to this group.
|
74
|
$parents = implode('][', $element['#parents']);
|
75
|
$children = element_children($element['#groups'][$parents]);
|
76
|
if (!empty($children)) {
|
77
|
if (empty($element['#default_tab'])) {
|
78
|
$children_keys = array_values($children);
|
79
|
$element['#default_tab'] = $element['#groups'][$parents][array_shift($children_keys)]['#id'];
|
80
|
}
|
81
|
foreach ($children as $key) {
|
82
|
// Break references and indicate that the element should be rendered as
|
83
|
// group member.
|
84
|
$child = (array) $element['#groups'][$parents][$key];
|
85
|
$child['#attributes']['id'] = $child['#id'];
|
86
|
$child['#group_fieldset'] = TRUE;
|
87
|
// Inject the element as new child element.
|
88
|
$element[] = $child;
|
89
|
|
90
|
$sort = TRUE;
|
91
|
}
|
92
|
// Re-sort the element's children if we injected group member elements.
|
93
|
if (isset($sort)) {
|
94
|
$element['#sorted'] = FALSE;
|
95
|
}
|
96
|
}
|
97
|
|
98
|
if (isset($element['#group'])) {
|
99
|
$group = $element['#group'];
|
100
|
// If this element belongs to a group, but the group-holding element does
|
101
|
// not exist, we need to render it (at its original location).
|
102
|
if (!isset($element['#groups'][$group]['#group_exists'])) {
|
103
|
// Intentionally empty to clarify the flow; we simply return $element.
|
104
|
}
|
105
|
// If we injected this element into the group, then we want to render it.
|
106
|
elseif (!empty($element['#group_fieldset'])) {
|
107
|
// Intentionally empty to clarify the flow; we simply return $element.
|
108
|
}
|
109
|
// Otherwise, this element belongs to a group and the group exists, so we do
|
110
|
// not render it.
|
111
|
elseif (element_children($element['#groups'][$group])) {
|
112
|
$element['#printed'] = TRUE;
|
113
|
}
|
114
|
}
|
115
|
|
116
|
return $element;
|
117
|
}
|