diff --git a/README.md b/README.md
index f3db8dc..2d5a0a4 100755
--- a/README.md
+++ b/README.md
@@ -182,6 +182,16 @@ ewz_recaptcha:
score_threshold: 0.6
```
+To disable loading recaptcha script from external source (most often for GDPR)
+``` yaml
+# app/config/config.yml
+
+ewz_recaptcha:
+ // ...
+ external_recaptcha_assets: true
+ external_recaptcha_assets_missing_message: ~
+```
+
Congratulations! You're ready!
## Basic Usage
diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php
index f5ecb03..ed6c4ab 100755
--- a/src/DependencyInjection/Configuration.php
+++ b/src/DependencyInjection/Configuration.php
@@ -42,6 +42,8 @@ public function getConfigTreeBuilder(): TreeBuilder
->integerNode('timeout')->min(0)->defaultNull()->end()
->arrayNode('trusted_roles')->prototype('scalar')->treatNullLike(array())->end()
+ ->booleanNode('external_recaptcha_assets')->defaultTrue()->end()
+ ->scalarNode('external_recaptcha_assets_missing_message')->defaultNull()->end()
->end()
;
diff --git a/src/Form/Type/EWZRecaptchaV3Type.php b/src/Form/Type/EWZRecaptchaV3Type.php
index 989a728..aecf5e4 100755
--- a/src/Form/Type/EWZRecaptchaV3Type.php
+++ b/src/Form/Type/EWZRecaptchaV3Type.php
@@ -13,6 +13,11 @@ class EWZRecaptchaV3Type extends AbstractEWZRecaptchaType
/** @var bool */
private $hideBadge;
+ /** @var bool */
+ private $externalRecaptcha;
+
+ /** @var string|null */
+ private $externalRecaptchaMissingMessage;
/**
* EWZRecaptchaV3Type constructor.
@@ -20,13 +25,16 @@ class EWZRecaptchaV3Type extends AbstractEWZRecaptchaType
* @param string $publicKey
* @param bool $enabled
* @param bool $hideBadge
+ * @param bool $externalRecaptcha
* @param string $apiHost
*/
- public function __construct(string $publicKey, bool $enabled, bool $hideBadge, string $apiHost = 'www.google.com')
+ public function __construct(string $publicKey, bool $enabled, bool $hideBadge, bool $externalRecaptcha, ?string $externalRecaptchaMissingMessage, string $apiHost = 'www.google.com')
{
parent::__construct($publicKey, $enabled, $apiHost);
$this->hideBadge = $hideBadge;
+ $this->externalRecaptcha = $externalRecaptcha;
+ $this->externalRecaptchaMissingMessage = $externalRecaptchaMissingMessage;
}
/**
@@ -61,6 +69,8 @@ protected function addCustomVars(FormView $view, FormInterface $form, array $opt
{
$view->vars = array_replace($view->vars, [
'ewz_recaptcha_hide_badge' => $this->hideBadge,
+ 'ewz_external_recaptcha_assets' => $this->externalRecaptcha,
+ 'external_recaptcha_assets_missing_message' => $this->externalRecaptchaMissingMessage,
'script_nonce_csp' => $options['script_nonce_csp'] ?? '',
'action_name' => $options['action_name'] ?? '',
]);
diff --git a/src/Resources/config/services.yml b/src/Resources/config/services.yml
index abb86a6..f56301c 100755
--- a/src/Resources/config/services.yml
+++ b/src/Resources/config/services.yml
@@ -62,6 +62,7 @@ services:
- '%ewz_recaptcha.public_key%'
- '%ewz_recaptcha.enabled%'
- '%ewz_recaptcha.hide_badge%'
+ - '%ewz_recaptcha.external_recaptcha_assets%'
- '%ewz_recaptcha.api_host%'
tags:
- { name: form.type }
diff --git a/src/Resources/views/Form/v3/ewz_recaptcha_widget.html.twig b/src/Resources/views/Form/v3/ewz_recaptcha_widget.html.twig
index 5adcfc2..d6da387 100644
--- a/src/Resources/views/Form/v3/ewz_recaptcha_widget.html.twig
+++ b/src/Resources/views/Form/v3/ewz_recaptcha_widget.html.twig
@@ -1,7 +1,9 @@
{% block ewz_recaptcha_widget %}
{% apply spaceless %}
{% if form.vars.ewz_recaptcha_enabled %}
-
+ {% if form.vars.ewz_external_recaptcha_assets %}
+
+ {% endif %}
{% if form.vars.ewz_recaptcha_hide_badge %}
@@ -16,6 +18,18 @@
grecaptchaForm.addEventListener('submit', function (e) {
e.preventDefault();
+ {% if form.vars.ewz_external_recaptcha_assets %}
+ if (!Object.hasOwn(window, "grecaptcha")) {
+ console.error('Recaptcha not found');
+
+ {% if form.vars.external_recaptcha_assets_missing_message is not null %}
+ alert({{ form.vars.external_recaptcha_assets_missing_message|trans }});
+
+ {% endif %}
+ return;
+ }
+ {% endif %}
+
grecaptcha.ready(function () {
grecaptcha.execute('{{ form.vars.public_key }}', { action: '{{ form.vars.action_name|default(constant('EWZ\\Bundle\\RecaptchaBundle\\Form\\Type\\EWZRecaptchaV3Type::DEFAULT_ACTION_NAME')) }}' }).then(function (token) {
grecaptchaInput.value = token;
diff --git a/tests/Form/Type/EWZRecaptchaV3TypeTest.php b/tests/Form/Type/EWZRecaptchaV3TypeTest.php
index 20ac500..e6d8283 100755
--- a/tests/Form/Type/EWZRecaptchaV3TypeTest.php
+++ b/tests/Form/Type/EWZRecaptchaV3TypeTest.php
@@ -19,7 +19,7 @@ class EWZRecaptchaV3TypeTest extends TestCase
protected function setUp(): void
{
$requestStack = $this->createMock(RequestStack::class);
- $this->type = new EWZRecaptchaV3Type('key', true, true, 'www.google.com');
+ $this->type = new EWZRecaptchaV3Type('key', true, true, true, null, 'www.google.com');
}
/**