You are here

function recaptcha_v3_pre_captcha_element_process in reCAPTCHA v3 8

Perform replacing of the recaptcha element by fallback challenge.

This happens in two cases: 1. form was submitted 2. performed form API ajax request.

Parameters

array $element: The recaptcha v3 form element.

\Drupal\Core\Form\FormStateInterface $form_state: The recaptcha v3 form state.

array $complete_form: The recaptcha v3 complete form object.

Return value

mixed Set fallback challenge if recaptcha v3 fail.

1 string reference to 'recaptcha_v3_pre_captcha_element_process'
recaptcha_v3_element_info_alter in ./recaptcha_v3.module
Implements hook_element_info_alter().

File

./recaptcha_v3.module, line 85
Contains recaptcha_v3.module.

Code

function recaptcha_v3_pre_captcha_element_process(array &$element, FormStateInterface $form_state, array &$complete_form) {

  // If form is processed input then recaptcha v3 response should be in
  // form values and need replace reCAPTCHA v3 element by fallback
  // challenge before captcha module element process callback, because,
  // otherwise, in case of error, form will not rebuild
  // and recaptcha v3 element will return again.
  if ($form_state
    ->isProcessingInput()) {
    \Drupal::moduleHandler()
      ->loadInclude('captcha', 'inc', 'captcha');
    list($captcha_type_module, $captcha_type_challenge) = _captcha_parse_captcha_type($element['#captcha_type']);
    if ($captcha_type_module === 'recaptcha_v3') {
      $action = ReCaptchaV3Action::load($captcha_type_challenge);
      $challenge = $action ? $action
        ->getChallenge() : 'default';

      // Replacing 'default' challenge by the real captcha challenge.
      if ($challenge === 'default') {
        $challenge = \Drupal::config('recaptcha_v3.settings')
          ->get('default_challenge');
      }
      if ($challenge) {
        $element['#captcha_type'] = $challenge;
      }
      $form_state
        ->setTemporaryValue('recaptcha_v3_action_name', $captcha_type_challenge);
    }
  }
  return $element;
}