-
Notifications
You must be signed in to change notification settings - Fork 22
/
DrupalFormHelpers.inc
70 lines (65 loc) · 1.84 KB
/
DrupalFormHelpers.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
/**
* @file
*
*/
module_load_include('inc', 'php_lib', 'Array');
/**
*
* @param array $element
* @param array $form
*/
function &get_form_element_parent(array $element, array &$form) {
if (!empty($element['#array_parents'])) {
array_pop($element['#array_parents']);
$parent = &array_traverse_path($form, $element['#array_parents']);
return $parent;
}
return NULL;
}
/**
* Takes the value of the given property from the current element and propogates it to its child elements
* if they have not already set that property.
*
* @param array $element
* @param string $property
*/
function propagate_form_element_property(array &$element, $property) {
$element[$property] = get_form_element_property($element, $property);
foreach (element_children($element) as $key) {
$child = &$element[$key];
if (!isset($child[$property])) { // Don't squash an existing property value.
$child[$property] = $element[$property];
}
propagate_form_element_property($child, $property);
}
}
/**
* Get the value of the given property if set, otherwise the default value for that property will be returned.
*
* @param array $element
* @param string $property
* @return mixed
*/
function get_form_element_property(array &$element, $property) {
$default_value = NULL;
if ((!empty($element['#type'])) && ($info = _element_info($element['#type']))) {
$default_value = $info[$property];
}
return isset($element[$property]) ? $element[$property] : $default_value;
}
/**
*
* @param array $element
* @param string $type
* @return array
*/
function get_element_children(array &$element, $type) {
$children = element_children($element);
foreach ($children as $key => $child) {
if (empty($element[$child]['#type']) || $element[$child]['#type'] != $type) {
unset($children[$key]);
}
}
return $children;
}