-
Notifications
You must be signed in to change notification settings - Fork 1
/
prepopulate.module
108 lines (102 loc) · 3.54 KB
/
prepopulate.module
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
<?php
/**
* @file
* Fill form elements with data from GET or POST values.
*
* Originally written by ea. Farris <[email protected]>
* Based on an idea from chx, from the conversation at
* http://www.drupal.org/node/27155.
*/
/**
* Implements hook_help().
*/
function prepopulate_help($path, $arg) {
switch ($path) {
case 'admin/modules#description':
return t('Pre-populates forms with HTTP GET or POST data');
}
}
/**
* Implements hook_form_alter().
*/
function prepopulate_form_alter(&$form, $form_state, $form_id) {
// If this is a subsequent step of a multi-step form, the prepopulate values
// have done their work, and the user may have modified them: bail.
if (!empty($form_state['rebuild'])) {
return;
}
if (isset($_REQUEST['edit'])) {
$form['#after_build'][] = 'prepopulate_after_build';
}
}
/**
* An #after_build function to set the values prepopulated in the request.
*/
function prepopulate_after_build($form, &$form_state) {
if (isset($_REQUEST['edit'])) {
$request = (array) $_REQUEST['edit'];
_prepopulate_request_walk($form, $request);
}
return $form;
}
/**
* Internal helper to set element values from the $_REQUEST variable.
*
* @param array &$form
* A form element.
* @param mixed &$request_slice
* String or array. Value(s) to be applied to the element.
*/
function _prepopulate_request_walk(&$form, &$request_slice) {
$limited_types = array(
'actions',
'button',
'token',
'value',
'hidden',
'image_button',
'password',
'password_confirm',
'markup',
);
if (is_array($request_slice)) {
foreach (array_keys($request_slice) as $request_variable) {
$and_checks = array(
'is child' => element_child($request_variable),
'in form' => !empty($form[$request_variable]),
);
if (array_sum($and_checks) == count($and_checks)) {
$or_checks = array(
'no #type set' => !isset($form[$request_variable]['#type']),
'language container' => isset($form[$request_variable]['#language']) && isset($form[$request_variable][$form[$request_variable]['#language']]['#type']) && !in_array($form[$request_variable][$form[$request_variable]['#language']]['#type'], $limited_types),
'container' => isset($form[$request_variable]['#type']) && $form[$request_variable]['#type'] == 'container' && isset($form[$request_variable]['#language']) && isset($form[$request_variable][$form[$request_variable]['#language']][0]['#type']) && $form[$request_variable][$form[$request_variable]['#language']][0]['#type'] == 'text_format',
'allowed #type' => isset($form[$request_variable]['#type']) && !in_array($form[$request_variable]['#type'], $limited_types),
);
if (array_sum($or_checks) > 0) {
if (!isset($form[$request_variable]['#access']) || $form[$request_variable]['#access'] != FALSE) {
_prepopulate_request_walk($form[$request_variable], $request_slice[$request_variable]);
}
}
}
}
if (!empty($form['#default_value']) && is_array($form['#default_value'])) {
$form['#default_value'] = array_merge($form['#default_value'], $request_slice);
}
}
else {
if ($form['#type'] == 'markup' || empty($form['#type'])) {
$form['#value'] = check_plain($request_slice);
}
else {
$form['#value'] = $request_slice;
}
if ($form['#type'] == 'checkboxes' || $form['#type'] == 'checkbox') {
if (!empty($form['#value'])) {
$form['#checked'] = TRUE;
}
else {
$form['#checked'] = FALSE;
}
}
}
}