You are here

public function ReCaptchaV3ActionForm::form in reCAPTCHA v3 8

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

src/Form/ReCaptchaV3ActionForm.php, line 45

Class

ReCaptchaV3ActionForm
Form controller for the recaptcha_v3_action entity edit forms.

Namespace

Drupal\recaptcha_v3\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);
  $recaptcha_v3_action = $this->entity;
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#maxlength' => 255,
    '#default_value' => $recaptcha_v3_action
      ->label(),
    '#description' => $this
      ->t('Label for the reCAPTCHA v3 action.'),
    '#required' => TRUE,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $recaptcha_v3_action
      ->id(),
    '#required' => TRUE,
    '#machine_name' => [
      'exists' => [
        ReCaptchaV3Action::class,
        'load',
      ],
    ],
    '#disabled' => !$recaptcha_v3_action
      ->isNew(),
  ];
  $form['threshold'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Threshold'),
    '#min' => 0,
    '#max' => 1,
    '#step' => 0.1,
    '#required' => TRUE,
    '#default_value' => $recaptcha_v3_action
      ->getThreshold(),
  ];

  // @todo the same code lines using in several other places
  // need to refactor this.
  // Maybe create method in recaptcha v3 action storage?
  $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);
  $challenges = [
    'default' => $this
      ->t('Default fallback challenge'),
  ] + $challenges;
  $form['challenge'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Fallback challenge'),
    '#description' => $this
      ->t('Select the fallback challenge on reCAPTCHA v3 user validation fail.'),
    '#options' => $challenges,
    '#default_value' => $recaptcha_v3_action
      ->getChallenge(),
    '#empty_option' => $this
      ->t('- None -'),
    '#empty_value' => '',
  ];
  return $form;
}