You are here

public function CaptchaExamplesForm::buildForm in CAPTCHA 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 FormInterface::buildForm

File

src/Form/CaptchaExamplesForm.php, line 53

Class

CaptchaExamplesForm
Displays the captcha settings form.

Namespace

Drupal\captcha\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $module = NULL, $challenge = NULL) {
  module_load_include('inc', 'captcha', 'captcha.admin');
  $form = [];
  if ($module && $challenge) {

    // Generate 10 example challenges.
    for ($i = 0; $i < 10; $i++) {
      $form["challenge_{$i}"] = $this
        ->buildChallenge($module, $challenge);
    }
  }
  else {

    // Generate a list with examples of the available CAPTCHA types.
    $form['info'] = [
      '#markup' => $this
        ->t('This page gives an overview of all available challenge types, generated with their current settings.'),
    ];
    $modules_list = $this->moduleHandler
      ->getImplementations('captcha');
    foreach ($modules_list as $mkey => $module) {
      $challenges = call_user_func_array($module . '_captcha', [
        'list',
      ]);
      if ($challenges) {
        foreach ($challenges as $ckey => $challenge) {
          $form["captcha_{$mkey}_{$ckey}"] = [
            '#type' => 'details',
            '#title' => $this
              ->t('Challenge %challenge by module %module', [
              '%challenge' => $challenge,
              '%module' => $module,
            ]),
            'challenge' => $this
              ->buildChallenge($module, $challenge),
            'more_examples' => [
              '#markup' => Link::fromTextAndUrl($this
                ->t('10 more examples of this challenge.'), Url::fromRoute('captcha_examples', [
                'module' => $module,
                'challenge' => $challenge,
              ]))
                ->toString(),
            ],
          ];
        }
      }
    }
  }
  return $form;
}