You are here

function captcha_pre_render_process in CAPTCHA 6.2

Same name and namespace in other branches
  1. 8 captcha.module \captcha_pre_render_process()
  2. 7 captcha.module \captcha_pre_render_process()

Pre-render callback for additional processing of a CAPTCHA form element.

This encompasses tasks that should happen after the general FAPI processing (building, submission and validation) but before rendering (e.g. storing the solution).

Parameters

$element the CAPTCHA form element:

Return value

the manipulated element

1 string reference to 'captcha_pre_render_process'
captcha_process in ./captcha.module
Process callback for CAPTCHA form element.

File

./captcha.module, line 668
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_process($element) {

  // Get form and CAPTCHA information.
  $captcha_info = $element['#captcha_info'];
  $form_id = $captcha_info['form_id'];
  $captcha_sid = (int) $captcha_info['captcha_sid'];

  // Check if CAPTCHA is still required.
  // This check is done in a first phase during the element processing
  // (@see captcha_process), but it is also done here for better support
  // of multi-page forms. Take previewing a node submission for example:
  // when the challenge is solved correctely on preview, the form is still
  // not completely submitted, but the CAPTCHA can be skipped.
  if (_captcha_required_for_user($captcha_sid, $form_id) || $element['#captcha_admin_mode']) {

    // Update captcha_sessions table: store the solution of the generated CAPTCHA.
    _captcha_update_captcha_session($captcha_sid, $captcha_info['solution']);

    // Handle the response field if it is available and if it is a textfield.
    if (isset($element['captcha_widgets']['captcha_response']['#type']) && $element['captcha_widgets']['captcha_response']['#type'] == 'textfield') {

      // Before rendering: presolve an admin mode challenge or
      // empty the value of the captcha_response form item.
      $value = $element['#captcha_admin_mode'] ? $captcha_info['solution'] : '';
      $element['captcha_widgets']['captcha_response']['#value'] = $value;
    }
  }
  else {

    // Remove CAPTCHA widgets from form.
    unset($element['captcha_widgets']);
  }
  return $element;
}