-
Notifications
You must be signed in to change notification settings - Fork 21
/
FormValueTracker.inc
executable file
·165 lines (150 loc) · 4.21 KB
/
FormValueTracker.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/**
* @file
* Defines the FormValueTracker class that is used by only the FormValues class.
*/
module_load_include('inc', 'objective_forms', 'FormElement');
module_load_include('inc', 'objective_forms', 'FormElementRegistry');
/**
* This class utilizes scope and a reference pointer to track where
* the current value for a given FormElement. Its used by the Form values
* class where it is used to retrieve all the values of FormElements.
*/
class FormValueTracker {
/**
* Submitted values from the form. A reference to $form_state['values'].
*
* @var array
*/
protected $values;
/**
* The form element registry used to get properties for given form elements.
*
* @var FormElementRegistry
*/
protected $registry;
/**
* A reference to a position in $values.
*
* @var mixed
*/
protected $current;
/**
* TRUE if we are tracking a location in the values array, FALSE if not.
*
* @var boolean
*/
protected $track;
/**
* Creates a FormValues instance.
*
* @param array $values
* Array of values
*/
public function __construct(array &$values, FormElementRegistry $registry) {
$this->values = &$values;
$this->current = &$this->values;
// Default value is FALSE.
$this->track = FALSE;
$this->registry = $registry;
}
/**
* Gets the value for a given FormElement.
*
* Tracks the current position in the $values array if applicable.
*
* @param array $element
* An element in the Drupal Form.
*
* @return mixed
* Submitted value for the given FormElement if found, NULL otherwise.
*/
public function getValue(array &$element) {
module_load_include('inc', 'objective_forms', 'FormElementRegistry');
$value = $this->track($element);
// Array's are roots of #tree branchs.
return is_array($value) ? NULL : $value;
}
/**
* Get this elements index in its parent.
*
* @param array $element
* An element in the Drupal Form.
*
* @return mixed
* The index of the element if found, NULL otherwise.
*/
protected function getIndex(array &$element) {
if (isset($element['#array_parents'])) {
$parents = $element['#array_parents'];
return array_pop($parents);
}
elseif (isset($element['#hash'])) {
$found = $this->registry->get($element['#hash']);
if ($found) {
return $found->getIndex();
}
}
return NULL;
}
/**
* Function track.
*
* Update the $current pointer so that it is pointing at the correct value.
* Return the current position.
*
* @param array $element
* An element in the Drupal Form.
*
* @return mixed
* The value of the current position in the values tree. Typically a array
* or a scalar value.
*/
protected function track(array &$element) {
if ($this->shouldStartTracking($element)) {
$this->track = TRUE;
}
if ($this->shouldStopTracking($element)) {
$this->track = FALSE;
$this->reset();
}
$index = $this->getIndex($element);
if ($this->track && isset($this->current[$index])) {
$this->current = &$this->current[$index];
return $this->current;
}
return isset($this->current[$index]) ? $this->current[$index] : NULL;
}
/**
* Checks if the tracker should begin tracking.
*
* @param array $element
* An element in the Drupal Form.
*
* @return bool
* TRUE if the tracker should start to track values, FALSE if it is already
* tracking or shouldn't start tracking.
*/
protected function shouldStartTracking(array &$element) {
return (isset($element['#tree']) && $element['#tree'] === TRUE) && !$this->track;
}
/**
* Checks if the tracker should stop tracking.
*
* @param array $element
* An element in the Drupal Form.
*
* @return bool
* TRUE if the tracker should stop tracking values, FALSE if it is not
* tracking or should continue tracking.
*/
protected function shouldStopTracking(array &$element) {
return (isset($element['#tree']) && $element['#tree'] === FALSE) && $this->track;
}
/**
* Reset the $current pointer to the top level of the $values array.
*/
protected function reset() {
$this->current = &$this->values;
}
}