forked from nigelgbanks/objective_forms
-
Notifications
You must be signed in to change notification settings - Fork 20
/
FormValueTracker.inc
executable file
·80 lines (70 loc) · 1.91 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
<?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) {
if (!isset($element['#hash'])) {
return NULL;
}
$form_element = $this->registry->get($element['#hash']);
$value = drupal_array_get_nested_value($this->current, $form_element->getParentsArray());
return is_array($value) ? NULL : $value;
}
}