Commit 8e9d85eb authored by wundo's avatar wundo

Merge branch 'issue_3086495' into 8.x-1.x

parents ff6ba313 a924ab55
...@@ -5,41 +5,6 @@ ...@@ -5,41 +5,6 @@
* Functionality and helper functions for CAPTCHA administration. * Functionality and helper functions for CAPTCHA administration.
*/ */
/**
* Return an array with the available CAPTCHA types.
*
* For use as options array for a select form elements.
*
* @param bool $add_special_options
* If true: also add the 'default' option.
*
* @return array
* An associative array mapping "$module/$type" to
* "$type (from module $module)" with $module the module name
* implementing the CAPTCHA and $type the name of the CAPTCHA type.
*/
function _captcha_available_challenge_types($add_special_options = TRUE) {
$captcha_types = [];
if ($add_special_options) {
$captcha_types['default'] = t('Default challenge type');
}
// We do our own version of Drupal's module_invoke_all() here because
// we want to build an array with custom keys and values.
foreach (\Drupal::moduleHandler()->getImplementations('captcha') as $module) {
$result = call_user_func_array($module . '_captcha', ['list']);
if (is_array($result)) {
foreach ($result as $type) {
$captcha_types["$module/$type"] = t('@type (from module @module)', [
'@type' => $type,
'@module' => $module,
]);
}
}
}
return $captcha_types;
}
/** /**
* Helper function for generating an example challenge. * Helper function for generating an example challenge.
*/ */
......
...@@ -4,3 +4,6 @@ services: ...@@ -4,3 +4,6 @@ services:
arguments: ['@element_info'] arguments: ['@element_info']
tags: tags:
- { name: event_subscriber } - { name: event_subscriber }
captcha.helper:
class: Drupal\captcha\Service\CaptchaService
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace Drupal\captcha\Form; namespace Drupal\captcha\Form;
use Drupal\captcha\Service\CaptchaService;
use Drupal\Core\Entity\EntityForm; use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
...@@ -12,6 +13,13 @@ use Symfony\Component\HttpFoundation\RequestStack; ...@@ -12,6 +13,13 @@ use Symfony\Component\HttpFoundation\RequestStack;
*/ */
class CaptchaPointForm extends EntityForm { class CaptchaPointForm extends EntityForm {
/**
* The CAPTCHA helper service.
*
* @var \Drupal\captcha\Service\CaptchaService
*/
protected $captchaService;
/** /**
* The request stack. * The request stack.
* *
...@@ -25,8 +33,9 @@ class CaptchaPointForm extends EntityForm { ...@@ -25,8 +33,9 @@ class CaptchaPointForm extends EntityForm {
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* Constructor. * Constructor.
*/ */
public function __construct(RequestStack $request_stack) { public function __construct(RequestStack $request_stack, CaptchaService $captcha_service) {
$this->requestStack = $request_stack; $this->requestStack = $request_stack;
$this->captchaService = $captcha_service;
} }
/** /**
...@@ -39,7 +48,8 @@ class CaptchaPointForm extends EntityForm { ...@@ -39,7 +48,8 @@ class CaptchaPointForm extends EntityForm {
*/ */
public static function create(ContainerInterface $container) { public static function create(ContainerInterface $container) {
return new static( return new static(
$container->get('request_stack') $container->get('request_stack'),
$container->get('captcha.helper')
); );
} }
...@@ -84,9 +94,8 @@ class CaptchaPointForm extends EntityForm { ...@@ -84,9 +94,8 @@ class CaptchaPointForm extends EntityForm {
'#type' => 'select', '#type' => 'select',
'#title' => $this->t('Challenge type'), '#title' => $this->t('Challenge type'),
'#description' => $this->t('The CAPTCHA type to use for this form.'), '#description' => $this->t('The CAPTCHA type to use for this form.'),
'#default_value' => ($captcha_point->getCaptchaType() ?: $this->config('captcha.settings') '#default_value' => $captcha_point->getCaptchaType() ?: $this->config('captcha.settings')->get('default_challenge'),
->get('default_challenge')), '#options' => $this->captchaService->getAvailableChallengeTypes(),
'#options' => _captcha_available_challenge_types(),
]; ];
return $form; return $form;
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace Drupal\captcha\Form; namespace Drupal\captcha\Form;
use Drupal\captcha\Service\CaptchaService;
use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Extension\ModuleHandlerInterface;
...@@ -22,6 +23,13 @@ class CaptchaSettingsForm extends ConfigFormBase { ...@@ -22,6 +23,13 @@ class CaptchaSettingsForm extends ConfigFormBase {
*/ */
protected $cacheBackend; protected $cacheBackend;
/**
* The CAPTCHA helper service.
*
* @var \Drupal\captcha\Service\CaptchaService
*/
protected $captchaService;
/** /**
* The module handler. * The module handler.
* *
...@@ -39,10 +47,11 @@ class CaptchaSettingsForm extends ConfigFormBase { ...@@ -39,10 +47,11 @@ class CaptchaSettingsForm extends ConfigFormBase {
* @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler * @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
* Module handler. * Module handler.
*/ */
public function __construct(ConfigFactoryInterface $config_factory, CacheBackendInterface $cache_backend, ModuleHandlerInterface $moduleHandler) { public function __construct(ConfigFactoryInterface $config_factory, CacheBackendInterface $cache_backend, ModuleHandlerInterface $moduleHandler, CaptchaService $captcha_service) {
parent::__construct($config_factory); parent::__construct($config_factory);
$this->cacheBackend = $cache_backend; $this->cacheBackend = $cache_backend;
$this->moduleHandler = $moduleHandler; $this->moduleHandler = $moduleHandler;
$this->captchaService = $captcha_service;
} }
/** /**
...@@ -52,7 +61,8 @@ class CaptchaSettingsForm extends ConfigFormBase { ...@@ -52,7 +61,8 @@ class CaptchaSettingsForm extends ConfigFormBase {
return new static( return new static(
$container->get('config.factory'), $container->get('config.factory'),
$container->get('cache.default'), $container->get('cache.default'),
$container->get('module_handler') $container->get('module_handler'),
$container->get('captcha.helper')
); );
} }
...@@ -90,7 +100,7 @@ class CaptchaSettingsForm extends ConfigFormBase { ...@@ -90,7 +100,7 @@ class CaptchaSettingsForm extends ConfigFormBase {
'#type' => 'select', '#type' => 'select',
'#title' => $this->t('Default challenge type'), '#title' => $this->t('Default challenge type'),
'#description' => $this->t('Select the default challenge type for CAPTCHAs. This can be overridden for each form if desired.'), '#description' => $this->t('Select the default challenge type for CAPTCHAs. This can be overridden for each form if desired.'),
'#options' => _captcha_available_challenge_types(FALSE), '#options' => $this->captchaService->getAvailableChallengeTypes(FALSE),
'#default_value' => $config->get('default_challenge'), '#default_value' => $config->get('default_challenge'),
]; ];
......
<?php
namespace Drupal\captcha\Service;
/**
* Helper service for CAPTCHA module.
*/
class CaptchaService {
/**
* Return an array with the available CAPTCHA types.
*
* For use as options array for a select form elements.
*
* @param bool $add_special_options
* If true: also add the 'default' option.
*
* @return array
* An associative array mapping "$module/$type" to
* "$type (from module $module)" with $module the module name
* implementing the CAPTCHA and $type the name of the CAPTCHA type.
*/
public function getAvailableChallengeTypes(bool $add_special_options = TRUE) {
$challenges = [];
if ($add_special_options) {
$challenges['default'] = t('Default challenge type');
}
// We do our own version of Drupal's module_invoke_all() here because
// we want to build an array with custom keys and values.
foreach (\Drupal::moduleHandler()->getImplementations('captcha') as $module) {
$result = call_user_func_array($module . '_captcha', ['list']);
if (is_array($result)) {
foreach ($result as $type) {
$challenges["$module/$type"] = t('@type (from module @module)', [
'@type' => $type,
'@module' => $module,
]);
}
}
}
return $challenges;
}
}
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