prepopulate.module 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * @file
  4. * Fill form elements with data from GET or POST values.
  5. *
  6. * Originally written by ea. Farris <eafarris@gmail.com>
  7. * Based on an idea from chx, from the conversation at
  8. * http://www.drupal.org/node/27155.
  9. */
  10. /**
  11. * Implements hook_help().
  12. */
  13. function prepopulate_help($path, $arg) {
  14. switch ($path) {
  15. case 'admin/modules#description':
  16. return t('Pre-populates forms with HTTP GET or POST data');
  17. }
  18. }
  19. /**
  20. * Implements hook_form_alter().
  21. */
  22. function prepopulate_form_alter(&$form, $form_state, $form_id) {
  23. // If this is a subsequent step of a multi-step form, the prepopulate values
  24. // have done their work, and the user may have modified them: bail.
  25. if (!empty($form_state['rebuild'])) {
  26. return;
  27. }
  28. if (isset($_REQUEST['edit'])) {
  29. $form['#after_build'][] = 'prepopulate_after_build';
  30. }
  31. }
  32. /**
  33. * An #after_build function to set the values prepopulated in the request.
  34. */
  35. function prepopulate_after_build($form, &$form_state) {
  36. if (isset($_REQUEST['edit'])) {
  37. $request = (array) $_REQUEST['edit'];
  38. _prepopulate_request_walk($form, $request);
  39. }
  40. return $form;
  41. }
  42. /**
  43. * Internal helper to set element values from the $_REQUEST variable.
  44. *
  45. * @param array &$form
  46. * A form element.
  47. * @param mixed &$request_slice
  48. * String or array. Value(s) to be applied to the element.
  49. */
  50. function _prepopulate_request_walk(&$form, &$request_slice) {
  51. $limited_types = array(
  52. 'actions',
  53. 'button',
  54. 'container',
  55. 'token',
  56. 'value',
  57. 'hidden',
  58. 'image_button',
  59. 'password',
  60. 'password_confirm',
  61. 'text_format',
  62. 'markup',
  63. );
  64. if (is_array($request_slice)) {
  65. foreach (array_keys($request_slice) as $request_variable) {
  66. if (element_child($request_variable) && !empty($form[$request_variable]) &&
  67. (!isset($form[$request_variable]['#type']) || !in_array($form[$request_variable]['#type'], $limited_types))) {
  68. if (!isset($form[$request_variable]['#access']) || $form[$request_variable]['#access'] != FALSE) {
  69. _prepopulate_request_walk($form[$request_variable], $request_slice[$request_variable]);
  70. }
  71. }
  72. }
  73. if (!empty($form['#default_value']) && is_array($form['#default_value'])) {
  74. $form['#default_value'] = array_merge($form['#default_value'], $request_slice);
  75. }
  76. }
  77. else {
  78. if ($form['#type'] == 'markup' || empty($form['#type'])) {
  79. $form['#value'] = check_plain($request_slice);
  80. }
  81. else {
  82. $form['#value'] = $request_slice;
  83. }
  84. if ($form['#type'] == 'checkboxes' || $form['#type'] == 'checkbox') {
  85. if (!empty($form['#value'])) {
  86. $form['#checked'] = TRUE;
  87. }
  88. else {
  89. $form['#checked'] = FALSE;
  90. }
  91. }
  92. }
  93. }