| 1 |
<?php |
<?php |
| 2 |
// $Id: prepopulate.module,v 1.12 2008/11/17 17:43:14 add1sun Exp $ |
// $Id: prepopulate.module,v 1.13 2009/10/08 03:01:32 brauerranch Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file |
* @file |
| 6 |
* Fill form elements with data from GET values. |
* Fill form elements with data from GET or POST values. |
| 7 |
* |
* |
| 8 |
* Originally written by ea. Farris <eafarris@gmail.com> |
* Originally written by ea. Farris <eafarris@gmail.com> |
| 9 |
* Based on an idea from chx, from the conversation at |
* Based on an idea from chx, from the conversation at |
| 16 |
function prepopulate_help($path, $arg) { |
function prepopulate_help($path, $arg) { |
| 17 |
switch ($path) { |
switch ($path) { |
| 18 |
case 'admin/modules#description': |
case 'admin/modules#description': |
| 19 |
return t('Pre-populates forms with HTTP GET data'); |
return t('Pre-populates forms with HTTP GET or POST data'); |
| 20 |
break; |
break; |
| 21 |
} |
} |
| 22 |
} |
} |
| 25 |
* Implementation of hook_form_alter(). |
* Implementation of hook_form_alter(). |
| 26 |
*/ |
*/ |
| 27 |
function prepopulate_form_alter(&$form, $form_state, $form_id) { |
function prepopulate_form_alter(&$form, $form_state, $form_id) { |
| 28 |
|
// Provide for accepting base64 encoded fields. |
| 29 |
|
if (isset($_REQUEST['pp'])) { |
| 30 |
|
parse_str(base64_decode($_REQUEST['pp']), $_REQUEST); |
| 31 |
|
} |
| 32 |
if (isset($_REQUEST['edit'])) { |
if (isset($_REQUEST['edit'])) { |
| 33 |
foreach (array_keys((array)$_REQUEST['edit']) as $getvar) { |
foreach (array_keys((array)$_REQUEST['edit']) as $requestvar) { |
| 34 |
if (element_child($getvar) && !is_null($form[$getvar])) { |
if (element_child($requestvar) && !is_null($form[$requestvar])) { |
| 35 |
_prepopulate_get_walk($form[$getvar], $_REQUEST['edit'][$getvar]); |
_prepopulate_request_walk($form[$requestvar], $_REQUEST['edit'][$requestvar]); |
| 36 |
} |
} |
| 37 |
} |
} |
| 38 |
} |
} |
| 39 |
} |
} |
| 40 |
|
|
| 41 |
/** |
/** |
| 42 |
* Internal helper to set element values from the $_GET variable. |
* Internal helper to set element values from the $_REQUEST variable. |
| 43 |
* |
* |
| 44 |
* @param &$form |
* @param &$form |
| 45 |
* Array. A form element. |
* Array. A form element. |
| 46 |
* @param &$getslice |
* @param &$requestslice |
| 47 |
* String or array. Value(s) to be applied to the element. |
* String or array. Value(s) to be applied to the element. |
| 48 |
*/ |
*/ |
| 49 |
function _prepopulate_get_walk( & $form, & $getslice) { |
function _prepopulate_request_walk(&$form, &$requestslice) { |
| 50 |
if (is_array($getslice)) { |
if (is_array($requestslice)) { |
| 51 |
if (!is_null($form['#default_value'])) { |
if (!is_null($form['#default_value'])) { |
| 52 |
if (!is_array($form['#default_value'])) { |
if (!is_array($form['#default_value'])) { |
| 53 |
// Something went wrong so stop here. |
// Something went wrong so stop here. |
| 54 |
return; |
return; |
| 55 |
} |
} |
| 56 |
$form['#default_value'] = array_merge($form['#default_value'], $getslice); |
$form['#default_value'] = array_merge($form['#default_value'], $requestslice); |
| 57 |
} |
} |
| 58 |
else { |
else { |
| 59 |
foreach (array_keys($getslice) as $getvar) { |
foreach (array_keys($requestslice) as $requestvar) { |
| 60 |
if (element_child($getvar) && is_array($form) && !is_null($form[$getvar])) { |
if (element_child($requestvar) && is_array($form) && !is_null($form[$getvar])) { |
| 61 |
_prepopulate_get_walk($form[$getvar], $getslice[$getvar]); |
_prepopulate_request_walk($form[$requestvar], $requestslice[$requestvar]); |
| 62 |
} |
} |
| 63 |
} |
} |
| 64 |
} |
} |
| 65 |
} |
} |
| 66 |
else { |
else { |
| 67 |
$form['#default_value'] = $getslice; |
$form['#default_value'] = $requestslice; |
| 68 |
} |
} |
| 69 |
} |
} |