You are here

function _botcha_insert_botcha_element in BOTCHA Spam Prevention 6

Same name and namespace in other branches
  1. 6.2 botcha.inc \_botcha_insert_botcha_element()
  2. 6.3 botcha.module \_botcha_insert_botcha_element()
  3. 7 botcha.inc \_botcha_insert_botcha_element()
  4. 7.2 botcha.inc \_botcha_insert_botcha_element()
  5. 7.3 botcha.module \_botcha_insert_botcha_element()

Helper function to insert a BOTCHA element in a form before a given form element.

Parameters

$form the form to add the BOTCHA element to.:

$placement information where the BOTCHA element should be inserted.: $placement should be an associative array with fields:

  • 'path': path (array of path items) of the container in the form where the BOTCHA element should be inserted.
  • 'key': the key of the element before which the BOTCHA element should be inserted. If the field 'key' is undefined or NULL, the BOTCHA will just be appended to the container.
  • 'weight': if 'key' is not NULL: should be the weight of the element defined by 'key'. If 'key' is NULL and weight is not NULL: set the weight property of the BOTCHA element to this value.

$botcha_element the BOTCHA element to insert.:

1 call to _botcha_insert_botcha_element()
botcha_form_alter in ./botcha.module
Implementation of hook_form_alter().

File

./botcha.inc, line 196
General BOTCHA functionality and helper functions.

Code

function _botcha_insert_botcha_element(&$form, $placement, $botcha_element) {

  // Get path, target and target weight or use defaults if not available.
  $target_key = isset($placement['key']) ? $placement['key'] : NULL;
  $target_weight = isset($placement['weight']) ? $placement['weight'] : NULL;
  $path = isset($placement['path']) ? $placement['path'] : array();

  // Walk through the form along the path.
  $form_stepper =& $form;
  foreach ($path as $step) {
    if (isset($form_stepper[$step])) {
      $form_stepper =& $form_stepper[$step];
    }
    else {

      // Given path is invalid: stop stepping and
      // continue in best effort (append instead of insert).
      $target_key = NULL;
      break;
    }
  }

  // If no target is available: just append the BOTCHA element to the container.
  if ($target_key == NULL || !array_key_exists($target_key, $form_stepper)) {

    // Optionally, set weight of BOTCHA element.
    if ($target_weight != NULL) {
      $botcha_element['#weight'] = $target_weight;
    }
    $form_stepper['botcha'] = $botcha_element;
  }
  else {

    // If target has a weight: set weight of BOTCHA element a bit smaller
    // and just append the BOTCHA: sorting will fix the ordering anyway.
    if ($target_weight != NULL) {
      $botcha_element['#weight'] = $target_weight - 0.1;
      $form_stepper['botcha'] = $botcha_element;
    }
    else {

      // If we can't play with weights: insert the BOTCHA element at the right position.
      // Because PHP lacks a function for this (array_splice() comes close,
      // but it does not preserve the key of the inserted element), we do it by hand:
      // chop of the end, append the BOTCHA element and put the end back.
      $offset = array_search($target_key, array_keys($form_stepper));
      $end = array_splice($form_stepper, $offset);
      $form_stepper['botcha'] = $botcha_element;
      foreach ($end as $k => $v) {
        $form_stepper[$k] = $v;
      }
    }
  }
}