Loading captcha.inc +11 −0 Original line number Diff line number Diff line Loading @@ -168,6 +168,17 @@ function _captcha_required_for_user($captcha_sid, $form_id) { return TRUE; } /** * Get the CAPTCHA title. * * @return string * CAPTCHA title. */ function _captcha_get_title() { $title = \Drupal::config('captcha.settings')->get('title'); return Xss::filter($title); } /** * Get the CAPTCHA description. * Loading captcha.install +19 −0 Original line number Diff line number Diff line Loading @@ -166,3 +166,22 @@ function captcha_update_8902(&$sandbox) { } } } /** * Implements hook_update_N(). * * Handle and delete "add_captcha_description". */ function captcha_update_8903(&$sandbox) { $config_factory = \Drupal::configFactory(); $config = $config_factory->getEditable('captcha.settings'); $addDescription = $config->get('add_captcha_description'); // If description was disabled before, set 'description' to an empty string, // so it is disabled again in the newest version: if (!$addDescription) { $config->set('description', '')->save(); } // Delete old config: $config->clear('add_captcha_description')->save(); } captcha.libraries.yml 0 → 100644 +5 −0 Original line number Diff line number Diff line base: version: 1.0 css: theme: css/captcha.css: {} captcha.module +22 −30 Original line number Diff line number Diff line Loading @@ -77,6 +77,15 @@ function captcha_theme() { ]; } /** * Implements hook_theme_suggestions_HOOK(). */ function captcha_theme_suggestions_captcha(array $variables) { $suggestions = []; $suggestions[] = 'captcha__' . strtolower($variables['element']['#captcha_type_challenge']); return $suggestions; } /** * Implements hook_cron(). * Loading @@ -99,31 +108,19 @@ function captcha_cron() { * Render it in a section element if a description of the CAPTCHA * is available. Render it as is otherwise. */ function template_preprocess_captcha(&$variables) { $element = $variables['element']; function template_preprocess_captcha(&$variables, $hook, $info) { $variables['title'] = _captcha_get_title(); $variables['description'] = _captcha_get_description(); if (!empty($element['#description']) && isset($element['captcha_widgets'])) { $children_keys = Element::children($element); $captcha_children_output = ''; foreach ($children_keys as $key) { if (!empty($element[$key])) { $captcha_children_output .= \Drupal::service('renderer')->render($element[$key]); } } $variables['details'] = [ '#type' => 'details', '#title' => t('CAPTCHA'), '#description' => $element['#description'], '#children' => Markup::create($captcha_children_output), '#attributes' => [ 'id' => 'captcha', 'class' => ['captcha'], 'open' => [''], ], '#open' => TRUE, ]; } // Add an indicator, if the captcha widget is visible, to // account for _captcha_required_for_user(). // See Captcha::preRenderProcess(). // If the CAPTCHA was solved, hidden inputs have to be preserved to the form // but the CAPTCHA and its wrappers are not visible. // This variable exposed this to the theme layer transparently: $variables['is_visible'] = isset($variables['element']['captcha_widgets']); // Attach library $variables['#attached']['library'][] = 'captcha/base'; } /** Loading Loading @@ -198,11 +195,6 @@ function captcha_form_alter(array &$form, FormStateInterface $form_state, $form_ '#captcha_type' => $captcha_point->getCaptchaType(), ]; // Add a CAPTCHA description if required. if ($config->get('add_captcha_description')) { $captcha_element['#description'] = _captcha_get_description(); } // Get placement in form and insert in form. if ($captcha_placement = _captcha_get_captcha_placement($form_id, $form)) { $captchaService->insertCaptchaElement($form, $captcha_placement, $captcha_element); Loading config/install/captcha.settings.yml +1 −1 Original line number Diff line number Diff line 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.' title: 'CAPTCHA' administration_mode: false allow_on_admin_pages: false whitelist_ips: '' add_captcha_description: true wrong_captcha_response_message: 'The answer you entered for the CAPTCHA was not correct.' default_validation: 1 persistence: 1 Loading Loading
captcha.inc +11 −0 Original line number Diff line number Diff line Loading @@ -168,6 +168,17 @@ function _captcha_required_for_user($captcha_sid, $form_id) { return TRUE; } /** * Get the CAPTCHA title. * * @return string * CAPTCHA title. */ function _captcha_get_title() { $title = \Drupal::config('captcha.settings')->get('title'); return Xss::filter($title); } /** * Get the CAPTCHA description. * Loading
captcha.install +19 −0 Original line number Diff line number Diff line Loading @@ -166,3 +166,22 @@ function captcha_update_8902(&$sandbox) { } } } /** * Implements hook_update_N(). * * Handle and delete "add_captcha_description". */ function captcha_update_8903(&$sandbox) { $config_factory = \Drupal::configFactory(); $config = $config_factory->getEditable('captcha.settings'); $addDescription = $config->get('add_captcha_description'); // If description was disabled before, set 'description' to an empty string, // so it is disabled again in the newest version: if (!$addDescription) { $config->set('description', '')->save(); } // Delete old config: $config->clear('add_captcha_description')->save(); }
captcha.libraries.yml 0 → 100644 +5 −0 Original line number Diff line number Diff line base: version: 1.0 css: theme: css/captcha.css: {}
captcha.module +22 −30 Original line number Diff line number Diff line Loading @@ -77,6 +77,15 @@ function captcha_theme() { ]; } /** * Implements hook_theme_suggestions_HOOK(). */ function captcha_theme_suggestions_captcha(array $variables) { $suggestions = []; $suggestions[] = 'captcha__' . strtolower($variables['element']['#captcha_type_challenge']); return $suggestions; } /** * Implements hook_cron(). * Loading @@ -99,31 +108,19 @@ function captcha_cron() { * Render it in a section element if a description of the CAPTCHA * is available. Render it as is otherwise. */ function template_preprocess_captcha(&$variables) { $element = $variables['element']; function template_preprocess_captcha(&$variables, $hook, $info) { $variables['title'] = _captcha_get_title(); $variables['description'] = _captcha_get_description(); if (!empty($element['#description']) && isset($element['captcha_widgets'])) { $children_keys = Element::children($element); $captcha_children_output = ''; foreach ($children_keys as $key) { if (!empty($element[$key])) { $captcha_children_output .= \Drupal::service('renderer')->render($element[$key]); } } $variables['details'] = [ '#type' => 'details', '#title' => t('CAPTCHA'), '#description' => $element['#description'], '#children' => Markup::create($captcha_children_output), '#attributes' => [ 'id' => 'captcha', 'class' => ['captcha'], 'open' => [''], ], '#open' => TRUE, ]; } // Add an indicator, if the captcha widget is visible, to // account for _captcha_required_for_user(). // See Captcha::preRenderProcess(). // If the CAPTCHA was solved, hidden inputs have to be preserved to the form // but the CAPTCHA and its wrappers are not visible. // This variable exposed this to the theme layer transparently: $variables['is_visible'] = isset($variables['element']['captcha_widgets']); // Attach library $variables['#attached']['library'][] = 'captcha/base'; } /** Loading Loading @@ -198,11 +195,6 @@ function captcha_form_alter(array &$form, FormStateInterface $form_state, $form_ '#captcha_type' => $captcha_point->getCaptchaType(), ]; // Add a CAPTCHA description if required. if ($config->get('add_captcha_description')) { $captcha_element['#description'] = _captcha_get_description(); } // Get placement in form and insert in form. if ($captcha_placement = _captcha_get_captcha_placement($form_id, $form)) { $captchaService->insertCaptchaElement($form, $captcha_placement, $captcha_element); Loading
config/install/captcha.settings.yml +1 −1 Original line number Diff line number Diff line 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.' title: 'CAPTCHA' administration_mode: false allow_on_admin_pages: false whitelist_ips: '' add_captcha_description: true wrong_captcha_response_message: 'The answer you entered for the CAPTCHA was not correct.' default_validation: 1 persistence: 1 Loading