Skip to content
prepopulate.module 2.67 KiB
Newer Older
eric Farris's avatar
eric Farris committed
<?php

 * Fill form elements with data from GET or POST values.
Addison Berry's avatar
Addison Berry committed
 * Originally written by ea. Farris <eafarris@gmail.com>
 * Based on an idea from chx, from the conversation at
 * http://www.drupal.org/node/27155.
jbrauer's avatar
jbrauer committed
 * Implements hook_help().
Addison Berry's avatar
Addison Berry committed
function prepopulate_help($path, $arg) {
  switch ($path) {
eric Farris's avatar
eric Farris committed
    case 'admin/modules#description':
      return t('Pre-populates forms with HTTP GET or POST data');
Addison Berry's avatar
Addison Berry committed
  }
}
eric Farris's avatar
eric Farris committed

jbrauer's avatar
jbrauer committed
 * Implements hook_form_alter().
Addison Berry's avatar
Addison Berry committed
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) {
jbrauer's avatar
jbrauer committed
    $request = (array) $_REQUEST['edit'];
    _prepopulate_request_walk($form, $request);
  return $form;
Addison Berry's avatar
Addison Berry committed
}
eric Farris's avatar
eric Farris committed

 * Internal helper to set element values from the $_REQUEST variable.
Addison Berry's avatar
Addison Berry committed
 *
jbrauer's avatar
jbrauer committed
 * @param array &$form
 *   A form element.
 * @param mixed &$request_slice
Addison Berry's avatar
Addison Berry committed
 *   String or array. Value(s) to be applied to the element.
function _prepopulate_request_walk(&$form, &$request_slice) {
  $limited_types = array(
    'actions',
    'button',
    'container',
    'token',
    'value',
    'hidden',
    'image_button',
    'password',
    'password_confirm',
    'text_format',
    'markup',
    );
  if (is_array($request_slice)) {
    foreach (array_keys($request_slice) as $request_variable) {
      if (element_child($request_variable) && !empty($form[$request_variable]) &&
       (!isset($form[$request_variable]['#type']) || !in_array($form[$request_variable]['#type'], $limited_types))) {
        if (!isset($form[$request_variable]['#access']) || $form[$request_variable]['#access'] != FALSE) {
          _prepopulate_request_walk($form[$request_variable], $request_slice[$request_variable]);
eric Farris's avatar
 
eric Farris committed
      }
Addison Berry's avatar
Addison Berry committed
    }
    if (!empty($form['#default_value']) && is_array($form['#default_value'])) {
      $form['#default_value'] = array_merge($form['#default_value'], $request_slice);
Addison Berry's avatar
Addison Berry committed
  }
jbrauer's avatar
jbrauer committed
    if ($form['#type'] == 'markup' || empty($form['#type'])) {
      $form['#value'] = check_plain($request_slice);
      $form['#value'] = $request_slice;
    if ($form['#type'] == 'checkboxes' || $form['#type'] == 'checkbox') {
      if (!empty($form['#value'])) {
        $form['#checked'] = TRUE;
      }
      else {
        $form['#checked'] = FALSE;
      }
    }
jbrauer's avatar
jbrauer committed
}