-
Notifications
You must be signed in to change notification settings - Fork 21
/
Utils.inc
executable file
·114 lines (107 loc) · 2.78 KB
/
Utils.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
/**
* @file
* Utility functions
*/
module_load_include('inc', 'php_lib', 'Array');
/**
* Recursively searches the Drupal form for the element with the given hash.
*
* @param array $form
* The Drupal Form.
*
* @param hash $hash
* The unique #hash property that identifies the FormElement.
*/
function &find_element(array &$form, $hash) {
if (has_hash($form, $hash)) {
return $form;
}
return find_child_element($form, $hash);
}
/**
* Function find_child_elements.
*
* Recursively searches the Drupal Form elements children for the element that
* matches the given hash.
*
* @param array $form
* A Drupal Form Element.
* @param hash $hash
* The unique #hash property that identifies the FormElement.
*
* @return array
* The drupal form element whos #hash matches that of the given $hash value.
*/
function &find_child_element(array &$form, $hash) {
$found = NULL;
$children = element_children($form);
foreach ($children as $key) {
$child = &$form[$key];
$found = has_hash($child, $hash) ? $child : find_child_element($child, $hash);
if (isset($found)) {
return $found;
}
}
return $found;
}
/**
* Functin has_hash.
*
* Checks to see if the given drupal form element has a #hash property and
* checks to see if it matches the given hash.
*
* @param array $element
* A Drupal Form Element.
* @param hash $hash
* The unique #hash property that identifies the FormElement.
*
* @return bool
* TRUE if the given element's hash matches, FALSE otherwise.
*/
function has_hash(array &$element, $hash) {
if (isset($element['#hash']) && $element['#hash'] == $hash) {
return TRUE;
}
return FALSE;
}
/**
* Gets the drupal form element name for the given element if possible.
*
* Only works on processed form elements.
*
* The name of the form element is based on the #parents property of your form
* element. If it is array('foo', 'bar', 'baz') then 'foo][bar][baz' is the
* elements name.
*
* @param array $element
* The Drupal form element.
*
* @return string
* The Drupal style name for the given form element if possible, FALSE
* otherwise.
*/
function form_element_name(array $element) {
if (isset($element['#parents'])) {
return implode('][', $element['#parents']);
}
return FALSE;
}
/**
* Function form_element_is_child.
*
* Compares two Drupal form element names to determine if the second
* argument is a child element of the first.
*
* @param string $parent
* A Drupal form element name for the assumed parent form element.
*
* @param string $child
* A Drupal form element name for the assumed child form element.
*
* @return bool
* TRUE if the given element
*/
function form_element_is_child($parent, $child) {
return preg_match('/^' . preg_quote($parent) . '/', $child) != 0;
}