diff --git a/captcha.module b/captcha.module index 8634393..05dc38e 100755 --- a/captcha.module +++ b/captcha.module @@ -142,17 +142,24 @@ function captcha_form_alter(array &$form, FormStateInterface $form_state, $form_ // Visitor does not have permission to skip CAPTCHAs. module_load_include('inc', 'captcha'); if (!$account->hasPermission('skip CAPTCHA')) { - /* @var CaptchaPoint $captcha_point */ $captcha_point = \Drupal::entityTypeManager() ->getStorage('captcha_point') ->load($form_id); + $captcha_type = ''; + if ($config->get('default_challenge_on_nonlisted_forms')) { + $captcha_type = $config->get('default_challenge'); + } if ($captcha_point && $captcha_point->status()) { + $captcha_type = $captcha_point->getCaptchaType(); + } + + if (!empty($captcha_type)) { // Build CAPTCHA form element. $captcha_element = [ '#type' => 'captcha', - '#captcha_type' => $captcha_point->getCaptchaType(), + '#captcha_type' => $captcha_type, ]; // Add a CAPTCHA description if required. diff --git a/config/install/captcha.settings.yml b/config/install/captcha.settings.yml index ac797b0..2bfaaac 100755 --- a/config/install/captcha.settings.yml +++ b/config/install/captcha.settings.yml @@ -1,5 +1,6 @@ default_challenge: 'captcha/Math' description: 'This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.' +default_challenge_on_nonlisted_forms: false administration_mode: false allow_on_admin_pages: false add_captcha_description: true diff --git a/config/schema/captcha.settings.yml b/config/schema/captcha.settings.yml index 3cdcddb..1033517 100755 --- a/config/schema/captcha.settings.yml +++ b/config/schema/captcha.settings.yml @@ -7,10 +7,13 @@ captcha.settings: mapping: default_challenge: type: string - label: 'The default challenge for captcha.' + label: 'The default challenge for CAPTCHA.' description: type: label label: 'The default captcha description.' + default_challenge_on_nonlisted_forms: + type: boolean + label: 'Add default challenge of CAPTCHA to all forms.' administration_mode: type: boolean label: 'Add CAPTCHA administration links to forms' diff --git a/src/Form/CaptchaSettingsForm.php b/src/Form/CaptchaSettingsForm.php index 24eb912..dde6b30 100755 --- a/src/Form/CaptchaSettingsForm.php +++ b/src/Form/CaptchaSettingsForm.php @@ -82,6 +82,13 @@ class CaptchaSettingsForm extends ConfigFormBase { '#default_value' => $config->get('default_challenge'), ]; + $form['form_protection']['default_challenge_on_nonlisted_forms'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Default challenge on non-listed forms.'), + '#default_value' => $config->get('default_challenge_on_nonlisted_forms'), + '#description' => $this->t('Normally, no challenge is added to forms that are not listed above. Enabling this option will add the default challenge instead.'), + ]; + // Field for the CAPTCHA administration mode. $form['form_protection']['administration_mode'] = [ '#type' => 'checkbox', @@ -89,6 +96,7 @@ class CaptchaSettingsForm extends ConfigFormBase { '#default_value' => $config->get('administration_mode'), '#description' => $this->t('This option makes it easy to manage CAPTCHA settings on forms. When enabled, users with the administer CAPTCHA settings permission will see a fieldset with CAPTCHA administration links on all forms, except on administrative pages.'), ]; + // Field for the CAPTCHAs on admin pages. $form['form_protection']['allow_on_admin_pages'] = [ '#type' => 'checkbox', @@ -203,6 +211,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('default_challenge_on_nonlisted_forms', $form_state->getValue('default_challenge_on_nonlisted_forms')); // CAPTCHA description stuff. $config->set('add_captcha_description', $form_state->getValue('add_captcha_description'));