You are here

function recaptcha_v3_captcha in reCAPTCHA v3 8

Same name and namespace in other branches
  1. 7 recaptcha_v3.module \recaptcha_v3_captcha()

Implements hook_captcha().

File

./recaptcha_v3.module, line 118
Contains recaptcha_v3.module.

Code

function recaptcha_v3_captcha($op, $captcha_type = '', $captcha_sid = NULL) {
  switch ($op) {
    case 'list':
      return array_keys(ReCaptchaV3Action::loadMultiple());
    case 'generate':
      $captcha = [];
      if ($recaptcha_v3_action = ReCaptchaV3Action::load($captcha_type)) {
        $config = \Drupal::config('recaptcha_v3.settings');
        $captcha['form']['captcha_response'] = [
          '#type' => 'hidden',
          '#default_value' => '',
          '#recaptcha_v3' => TRUE,
          '#attributes' => [
            // Need add id, because element should have id or
            // 'selector' property should exist in #ajax array
            // to attaching event for triggering ajax request.
            'id' => Html::getUniqueId('recaptcha_v3_token'),
            'class' => [
              'recaptcha-v3-token',
            ],
            'data-recaptcha-v3-action' => $recaptcha_v3_action
              ->id(),
            'data-recaptcha-v3-site-key' => $config
              ->get('site_key'),
          ],
          '#attached' => [
            'library' => [
              'recaptcha_v3/recaptcha_v3',
            ],
          ],
          '#ajax' => [
            'callback' => 'recaptcha_v3_ajax_callback',
            'event' => 'change',
          ],
        ];

        // Flag that indicates that current captcha element is recaptcha_v3.
        $captcha['form']['is_recaptcha_v3'] = [
          '#type' => 'hidden',
          '#value' => 1,
        ];
        $captcha['solution'] = TRUE;
        $captcha['captcha_validate'] = 'recaptcha_v3_validate';
        $captcha['cacheable'] = (bool) $config
          ->get('cacheable');
      }
      return $captcha;
  }
}