You are here

public function ReCaptchaV3SettingsForm::buildForm in reCAPTCHA v3 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides ConfigFormBase::buildForm

File

src/Form/ReCaptchaV3SettingsForm.php, line 89

Class

ReCaptchaV3SettingsForm
Configure the google reCAPTCHA v3 api and fallback challenge.

Namespace

Drupal\recaptcha_v3\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config = $this
    ->config('recaptcha_v3.settings');
  $form['site_key'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Site key'),
    '#default_value' => $config
      ->get('site_key'),
    '#maxlength' => 40,
    '#description' => $this
      ->t('The site key given to you when you <a href="@url">register for reCAPTCHA</a>.', [
      '@url' => 'https://www.google.com/recaptcha/admin',
    ]),
    '#required' => TRUE,
  ];
  $form['secret_key'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Secret key'),
    '#default_value' => $config
      ->get('secret_key'),
    '#maxlength' => 40,
    '#description' => $this
      ->t('The secret key given to you when you <a href="@url">register for reCAPTCHA</a>.', [
      '@url' => 'https://www.google.com/recaptcha/admin',
    ]),
    '#required' => TRUE,
  ];
  $form['verify_hostname'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Local domain name validation'),
    '#default_value' => $config
      ->get('verify_hostname'),
    '#description' => $this
      ->t('Checks the hostname on your server when verifying a solution. Enable this validation only, if <em>Verify the origin of reCAPTCHA solutions</em> is unchecked for your key pair. Provides crucial security by verifying requests come from one of your listed domains.'),
  ];
  $challenges = $this->captchaService
    ->getAvailableChallengeTypes(FALSE);

  // Remove recaptcha v3 challenges from the list of available
  // fallback challenges.
  $challenges = array_filter($challenges, static function ($captcha_type) {
    return !(strpos($captcha_type, 'recaptcha_v3') === 0);
  }, ARRAY_FILTER_USE_KEY);
  $form['default_challenge'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Default fallback challenge type'),
    '#description' => $this
      ->t('Select the default fallback challenge type on verification fail.'),
    '#options' => $challenges,
    '#default_value' => $config
      ->get('default_challenge'),
    '#empty_option' => $this
      ->t('- None -'),
    '#empty_value' => '',
  ];
  $form['error_message'] = [
    '#type' => 'textfield',
    '#size' => 128,
    '#title' => $this
      ->t('Error message'),
    '#description' => $this
      ->t('This message will be displayed to user in case of failed recaptcha v3 verification.'),
    '#default_value' => $config
      ->get('error_message'),
  ];
  $form['cacheable'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Cacheable'),
    '#description' => $this
      ->t('Make captcha cacheble: can lead to some validation errors like "unknown CAPTCHA session ID".'),
    '#default_value' => $config
      ->get('cacheable'),
  ];
  return parent::buildForm($form, $form_state);
}