You are here

prepopulate.module in Prepopulate 5

Fill form elements with data from GET.

Originally written by ea. Farris <eafarris@gmail.com> Based on an idea from chx, from the conversation at http://www.drupal.org/node/27155.

File

prepopulate.module
View source
<?php

/**
 * @file
 *  Fill form elements with data from GET.
 *
 *  Originally written by ea. Farris <eafarris@gmail.com>
 *  Based on an idea from chx, from the conversation at
 *  http://www.drupal.org/node/27155.
 */

/**
 * Implementation of hook_help().
 */
function prepopulate_help($section) {
  switch ($section) {
    case 'admin/modules#description':
      return t('Pre-populates forms with HTTP GET data');
      break;
  }

  // endswitch $section
}

// endfunction prepopulate_help()

/**
 * Implementation of hook_form_alter().
 */
function prepopulate_form_alter($form_id, &$form) {
  if (isset($_GET['edit'])) {
    $form['#after_build'][] = 'prepopulate_after_build';
  }
}

/**
 * Implementation of prepopulate_after_build().
 */
function prepopulate_after_build($form_id, &$form) {
  $node = $form['#node'];
  if (isset($_GET['edit'])) {
    foreach (array_keys((array) $_GET['edit']) as $getvar) {
      if (element_child($getvar) && is_array($form) && !is_null($form[$getvar]) && $form[$getvar]['#type'] != "value") {
        if (!isset($form[$getvar]['#access']) || $form[$getvar]['#access'] != FALSE) {
          _prepopulate_get_walk($form[$getvar], $getslice[$getvar]);
        }
      }
    }
  }
}

// endfunction prepopulate_form_alter()

/**
 * Internal helper to get variables from the $_GET.
 */
function _prepopulate_get_walk(&$form, &$getslice) {
  if (!is_array($getslice)) {
    $form['#default_value'] = $getslice;
  }
  else {
    foreach (array_keys($getslice) as $getvar) {
      if (element_child($getvar) && is_array($form) && !is_null($form[$getvar]) && $form[$getvar]['#type'] != "value") {
        if (!isset($form[$getvar]['#access']) || $form[$getvar]['#access'] != FALSE) {
          _prepopulate_get_walk($form[$getvar], $getslice[$getvar]);
        }
      }
    }

    // endforeach walking through get
  }

  // endif getslice is not an array
}

// endfunction _prepopulate_get_walk()

Functions

Namesort descending Description
prepopulate_after_build Implementation of prepopulate_after_build().
prepopulate_form_alter Implementation of hook_form_alter().
prepopulate_help Implementation of hook_help().
_prepopulate_get_walk Internal helper to get variables from the $_GET.