Adds a "spam protection" field to SilverStripe userforms using Cloudflare's Turnstile CAPTCHA service.
- SilverStripe 5.x
- SilverStripe Spam Protection 4.x
composer require silverstripe-terraformers/turnstile-captcha
After installing the module via composer or manual install you must set the spam protector to TurnstileCaptchaProtector, this needs to be set in your site's config file normally this is mysite/_config/config.yml.
SilverStripe\SpamProtection\Extension\FormSpamProtectionExtension:
default_spam_protector: Terraformers\TurnstileCaptcha\Forms\TurnstileCaptchaProtector
Finally, add the "spam protection" field to your form by calling
enableSpamProtection()
on the form object.
$form->enableSpamProtection();
Set the site_key
and the secret_key
via environment variables.
SS_TURNSTILE_SITE_KEY=""
SS_TURNSTILE_SECRET_KEY=""
You can get these from your cloudflare account refer to the turnstile documentation.
There are some optional configuration settings that can be added to your site's yaml config (typically this is mysite/_config/config.yml).
Terraformers\TurnstileCaptcha\Forms\TurnstileCaptchaField:
default_theme: "light" #Default theme color (optional, light or dark, defaults to auto)
default_render_type: 'explicit' #Default setting for how to render the widget. See the "Render Type" section below.
TurnstileCaptchaField uses Guzzle to communicate with cloudflare. If you would like to change http connection settings (Eg proxy settings) you can configure your own HttpClient class via injector
SilverStripe\Core\Injector\Injector:
Terraformers\TurnstileCaptcha\Http\HttpClient:
class: App\HttpClient
If you want to add a field label or help text to the TurnstileCaptchaField field you can do so like this:
$form->enableSpamProtection()
->fields()->fieldByName('TurnstileCaptchaField')
->setTitle("Spam protection")
->setDescription("Please tick the box to prove you're a human and help us stop spam.");
When your using the silverstripe/comments module you must add the following (per their documentation) to your _config.php in order to use Terraformers\TurnstileCaptcha on comment forms.
CommentingController::add_extension('CommentSpamProtection');
By default, the turnstyle widget will be rendered automatically. To change this you can set the render type.
This can be configured site-wide using the Config API
Terraformers\TurnstileCaptcha\Forms\TurnstileCaptchaField:
default_render_type: 'explicit'
Or on a per form basis:
$captchaField = $form->Fields()->fieldByName('TurnstileCaptchaField');
$captchaField->setRenderType('explicit');
With this configuration you will need to add your own javascript to render the widget. Refer to the cloudflare documentation for details.
When you're reporting an issue please ensure you specify what version of SilverStripe you are using i.e. 3.1.3, 3.2beta, master etc. Also be sure to include any JavaScript or PHP errors you receive, for PHP errors please ensure you include the full stack trace. Also please include how you produced the issue. You may also be asked to provide some of the classes to aid in re-producing the issue. Stick with the issue, remember that you seen the issue not the maintainer of the module so it may take allot of questions to arrive at a fix or answer.