You are here

public static function Captcha::preRenderProcess in CAPTCHA 8

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

array $element: The CAPTCHA form element.

Return value

array The manipulated element.

1 call to Captcha::preRenderProcess()
captcha_pre_render_process in ./captcha.module
Pre-render callback for additional processing of a CAPTCHA form element.

File

src/Element/Captcha.php, line 264

Class

Captcha
Defines the CAPTCHA form element with default properties.

Namespace

Drupal\captcha\Element

Code

public static function preRenderProcess(array $element) {
  module_load_include('inc', 'captcha');

  // 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;
}