forked from nigelgbanks/objective_forms
-
Notifications
You must be signed in to change notification settings - Fork 20
/
FormPopulator.inc
executable file
·61 lines (54 loc) · 1.34 KB
/
FormPopulator.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
<?php
/**
* @file
* Defines a class that can be used to dynamically populate the default values
* of a Drupal Form, from the submitted POST data.
*/
module_load_include('inc', 'objective_forms', 'FormValues');
/**
* Used to populate a Drupal Form with values submitted as POST data.
*/
class FormPopulator {
/**
* The submitted POST data.
*
* @var FormValues
*/
protected $values;
/**
* Instantiates a FormPopulator.
*
* @param FormValues $values
* The submitted POST data.
*/
public function __construct(FormValues $values) {
$this->values = $values;
}
/**
* Function populate.
*
* Populates a Drupal Form's elements #default_value properties with POST
* data.
*
* @param array $form
* The Drupal Form to populate
*
* @return array
* The populated Drupal Form.
*/
public function populate(array &$form) {
$children = element_children($form);
foreach ($children as $key) {
$child = &$form[$key];
$default_value = isset($child['#hash']) ? $this->values->getValue($child['#hash']) : NULL;
// We must check that it is set so that newly created elements aren't
// overwritten.
if (isset($default_value)) {
$child['#default_value'] = $default_value;
}
// Recurse...
$this->populate($child);
}
return $form;
}
}