You are here

public function CaptchaPointForm::form in CAPTCHA 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/CaptchaPointForm.php, line 61

Class

CaptchaPointForm
Entity Form to edit CAPTCHA points.

Namespace

Drupal\captcha\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);
  module_load_include('inc', 'captcha', 'captcha.admin');

  /* @var CaptchaPointInterface $captchaPoint */
  $captcha_point = $this->entity;

  // Support to set a default form_id through a query argument.
  $request = $this->requestStack
    ->getCurrentRequest();
  if ($captcha_point
    ->isNew() && !$captcha_point
    ->id() && $request->query
    ->has('form_id')) {
    $captcha_point
      ->set('formId', $request->query
      ->get('form_id'));
    $captcha_point
      ->set('label', $request->query
      ->get('form_id'));
  }
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Form ID'),
    '#description' => $this
      ->t('Also works with the base form ID.'),
    '#default_value' => $captcha_point
      ->label(),
    '#required' => TRUE,
  ];
  $form['formId'] = [
    '#type' => 'machine_name',
    '#default_value' => $captcha_point
      ->id(),
    '#machine_name' => [
      'exists' => 'captcha_point_load',
    ],
    '#disable' => !$captcha_point
      ->isNew(),
    '#required' => TRUE,
  ];

  // Select widget for CAPTCHA type.
  $form['captchaType'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Challenge type'),
    '#description' => $this
      ->t('The CAPTCHA type to use for this form.'),
    '#default_value' => $captcha_point
      ->getCaptchaType() ?: $this
      ->config('captcha.settings')
      ->get('default_challenge'),
    '#options' => $this->captchaService
      ->getAvailableChallengeTypes(),
  ];
  return $form;
}