class RateSettingsForm in Rate 8.2
Same name and namespace in other branches
- 8 src/Form/RateSettingsForm.php \Drupal\rate\Form\RateSettingsForm
Configure rate settings for the site.
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\rate\Form\RateSettingsForm implements ContainerInjectionInterface
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of RateSettingsForm
1 file declares its use of RateSettingsForm
- RateWidgetSettingsTest.php in tests/
src/ Functional/ RateWidgetSettingsTest.php
1 string reference to 'RateSettingsForm'
File
- src/
Form/ RateSettingsForm.php, line 17
Namespace
Drupal\rate\FormView source
class RateSettingsForm extends ConfigFormBase implements ContainerInjectionInterface {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'rate_settings_form';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'rate.settings',
];
}
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The Http Client object.
*
* @var \GuzzleHttp\Client
*/
protected $httpClient;
/**
* RateSettingsForm constructor.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \GuzzleHttp\Client $http_client
* Http client object.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, Client $http_client) {
$this->entityTypeManager = $entity_type_manager;
$this->httpClient = $http_client;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('http_client'));
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('rate.settings');
$form['settings'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Rate settings'),
'#collapsbile' => FALSE,
'#collapsed' => FALSE,
];
$form['settings']['disable_log'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Disable log messages'),
'#default_value' => $config
->get('disable_log'),
'#description' => $this
->t('This will disable log messages when voting in rate module.'),
];
$form['settings']['disable_fontawesome'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Disable fontawesome'),
'#default_value' => $config
->get('disable_fontawesome'),
'#description' => $this
->t('This will disable fontawesome library from loading with rate module.'),
];
$form['bot'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Bot detection'),
'#description' => $this
->t('Bots can be automatically banned from voting if they rate more than a given amount of votes within one minute or hour. This threshold is configurable below. Votes from the same IP-address will be ignored forever after reaching this limit.'),
'#collapsbile' => FALSE,
'#collapsed' => FALSE,
];
$threshold_options = array_combine([
0,
10,
25,
50,
100,
250,
500,
1000,
], [
0,
10,
25,
50,
100,
250,
500,
1000,
]);
$threshold_options[0] = $this
->t('disable');
$form['bot']['bot_minute_threshold'] = [
'#type' => 'select',
'#title' => $this
->t('1 minute threshold'),
'#options' => $threshold_options,
'#default_value' => $config
->get('bot_minute_threshold'),
];
$form['bot']['bot_hour_threshold'] = [
'#type' => 'select',
'#title' => $this
->t('1 hour threshold'),
'#options' => $threshold_options,
'#default_value' => $config
->get('bot_hour_threshold'),
];
$form['bot']['botscout_key'] = [
'#type' => 'textfield',
'#title' => $this
->t('BotScout.com API key'),
'#default_value' => $config
->get('botscout_key'),
'#description' => $this
->t('Rate will check the voters IP against the BotScout database if it has an API key. You can request a key at %url.', [
'%url' => 'http://botscout.com/getkey.htm',
]),
];
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$messenger = $this
->messenger();
if ($form_state
->getValue([
'botscout_key',
])) {
$uri = "http://botscout.com/test/?ip=84.16.230.111&key=" . $form_state
->getValue([
'botscout_key',
]);
try {
$response = $this->httpClient
->get($uri, [
'headers' => [
'Accept' => 'text/plain',
],
]);
$data = (string) $response
->getBody();
$status_code = $response
->getStatusCode();
if (empty($data)) {
$messenger
->addWarning($this
->t('An empty response was returned from botscout.'));
}
elseif ($status_code == 200) {
if (in_array(substr($data, 0, 1), [
'Y',
'N',
], TRUE)) {
$messenger
->addStatus($this
->t('Rate has succesfully contacted the BotScout server.'));
}
else {
$form_state
->setErrorByName('botscout_key', $this
->t('Invalid API-key.'));
}
}
else {
$messenger
->addWarning($this
->t('Rate was unable to contact the BotScout server.'));
}
} catch (RequestException $e) {
$messenger
->addWarning($this
->t('An error occurred contacting BotScout.'));
watchdog_exception('rate', $e);
}
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this
->config('rate.settings');
if ((bool) $config
->get('disable_log') !== (bool) $form_state
->getValue('disable_log')) {
$config
->set('disable_log', $form_state
->getValue('disable_log'))
->save();
}
if ((bool) $config
->get('disable_fontawesome') !== (bool) $form_state
->getValue('disable_fontawesome')) {
$config
->set('disable_fontawesome', $form_state
->getValue('disable_fontawesome'))
->save();
Cache::invalidateTags([
'library_info',
]);
}
$config
->set('bot_minute_threshold', $form_state
->getValue('bot_minute_threshold'))
->set('bot_hour_threshold', $form_state
->getValue('bot_hour_threshold'))
->set('botscout_key', $form_state
->getValue('botscout_key'))
->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. | |
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. | |
RateSettingsForm:: |
protected | property | The entity type manager. | |
RateSettingsForm:: |
protected | property | The Http Client object. | |
RateSettingsForm:: |
public | function |
Form constructor. Overrides ConfigFormBase:: |
|
RateSettingsForm:: |
public static | function |
Instantiates a new instance of this class. Overrides ConfigFormBase:: |
|
RateSettingsForm:: |
protected | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
RateSettingsForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
RateSettingsForm:: |
public | function |
Form submission handler. Overrides ConfigFormBase:: |
|
RateSettingsForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
RateSettingsForm:: |
public | function |
RateSettingsForm 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. |