class ShurlySettingsForm in ShURLy 8
ShurlySettingsForm.
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\shurly\Form\ShurlySettingsForm
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of ShurlySettingsForm
1 string reference to 'ShurlySettingsForm'
File
- src/
Form/ ShurlySettingsForm.php, line 13
Namespace
Drupal\shurly\FormView source
class ShurlySettingsForm extends ConfigFormBase {
/**
* {@inheritdoc}
*/
public function __construct(ConfigFactoryInterface $config_factory) {
parent::__construct($config_factory);
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'shurly.settings',
];
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'shurly_settings_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('shurly.settings');
$form['shurly_url'] = [
'#type' => 'fieldset',
'#title' => t('Base URL'),
'#description' => t('If you want to use a dedicated url for the short URL\'s, enter above that short base URL to be used.'),
];
$form['shurly_url']['shurly_base'] = [
'#type' => 'textfield',
'#description' => t('Leave empty to use the default base URL of the Drupal installation'),
'#default_value' => $config
->get('shurly_base'),
'#required' => FALSE,
];
$form['shurly_redirect'] = [
'#type' => 'fieldset',
'#title' => t('Redirect URL'),
'#description' => t('Define the redirect page when the short link is deactivated.'),
];
$form['shurly_redirect']['shurly_redirect_page'] = [
'#type' => 'textfield',
'#field_prefix' => _shurly_get_shurly_base() . '/',
'#description' => t('Page displayed when the link is deactivated. If not defined, the default 404 page will be used.'),
'#default_value' => $config
->get('shurly_redirect_page'),
];
$form['shurly_restrictions'] = [
'#type' => 'fieldset',
'#title' => t('Restrictions'),
'#description' => t('Restrict short URL targets. Be aware of the fact, that the localhost, local network and unresolvable restriction are resolving the given address by staging a DNS request, which can significantly <strong>slow down the short URL creation</strong>!'),
];
$form['shurly_restrictions']['shurly_forbid_localhost'] = [
'#type' => 'checkbox',
'#title' => t('Forbid localhost'),
'#description' => t('Do not allow creation of short URLs targeting localhost addresses.'),
'#default_value' => $config
->get('shurly_forbid_localhost'),
];
$form['shurly_restrictions']['shurly_forbid_private_ips'] = [
'#type' => 'checkbox',
'#title' => t('Forbid private IP ranges'),
'#description' => t('Do not allow creation of short URLs targeting private IP ranges.'),
'#default_value' => $config
->get('shurly_forbid_private_ips'),
];
$form['shurly_restrictions']['shurly_forbid_unresolvable_hosts'] = [
'#type' => 'checkbox',
'#title' => t('Forbid unresolvable hostnames'),
'#description' => t('Do not allow creation of short URLs targeting host addresses that cannot be resolved.'),
'#default_value' => $config
->get('shurly_forbid_unresolvable_hosts'),
];
$form['shurly_restrictions']['shurly_forbid_ips'] = [
'#type' => 'checkbox',
'#title' => t('Forbid direct IP redirects'),
'#description' => t('Do not allow creation of short URLs containing an IP address instead of a human readable hostname.'),
'#default_value' => $config
->get('shurly_forbid_ips'),
];
$form['shurly_restrictions']['shurly_forbid_custom'] = [
'#type' => 'checkbox',
'#title' => t('Forbid URL target by custom pattern'),
'#description' => t('Define a custom pattern (RegEx) to forbid some kind of target URLs.'),
'#default_value' => $config
->get('shurly_forbid_custom'),
'#attributes' => [
'onchange' => "jQuery('#shurly_custom_restriction_container').toggle();",
],
];
$form['shurly_restrictions']['shurly_custom_restriction'] = [
'#type' => 'textfield',
'#title' => t('Custom pattern'),
'#description' => t('PERL regular expression defining a forbidden URL pattern.'),
'#default_value' => $config
->get('shurly_custom_restriction'),
];
$form['shurly_restrictions']['shurly_gsb'] = [
'#type' => 'checkbox',
'#title' => t('Google Safe Browsing'),
'#description' => t('Check if a long URL is blacklisted against Google Safe Browsing. This service
requires a Google developer account and is limited to 10,000 queries per day.'),
'#default_value' => $config
->get('shurly_gsb'),
'#attributes' => [
'onchange' => "jQuery('.shurly_gsb_container').toggle();",
],
];
$form['shurly_restrictions']['shurly_gsb_client'] = [
'#type' => 'textfield',
'#title' => t('Client'),
'#description' => t('You can choose any name. Google suggests that you choose a name that represents the true identiy
of the client (ie: name of your company).'),
'#default_value' => $config
->get('shurly_gsb_client'),
];
$form['shurly_restrictions']['shurly_gsb_apikey'] = [
'#type' => 'textfield',
'#title' => t('API Key'),
'#description' => t('Add your API key.'),
'#default_value' => $config
->get('shurly_gsb_apikey'),
];
$form['shurly_throttle'] = [
'#type' => 'fieldset',
'#title' => t('Rate limiting'),
'#tree' => TRUE,
'#description' => t('Limit requests by IP address. Leave blank for no rate limiting.<br /><strong>Note:</strong> Only roles with the \'Create short URLs\' permission are listed here.'),
];
$saved = $config
->get('shurly_throttle');
/**
* @var string $rid
* @var \Drupal\user\Entity\Role $role
*/
foreach (user_roles(FALSE, 'Create short URLs') as $rid => $role) {
$rate = isset($saved[$rid]['rate']) ? $saved[$rid]['rate'] : NULL;
$time = isset($saved[$rid]['time']) ? $saved[$rid]['time'] : NULL;
$form['shurly_throttle'][$rid] = [
'#type' => 'fieldset',
'#title' => $role
->label(),
'#tree' => TRUE,
];
$form['shurly_throttle'][$rid]['rate'] = [
'#type' => 'textfield',
'#size' => '3',
'#prefix' => '<div class="container-inline">',
'#field_suffix' => ' ' . t('requests'),
'#default_value' => $rate,
];
$form['shurly_throttle'][$rid]['time'] = [
'#type' => 'textfield',
'#size' => '3',
'#field_prefix' => t('within'),
'#field_suffix' => ' ' . t('minutes'),
'#default_value' => $time,
'#suffix' => '</div>',
];
$form['shurly_throttle'][$rid]['weight'] = [
'#type' => 'weight',
'#title' => t('Weight'),
'#default_value' => isset($saved[$rid]['weight']) ? $saved[$rid]['weight'] : 0,
'#description' => t('Order of this role when considering a user with multiple roles. A user\'s lightest role will take precedence.'),
];
}
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$custom_base_url = trim($form_state
->getValue('shurly_base'));
// We need to deal with an empty value here. But then wie don't need to validate the url.
if (!empty($custom_base_url) && !UrlHelper::isValid($custom_base_url, $absolute = TRUE)) {
$form_state
->setErrorByName('shurly_base', t('The base URL is not valid.'));
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this
->config('shurly.settings');
$form_state
->cleanValues();
foreach ($form_state
->getValues() as $key => $value) {
if (!empty($value) && !is_array($value)) {
$config
->set($key, $value);
}
}
$config
->save();
parent::submitForm($form, $form_state);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigFormBase:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
13 |
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. | |
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. | |
ShurlySettingsForm:: |
public | function |
Form constructor. Overrides ConfigFormBase:: |
|
ShurlySettingsForm:: |
protected | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
ShurlySettingsForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
ShurlySettingsForm:: |
public | function |
Form submission handler. Overrides ConfigFormBase:: |
|
ShurlySettingsForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
ShurlySettingsForm:: |
public | function |
Constructs a \Drupal\system\ConfigFormBase object. Overrides ConfigFormBase:: |
|
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. |