class ReCaptchaV3SettingsForm in reCAPTCHA v3 8
Configure the google reCAPTCHA v3 api and fallback challenge.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
- class \Drupal\recaptcha_v3\Form\ReCaptchaV3SettingsForm
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of ReCaptchaV3SettingsForm
1 string reference to 'ReCaptchaV3SettingsForm'
File
- src/
Form/ ReCaptchaV3SettingsForm.php, line 16
Namespace
Drupal\recaptcha_v3\FormView source
class ReCaptchaV3SettingsForm extends ConfigFormBase {
/**
* The library discovery service.
*
* @var \Drupal\Core\Asset\LibraryDiscoveryInterface
*/
protected $libraryDiscovery;
/**
* The element info manager.
*
* @var \Drupal\Core\Render\ElementInfoManager
*/
protected $elementInfoManager;
/**
* The CAPTCHA helper service.
*
* @var \Drupal\captcha\Service\CaptchaService
*/
protected $captchaService;
/**
* ReCaptchaV3SettingsForm constructor.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* Config factory service.
* @param \Drupal\Core\Asset\LibraryDiscoveryInterface $library_discovery
* Library discovery service.
* @param \Drupal\Core\Render\ElementInfoManager $element_info_manager
* Element info manager service.
* @param \Drupal\captcha\Service\CaptchaService $captcha_service
* Captcha service.
*/
public function __construct(ConfigFactoryInterface $config_factory, LibraryDiscoveryInterface $library_discovery, ElementInfoManager $element_info_manager, CaptchaService $captcha_service) {
parent::__construct($config_factory);
$this->libraryDiscovery = $library_discovery;
$this->elementInfoManager = $element_info_manager;
$this->captchaService = $captcha_service;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('library.discovery'), $container
->get('plugin.manager.element_info'), $container
->get('captcha.helper'));
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'recaptcha_v3.settings',
];
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'recaptcha_v3_settings';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('recaptcha_v3.settings');
$form['site_key'] = [
'#type' => 'textfield',
'#title' => $this
->t('Site key'),
'#default_value' => $config
->get('site_key'),
'#maxlength' => 40,
'#description' => $this
->t('The site key given to you when you <a href="@url">register for reCAPTCHA</a>.', [
'@url' => 'https://www.google.com/recaptcha/admin',
]),
'#required' => TRUE,
];
$form['secret_key'] = [
'#type' => 'textfield',
'#title' => $this
->t('Secret key'),
'#default_value' => $config
->get('secret_key'),
'#maxlength' => 40,
'#description' => $this
->t('The secret key given to you when you <a href="@url">register for reCAPTCHA</a>.', [
'@url' => 'https://www.google.com/recaptcha/admin',
]),
'#required' => TRUE,
];
$form['verify_hostname'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Local domain name validation'),
'#default_value' => $config
->get('verify_hostname'),
'#description' => $this
->t('Checks the hostname on your server when verifying a solution. Enable this validation only, if <em>Verify the origin of reCAPTCHA solutions</em> is unchecked for your key pair. Provides crucial security by verifying requests come from one of your listed domains.'),
];
$challenges = $this->captchaService
->getAvailableChallengeTypes(FALSE);
// Remove recaptcha v3 challenges from the list of available
// fallback challenges.
$challenges = array_filter($challenges, static function ($captcha_type) {
return !(strpos($captcha_type, 'recaptcha_v3') === 0);
}, ARRAY_FILTER_USE_KEY);
$form['default_challenge'] = [
'#type' => 'select',
'#title' => $this
->t('Default fallback challenge type'),
'#description' => $this
->t('Select the default fallback challenge type on verification fail.'),
'#options' => $challenges,
'#default_value' => $config
->get('default_challenge'),
'#empty_option' => $this
->t('- None -'),
'#empty_value' => '',
];
$form['error_message'] = [
'#type' => 'textfield',
'#size' => 128,
'#title' => $this
->t('Error message'),
'#description' => $this
->t('This message will be displayed to user in case of failed recaptcha v3 verification.'),
'#default_value' => $config
->get('error_message'),
];
$form['cacheable'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Cacheable'),
'#description' => $this
->t('Make captcha cacheble: can lead to some validation errors like "unknown CAPTCHA session ID".'),
'#default_value' => $config
->get('cacheable'),
];
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValues();
$config = $this
->config('recaptcha_v3.settings');
// If site key have been changed,
// then need to rebuild site libraries and elements.
if ($config
->get('site_key') !== $values['site_key']) {
$this->libraryDiscovery
->clearCachedDefinitions();
$this->elementInfoManager
->clearCachedDefinitions();
}
$this
->config('recaptcha_v3.settings')
->set('site_key', $values['site_key'])
->set('secret_key', $values['secret_key'])
->set('verify_hostname', $values['verify_hostname'])
->set('default_challenge', $values['default_challenge'])
->set('error_message', $values['error_message'])
->set('cacheable', $values['cacheable'])
->save();
parent::submitForm($form, $form_state);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigFormBaseTrait:: |
protected | function | Retrieves a configuration object. | |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
ReCaptchaV3SettingsForm:: |
protected | property | The CAPTCHA helper service. | |
ReCaptchaV3SettingsForm:: |
protected | property | The element info manager. | |
ReCaptchaV3SettingsForm:: |
protected | property | The library discovery service. | |
ReCaptchaV3SettingsForm:: |
public | function |
Form constructor. Overrides ConfigFormBase:: |
|
ReCaptchaV3SettingsForm:: |
public static | function |
Instantiates a new instance of this class. Overrides ConfigFormBase:: |
|
ReCaptchaV3SettingsForm:: |
protected | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
ReCaptchaV3SettingsForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
ReCaptchaV3SettingsForm:: |
public | function |
Form submission handler. Overrides ConfigFormBase:: |
|
ReCaptchaV3SettingsForm:: |
public | function |
ReCaptchaV3SettingsForm constructor. Overrides ConfigFormBase:: |
|
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |