You are here

class SimpleRecaptchaSettingsForm in Simple Google reCAPTCHA 8

Provides administration form for simple_recaptcha module.

Hierarchy

Expanded class hierarchy of SimpleRecaptchaSettingsForm

1 string reference to 'SimpleRecaptchaSettingsForm'
simple_recaptcha.routing.yml in ./simple_recaptcha.routing.yml
simple_recaptcha.routing.yml

File

src/Form/SimpleRecaptchaSettingsForm.php, line 11

Namespace

Drupal\simple_recaptcha\Form
View source
class SimpleRecaptchaSettingsForm extends ConfigFormBase {
  const SETTINGS = 'simple_recaptcha.config';

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'simple_recaptcha_settings';
  }

  /**
   * {@inheritdoc}
   */
  public function getEditableConfigNames() {
    return [
      static::SETTINGS,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config(static::SETTINGS);
    $form['recaptcha'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('General settings'),
      '#open' => TRUE,
    ];
    $form['recaptcha']['recaptcha_type'] = [
      '#type' => 'radios',
      '#title' => $this
        ->t('reCAPTCHA type'),
      '#options' => [
        'v2' => $this
          ->t('reCAPTCHA v2 (checkbox)'),
        'v3' => $this
          ->t('reCAPTCHA v3 (invisible)'),
      ],
      '#default_value' => $config
        ->get('recaptcha_type'),
    ];
    $form['recaptcha']['v3_score'] = [
      '#type' => 'number',
      '#title' => $this
        ->t('desired reCAPTCHA score'),
      '#max' => 100,
      '#min' => 1,
      '#default_value' => $config
        ->get('v3_score'),
      '#states' => [
        'visible' => [
          ':input[name="recaptcha_type"]' => [
            'value' => 'v3',
          ],
        ],
      ],
      '#description' => $this
        ->t('reCAPTCHA v3 returns a score (100 is very likely a good interaction, 0 is very likely a bot). Based on the score, you can decide when to block form submissions.'),
    ];
    $form['keys_v2'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('reCAPTCHA v2 checkbox'),
      '#open' => TRUE,
    ];
    $form['keys_v2']['site_key'] = [
      '#title' => $this
        ->t('Site key'),
      '#type' => 'textfield',
      '#default_value' => $config
        ->get('site_key'),
      '#description' => $this
        ->t('reCaptcha site key will be used in the HTML/JS code to render and handle reCaptcha widget.'),
    ];
    $form['keys_v2']['secret_key'] = [
      '#title' => $this
        ->t('Secret key'),
      '#type' => 'textfield',
      '#default_value' => $config
        ->get('secret_key'),
      '#description' => $this
        ->t('Secret key will be used internally to connect with reCaptcha API and verify responses.'),
    ];
    $form['keys_v3'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('reCAPTCHA v3 (invisible)'),
      '#open' => TRUE,
    ];
    $form['keys_v3']['site_key_v3'] = [
      '#title' => $this
        ->t('Site key'),
      '#type' => 'textfield',
      '#default_value' => $config
        ->get('site_key_v3'),
      '#description' => $this
        ->t('reCaptcha site key will be used in the HTML/JS code to render and handle reCaptcha widget.'),
    ];
    $form['keys_v3']['secret_key_v3'] = [
      '#title' => $this
        ->t('Secret key'),
      '#type' => 'textfield',
      '#default_value' => $config
        ->get('secret_key_v3'),
      '#description' => $this
        ->t('Secret key will be used internally to connect with reCaptcha API and verify responses.'),
    ];
    $form['form_ids'] = [
      '#type' => 'textarea',
      '#description' => $this
        ->t('Add comma separated list of form ids, e.g.: user_login_form,user_pass,user_register_form.<br />Support wildcard eg: user_* to apply on all user forms.'),
      '#title' => $this
        ->t('Form IDs'),
      '#default_value' => $config
        ->get('form_ids'),
    ];
    $form['recaptcha_use_globally'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('use reCAPTCHA globally'),
      '#default_value' => $config
        ->get('recaptcha_use_globally'),
      '#description' => $this
        ->t('For countries where google.com domain isn\'t allowed - use recaptcha.net to load api.js file (See <a href=":url" target="_blank">documentation</a>).', [
        ':url' => 'https://developers.google.com/recaptcha/docs/faq#can-i-use-recaptcha-globally',
      ]),
    ];
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->configFactory
      ->getEditable(static::SETTINGS)
      ->set('form_ids', $form_state
      ->getValue('form_ids'))
      ->set('site_key', $form_state
      ->getValue('site_key'))
      ->set('secret_key', $form_state
      ->getValue('secret_key'))
      ->set('site_key_v3', $form_state
      ->getValue('site_key_v3'))
      ->set('secret_key_v3', $form_state
      ->getValue('secret_key_v3'))
      ->set('recaptcha_type', $form_state
      ->getValue('recaptcha_type'))
      ->set('v3_score', $form_state
      ->getValue('v3_score'))
      ->set('recaptcha_use_globally', $form_state
      ->getValue('recaptcha_use_globally'))
      ->save();
    parent::submitForm($form, $form_state);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBase::create public static function Instantiates a new instance of this class. Overrides FormBase::create 13
ConfigFormBase::__construct public function Constructs a \Drupal\system\ConfigFormBase object. 11
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
SimpleRecaptchaSettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
SimpleRecaptchaSettingsForm::getEditableConfigNames public function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
SimpleRecaptchaSettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
SimpleRecaptchaSettingsForm::SETTINGS constant
SimpleRecaptchaSettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.