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
b7a09f88
Commit
b7a09f88
authored
Oct 12, 2019
by
Fabiano Sant'Ana
Browse files
Issue #3087618 by wundo: Move _captcha_insert_captcha_element to a service
parent
7adc7dcd
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
80 additions
and
77 deletions
+80
-77
captcha.inc
captcha.inc
+0
-73
captcha.module
captcha.module
+6
-4
src/Service/CaptchaService.php
src/Service/CaptchaService.php
+74
-0
No files found.
captcha.inc
View file @
b7a09f88
...
...
@@ -302,76 +302,3 @@ function _captcha_search_buttons(array $form) {
return
$buttons
;
}
/**
* Helper function to insert a CAPTCHA element before a given form element.
*
* @param array $form
* the form to add the CAPTCHA element to.
* @param array $placement
* information where the CAPTCHA element should be inserted.
* $placement should be an associative array with fields:
* - 'path': path (array of path items) of the container in
* the form where the CAPTCHA element should be inserted.
* - 'key': the key of the element before which the CAPTCHA element
* should be inserted. If the field 'key' is undefined or NULL,
* the CAPTCHA will just be appended in the container.
* - 'weight': if 'key' is not NULL: should be the weight of the
* element defined by 'key'. If 'key' is NULL and weight is not NULL:
* set the weight property of the CAPTCHA element to this value.
* @param array $captcha_element
* the CAPTCHA element to insert.
*/
function
_captcha_insert_captcha_element
(
array
&
$form
,
array
$placement
,
array
$captcha_element
)
{
// Get path, target and target weight or use defaults if not available.
$target_key
=
isset
(
$placement
[
'key'
])
?
$placement
[
'key'
]
:
NULL
;
$target_weight
=
isset
(
$placement
[
'weight'
])
?
$placement
[
'weight'
]
:
NULL
;
$path
=
isset
(
$placement
[
'path'
])
?
$placement
[
'path'
]
:
[];
// Walk through the form along the path.
$form_stepper
=
&
$form
;
foreach
(
$path
as
$step
)
{
if
(
isset
(
$form_stepper
[
$step
]))
{
$form_stepper
=
&
$form_stepper
[
$step
];
}
else
{
// Given path is invalid: stop stepping and
// continue in best effort (append instead of insert).
$target_key
=
NULL
;
break
;
}
}
// If no target is available: just append the CAPTCHA element
// to the container.
if
(
$target_key
==
NULL
||
!
array_key_exists
(
$target_key
,
$form_stepper
))
{
// Optionally, set weight of CAPTCHA element.
if
(
$target_weight
!=
NULL
)
{
$captcha_element
[
'#weight'
]
=
$target_weight
;
}
$form_stepper
[
'captcha'
]
=
$captcha_element
;
}
// If there is a target available: make sure the CAPTCHA element
// comes right before it.
else
{
// If target has a weight: set weight of CAPTCHA element a bit smaller
// and just append the CAPTCHA: sorting will fix the ordering anyway.
if
(
$target_weight
!=
NULL
)
{
$captcha_element
[
'#weight'
]
=
$target_weight
-
.1
;
$form_stepper
[
'captcha'
]
=
$captcha_element
;
}
else
{
// If we can't play with weights: insert the CAPTCHA element
// at the right position. Because PHP lacks a function for
// this (array_splice() comes close, but it does not preserve
// the key of the inserted element), we do it by hand: chop of
// the end, append the CAPTCHA element and put the end back.
$offset
=
array_search
(
$target_key
,
array_keys
(
$form_stepper
));
$end
=
array_splice
(
$form_stepper
,
$offset
);
$form_stepper
[
'captcha'
]
=
$captcha_element
;
foreach
(
$end
as
$k
=>
$v
)
{
$form_stepper
[
$k
]
=
$v
;
}
}
}
}
captcha.module
View file @
b7a09f88
...
...
@@ -11,13 +11,13 @@
use
Drupal\captcha\Entity\CaptchaPoint
;
use
Drupal\Core\Database\Database
;
use
Drupal\Core\Form\BaseFormIdInterface
;
use
Drupal\Core\Form\FormStateInterface
;
use
Drupal\Core\Link
;
use
Drupal\Core\Render\Element
;
use
Drupal\Core\Render\Markup
;
use
Drupal\Core\Routing\RouteMatchInterface
;
use
Drupal\Core\Url
;
use
Drupal\Core\Render\Element
;
use
Drupal\Core\Form\BaseFormIdInterface
;
/**
* Constants for CAPTCHA persistence.
...
...
@@ -160,6 +160,8 @@ function template_preprocess_captcha(&$variables) {
function
captcha_form_alter
(
array
&
$form
,
FormStateInterface
$form_state
,
$form_id
)
{
$account
=
\
Drupal
::
currentUser
();
$config
=
\
Drupal
::
config
(
'captcha.settings'
);
$captchaService
=
\
Drupal
::
service
(
'captcha.helper'
);
// Visitor does not have permission to skip CAPTCHAs.
module_load_include
(
'inc'
,
'captcha'
);
if
(
!
$account
->
hasPermission
(
'skip CAPTCHA'
))
{
...
...
@@ -218,7 +220,7 @@ function captcha_form_alter(array &$form, FormStateInterface $form_state, $form_
// Get placement in form and insert in form.
$captcha_placement
=
_captcha_get_captcha_placement
(
$form_id
,
$form
);
_
captcha
_
insert
_c
aptcha
_e
lement
(
$form
,
$captcha_placement
,
$captcha_element
);
$
captcha
Service
->
insert
C
aptcha
E
lement
(
$form
,
$captcha_placement
,
$captcha_element
);
}
}
...
...
@@ -273,7 +275,7 @@ function captcha_form_alter(array &$form, FormStateInterface $form_state, $form_
// Get placement in form and insert in form.
if
(
$captcha_placement
=
_captcha_get_captcha_placement
(
$form_id
,
$form
))
{
_
captcha
_
insert
_c
aptcha
_e
lement
(
$form
,
$captcha_placement
,
$captcha_element
);
$
captcha
Service
->
insert
C
aptcha
E
lement
(
$form
,
$captcha_placement
,
$captcha_element
);
};
}
...
...
src/Service/CaptchaService.php
View file @
b7a09f88
...
...
@@ -43,4 +43,78 @@ class CaptchaService {
return
$challenges
;
}
/**
* Helper function to insert a CAPTCHA element before a given form element.
*
* @param array $form
* the form to add the CAPTCHA element to.
* @param array $placement
* information where the CAPTCHA element should be inserted.
* $placement should be an associative array with fields:
* - 'path': path (array of path items) of the container in
* the form where the CAPTCHA element should be inserted.
* - 'key': the key of the element before which the CAPTCHA element
* should be inserted. If the field 'key' is undefined or NULL,
* the CAPTCHA will just be appended in the container.
* - 'weight': if 'key' is not NULL: should be the weight of the
* element defined by 'key'. If 'key' is NULL and weight is not NULL:
* set the weight property of the CAPTCHA element to this value.
* @param array $captcha_element
* the CAPTCHA element to insert.
*/
function
insertCaptchaElement
(
array
&
$form
,
array
$placement
,
array
$captcha_element
)
{
// Get path, target and target weight or use defaults if not available.
$target_key
=
isset
(
$placement
[
'key'
])
?
$placement
[
'key'
]
:
NULL
;
$target_weight
=
isset
(
$placement
[
'weight'
])
?
$placement
[
'weight'
]
:
NULL
;
$path
=
isset
(
$placement
[
'path'
])
?
$placement
[
'path'
]
:
[];
// Walk through the form along the path.
$form_stepper
=
&
$form
;
foreach
(
$path
as
$step
)
{
if
(
isset
(
$form_stepper
[
$step
]))
{
$form_stepper
=
&
$form_stepper
[
$step
];
}
else
{
// Given path is invalid: stop stepping and
// continue in best effort (append instead of insert).
$target_key
=
NULL
;
break
;
}
}
// If no target is available: just append the CAPTCHA element
// to the container.
if
(
$target_key
==
NULL
||
!
array_key_exists
(
$target_key
,
$form_stepper
))
{
// Optionally, set weight of CAPTCHA element.
if
(
$target_weight
!=
NULL
)
{
$captcha_element
[
'#weight'
]
=
$target_weight
;
}
$form_stepper
[
'captcha'
]
=
$captcha_element
;
}
// If there is a target available: make sure the CAPTCHA element
// comes right before it.
else
{
// If target has a weight: set weight of CAPTCHA element a bit smaller
// and just append the CAPTCHA: sorting will fix the ordering anyway.
if
(
$target_weight
!=
NULL
)
{
$captcha_element
[
'#weight'
]
=
$target_weight
-
.1
;
$form_stepper
[
'captcha'
]
=
$captcha_element
;
}
else
{
// If we can't play with weights: insert the CAPTCHA element
// at the right position. Because PHP lacks a function for
// this (array_splice() comes close, but it does not preserve
// the key of the inserted element), we do it by hand: chop of
// the end, append the CAPTCHA element and put the end back.
$offset
=
array_search
(
$target_key
,
array_keys
(
$form_stepper
));
$end
=
array_splice
(
$form_stepper
,
$offset
);
$form_stepper
[
'captcha'
]
=
$captcha_element
;
foreach
(
$end
as
$k
=>
$v
)
{
$form_stepper
[
$k
]
=
$v
;
}
}
}
}
}
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