You are here

public function MathCaptchaSettingsForm::buildForm in CAPTCHA Pack 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

math_captcha/src/Form/MathCaptchaSettingsForm.php, line 30

Class

MathCaptchaSettingsForm
Math CAPTCHA settings form.

Namespace

Drupal\math_captcha\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config = $this
    ->config('math_captcha.settings');
  $form = [];
  $enabled_challenges = _math_captcha_enabled_challenges();
  $form['math_captcha_enabled_challenges'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Enabled math challenges'),
    '#options' => [
      'addition' => $this
        ->t('Addition: x + y = z'),
      'subtraction' => $this
        ->t('Subtraction: x - y = z'),
      'multiplication' => $this
        ->t('Multiplication: x * y = z'),
    ],
    '#default_value' => $enabled_challenges,
    '#description' => $this
      ->t('Select the math challenges you want to enable.'),
  ];
  $form['math_captcha_textual_numbers'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Textual representation of numbers'),
    '#default_value' => $config
      ->get('math_captcha_textual_numbers'),
    '#description' => $this
      ->t('When enabled, the numbers in the challenge will get a textual representation if available. E.g. "four" instead of "4".'),
  ];
  $form['math_captcha_textual_operators'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Textual representation of operators'),
    '#default_value' => $config
      ->get('math_captcha_textual_operators'),
    '#description' => $this
      ->t('When enabled, the operators in the challenge will get a textual representation if available. E.g. "plus" instead of "+".'),
  ];

  // Addition challenge.
  $form['math_captcha_addition'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Addition challenge: x + y = z'),
    '#states' => [
      'invisible' => [
        ':input[name="math_captcha_enabled_challenges[addition]"]' => [
          'checked' => FALSE,
        ],
      ],
    ],
  ];
  $form['math_captcha_addition']['math_captcha_addition_argmax'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Maximum value for x and y'),
    '#default_value' => $config
      ->get('math_captcha_addition_argmax'),
    '#maxlength' => 3,
    '#size' => 3,
    '#states' => [
      'required' => [
        ':input[name="math_captcha_enabled_challenges[addition]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['math_captcha_addition']['math_captcha_addition_allow_negative'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Allow negative values.'),
    '#default_value' => $config
      ->get('math_captcha_addition_allow_negative'),
  ];

  // Subtraction challenge.
  $form['math_captcha_subtraction'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Subtraction challenge: x - y = z'),
    '#states' => [
      'invisible' => [
        ':input[name="math_captcha_enabled_challenges[subtraction]"]' => [
          'checked' => FALSE,
        ],
      ],
    ],
  ];
  $form['math_captcha_subtraction']['math_captcha_subtraction_argmax'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Maximum value for x and y'),
    '#default_value' => $config
      ->get('math_captcha_subtraction_argmax'),
    '#maxlength' => 3,
    '#size' => 3,
    '#states' => [
      'required' => [
        ':input[name="math_captcha_enabled_challenges[subtraction]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['math_captcha_subtraction']['math_captcha_subtraction_allow_negative'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Allow negative values.'),
    '#default_value' => $config
      ->get('math_captcha_subtraction_allow_negative'),
  ];

  // Multiplication challenge.
  $form['math_captcha_multiplication'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Multiplication challenge: x * y = z'),
    '#states' => [
      'invisible' => [
        ':input[name="math_captcha_enabled_challenges[multiplication]"]' => [
          'checked' => FALSE,
        ],
      ],
    ],
  ];
  $form['math_captcha_multiplication']['math_captcha_multiplication_argmax'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Maximum value for x and y'),
    '#default_value' => $config
      ->get('math_captcha_multiplication_argmax'),
    '#maxlength' => 3,
    '#size' => 3,
    '#states' => [
      'required' => [
        ':input[name="math_captcha_enabled_challenges[multiplication]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['math_captcha_multiplication']['math_captcha_multiplication_allow_negative'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Allow negative values.'),
    '#default_value' => $config
      ->get('math_captcha_multiplication_allow_negative'),
  ];
  return parent::buildForm($form, $form_state);
}