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