Commit 0d47a033 authored by fnuppy's avatar fnuppy Committed by elachlan

Issue #2025181 by fnuppy, ddrozdik: Add CAPTCHA to all forms by default

parent fddcc334
......@@ -165,6 +165,15 @@ function captcha_form_alter(array &$form, FormStateInterface $form_state, $form_
->getStorage('captcha_point')
->load($form_id);
if (!$captcha_point && $config->get('enabled_default')) {
// Create fake captcha point without saving.
$captcha_point = new CaptchaPoint([
'formId' => $form_id,
'captchaType' => $config->get('default_challenge'),
], 'captcha_point');
$captcha_point->enable();
}
if ($captcha_point && $captcha_point->status()) {
// Build CAPTCHA form element.
$captcha_element = [
......
enabled_default: 0
default_challenge: 'captcha/Math'
description: 'This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.'
administration_mode: false
......
......@@ -5,6 +5,9 @@ captcha.settings:
type: config_object
label: 'External Links Settings'
mapping:
enabled_default:
type: integer
label: 'Add captcha to all forms.'
default_challenge:
type: string
label: 'The default challenge for captcha.'
......
......@@ -92,6 +92,13 @@ class CaptchaSettingsForm extends ConfigFormBase {
'#default_value' => $config->get('default_challenge'),
];
// Option for enabling CAPTCHA for all forms.
$form['form_protection']['enabled_default'] = [
'#type' => 'checkbox',
'#title' => $this->t('Default challenge on non-listed forms.'),
'#description' => $this->t('Enable CAPTCHA for every form not listed in "CAPTCHA points"'),
'#default_value' => $config->get('enabled_default'),
];
// Field for the CAPTCHA administration mode.
$form['form_protection']['administration_mode'] = [
'#type' => 'checkbox',
......@@ -213,6 +220,7 @@ class CaptchaSettingsForm extends ConfigFormBase {
$config->set('administration_mode', $form_state->getValue('administration_mode'));
$config->set('allow_on_admin_pages', $form_state->getValue('allow_on_admin_pages'));
$config->set('default_challenge', $form_state->getValue('default_challenge'));
$config->set('enabled_default', $form_state->getValue('enabled_default'));
// CAPTCHA description stuff.
$config->set('add_captcha_description', $form_state->getValue('add_captcha_description'));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment