comparison sites/all/modules/prepopulate/prepopulate.module @ 4:ce11bbd8f642

added modules
author danieleb <danielebarchiesi@me.com>
date Thu, 19 Sep 2013 10:38:44 +0100
parents
children
comparison
equal deleted inserted replaced
3:b28be78d8160 4:ce11bbd8f642
1 <?php
2
3 /**
4 * @file
5 * Fill form elements with data from GET or POST values.
6 *
7 * Originally written by ea. Farris <eafarris@gmail.com>
8 * Based on an idea from chx, from the conversation at
9 * http://www.drupal.org/node/27155.
10 */
11
12 /**
13 * Implementation of hook_help().
14 */
15 function prepopulate_help($path, $arg) {
16 switch ($path) {
17 case 'admin/modules#description':
18 return t('Pre-populates forms with HTTP GET or POST data');
19 break;
20 }
21 }
22
23 /**
24 * Implementation of hook_form_alter().
25 */
26 function prepopulate_form_alter(&$form, $form_state, $form_id) {
27 // Provide for accepting base64 encoded fields.
28 if (isset($_REQUEST['pp'])) {
29 parse_str(base64_decode($_REQUEST['pp']), $_REQUEST);
30 }
31 if (isset($_REQUEST['edit'])) {
32 $form['#after_build'][] = 'prepopulate_after_build';
33 }
34 }
35
36 /**
37 * An #after_build function to set the values prepopulated in the request.
38 */
39 function prepopulate_after_build($form, &$form_state) {
40 if (isset($_REQUEST['pp'])) {
41 parse_str(base64_decode($_REQUEST['pp']), $_REQUEST);
42 }
43 if (isset($_REQUEST['edit'])) {
44 $request = (array)$_REQUEST['edit'];
45 _prepopulate_request_walk($form, $request);
46 }
47 return $form;
48 }
49
50 /**
51 * Internal helper to set element values from the $_REQUEST variable.
52 *
53 * @param &$form
54 * Array. A form element.
55 * @param &$requestslice
56 * String or array. Value(s) to be applied to the element.
57 */
58 function _prepopulate_request_walk(&$form, &$requestslice) {
59 $limited_types = array('value', 'hidden', 'button', 'image_button');
60 if (is_array($requestslice)) {
61 foreach (array_keys($requestslice) as $requestvar) {
62 if (element_child($requestvar) && !empty($form[$requestvar]) &&
63 (!isset($form[$requestvar]['#type']) || !in_array($form[$requestvar]['#type'], $limited_types))) {
64 if (!isset($form[$requestvar]['#access']) || $form[$requestvar]['#access'] != FALSE) {
65 _prepopulate_request_walk($form[$requestvar], $requestslice[$requestvar]);
66 }
67 }
68 }
69 if (!empty($form['#default_value']) && is_array($form['#default_value'])) {
70 $form['#default_value'] = array_merge($form['#default_value'], $requestslice);
71 }
72 }
73 else {
74 if ($form['#type'] == 'markup' || empty($form['#type']) ) {
75 $form['#value'] = check_plain($requestslice);
76 }
77 else {
78 $form['#value'] = $requestslice;
79 }
80 }
81 }