Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Open sidebar
drupal.org
captcha
Commits
8e9d85eb
Commit
8e9d85eb
authored
Oct 12, 2019
by
Fabiano Sant'Ana
Browse files
Merge branch 'issue_3086495' into 8.x-1.x
parents
ff6ba313
a924ab55
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
76 additions
and
43 deletions
+76
-43
captcha.admin.inc
captcha.admin.inc
+0
-35
captcha.services.yml
captcha.services.yml
+3
-0
src/Form/CaptchaPointForm.php
src/Form/CaptchaPointForm.php
+14
-5
src/Form/CaptchaSettingsForm.php
src/Form/CaptchaSettingsForm.php
+13
-3
src/Service/CaptchaService.php
src/Service/CaptchaService.php
+46
-0
No files found.
captcha.admin.inc
View file @
8e9d85eb
...
...
@@ -5,41 +5,6 @@
* 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.
*/
...
...
captcha.services.yml
View file @
8e9d85eb
...
...
@@ -4,3 +4,6 @@ services:
arguments
:
[
'
@element_info'
]
tags
:
-
{
name
:
event_subscriber
}
captcha.helper
:
class
:
Drupal\captcha\Service\CaptchaService
src/Form/CaptchaPointForm.php
View file @
8e9d85eb
...
...
@@ -2,6 +2,7 @@
namespace
Drupal\captcha\Form
;
use
Drupal\captcha\Service\CaptchaService
;
use
Drupal\Core\Entity\EntityForm
;
use
Drupal\Core\Form\FormStateInterface
;
use
Symfony\Component\DependencyInjection\ContainerInterface
;
...
...
@@ -12,6 +13,13 @@ use Symfony\Component\HttpFoundation\RequestStack;
*/
class
CaptchaPointForm
extends
EntityForm
{
/**
* The CAPTCHA helper service.
*
* @var \Drupal\captcha\Service\CaptchaService
*/
protected
$captchaService
;
/**
* The request stack.
*
...
...
@@ -25,8 +33,9 @@ class CaptchaPointForm extends EntityForm {
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* Constructor.
*/
public
function
__construct
(
RequestStack
$request_stack
)
{
public
function
__construct
(
RequestStack
$request_stack
,
CaptchaService
$captcha_service
)
{
$this
->
requestStack
=
$request_stack
;
$this
->
captchaService
=
$captcha_service
;
}
/**
...
...
@@ -39,7 +48,8 @@ class CaptchaPointForm extends EntityForm {
*/
public
static
function
create
(
ContainerInterface
$container
)
{
return
new
static
(
$container
->
get
(
'request_stack'
)
$container
->
get
(
'request_stack'
),
$container
->
get
(
'captcha.helper'
)
);
}
...
...
@@ -84,9 +94,8 @@ class CaptchaPointForm extends EntityForm {
'#type'
=>
'select'
,
'#title'
=>
$this
->
t
(
'Challenge type'
),
'#description'
=>
$this
->
t
(
'The CAPTCHA type to use for this form.'
),
'#default_value'
=>
(
$captcha_point
->
getCaptchaType
()
?:
$this
->
config
(
'captcha.settings'
)
->
get
(
'default_challenge'
)),
'#options'
=>
_captcha_available_challenge_types
(),
'#default_value'
=>
$captcha_point
->
getCaptchaType
()
?:
$this
->
config
(
'captcha.settings'
)
->
get
(
'default_challenge'
),
'#options'
=>
$this
->
captchaService
->
getAvailableChallengeTypes
(),
];
return
$form
;
...
...
src/Form/CaptchaSettingsForm.php
View file @
8e9d85eb
...
...
@@ -2,6 +2,7 @@
namespace
Drupal\captcha\Form
;
use
Drupal\captcha\Service\CaptchaService
;
use
Drupal\Core\Cache\CacheBackendInterface
;
use
Drupal\Core\Config\ConfigFactoryInterface
;
use
Drupal\Core\Extension\ModuleHandlerInterface
;
...
...
@@ -22,6 +23,13 @@ class CaptchaSettingsForm extends ConfigFormBase {
*/
protected
$cacheBackend
;
/**
* The CAPTCHA helper service.
*
* @var \Drupal\captcha\Service\CaptchaService
*/
protected
$captchaService
;
/**
* The module handler.
*
...
...
@@ -39,10 +47,11 @@ class CaptchaSettingsForm extends ConfigFormBase {
* @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
* 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
);
$this
->
cacheBackend
=
$cache_backend
;
$this
->
moduleHandler
=
$moduleHandler
;
$this
->
captchaService
=
$captcha_service
;
}
/**
...
...
@@ -52,7 +61,8 @@ class CaptchaSettingsForm extends ConfigFormBase {
return
new
static
(
$container
->
get
(
'config.factory'
),
$container
->
get
(
'cache.default'
),
$container
->
get
(
'module_handler'
)
$container
->
get
(
'module_handler'
),
$container
->
get
(
'captcha.helper'
)
);
}
...
...
@@ -90,7 +100,7 @@ class CaptchaSettingsForm extends ConfigFormBase {
'#type'
=>
'select'
,
'#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.'
),
'#options'
=>
_captcha_a
vailable
_c
hallenge
_t
ypes
(
FALSE
),
'#options'
=>
$this
->
captchaService
->
getA
vailable
C
hallenge
T
ypes
(
FALSE
),
'#default_value'
=>
$config
->
get
(
'default_challenge'
),
];
...
...
src/Service/CaptchaService.php
0 → 100644
View file @
8e9d85eb
<?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
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment