Silverstripe CMS Turnstile Captcha Spam Protection Field
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.
site_key and the
secret_key via environment variables.
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.
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');