You are here

function captcha_pre_render_place_captcha in CAPTCHA 6

Same name and namespace in other branches
  1. 5.3 captcha.module \captcha_pre_render_place_captcha()

Pre_render function to place the CAPTCHA form element just above the last submit button

1 string reference to 'captcha_pre_render_place_captcha'
captcha_form_alter in ./captcha.module
Implementation of hook_form_alter().

File

./captcha.module, line 432
This module enables basic CAPTCHA functionality: administrators can add a CAPTCHA to desired forms that users without the 'skip CAPTCHA' permission (typically anonymous visitors) have to solve.

Code

function captcha_pre_render_place_captcha($form) {

  // search the weights of the buttons in the form
  $button_weights = array();
  foreach (element_children($form) as $key) {
    if ($key == 'buttons' || isset($form[$key]['#type']) && ($form[$key]['#type'] == 'submit' || $form[$key]['#type'] == 'button')) {
      $button_weights[] = $form[$key]['#weight'];
    }
  }
  if ($button_weights) {

    // set the weight of the CAPTCHA element a tiny bit smaller than the lightest button weight
    // (note that the default resolution of #weight values is 1/1000 (see drupal/includes/form.inc))
    $first_button_weight = min($button_weights);
    $form['captcha']['#weight'] = $first_button_weight - 0.5 / 1000.0;

    // make sure the form gets sorted before rendering
    unset($form['#sorted']);
  }
  return $form;
}