class Domain301RedirectConfigForm in Domain 301 Redirect 8
Provides a settings for domain_301_redirect module.
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\domain_301_redirect\Form\Domain301RedirectConfigForm
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of Domain301RedirectConfigForm
1 string reference to 'Domain301RedirectConfigForm'
File
- src/
Form/ Domain301RedirectConfigForm.php, line 18
Namespace
Drupal\domain_301_redirect\FormView source
class Domain301RedirectConfigForm extends ConfigFormBase {
/**
* Domain 301 Redirect Manager.
*
* @var \Drupal\domain_301_redirect\Domain301RedirectManager
*/
protected $redirectManager;
/**
* The date formatter.
*
* @var \Drupal\Core\Datetime\DateFormatterInterface
*/
protected $dateFormatter;
/**
* Constructs a \Drupal\system\ConfigFormBase object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
* @param \Drupal\domain_301_redirect\Domain301RedirectManager $domain_301_redirect_manager
* The Domain301RedirectManager service.
* @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
* The date formatter.
*/
public function __construct(ConfigFactoryInterface $config_factory, Domain301RedirectManager $domain_301_redirect_manager, DateFormatterInterface $date_formatter) {
parent::__construct($config_factory);
$this->redirectManager = $domain_301_redirect_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('domain_301_redirect.manager'), $container
->get('date.formatter'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'domain_301_redirect_config_form';
}
/**
* {@inheritdoc}
*/
public function getEditableConfigNames() {
return [
'domain_301_redirect.settings',
];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL) {
$config = $this
->config('domain_301_redirect.settings');
$disabled_by_check = $config
->get('disabled_by_check');
$enabled = $config
->get('enabled');
// Warn the user if the redirect was disabled by cron.
if (!$enabled && $disabled_by_check) {
$last_checked = $config
->get('last_checked');
$this
->messenger()
->addWarning($this
->t('Redirects have been disabled by cron because the domain was not available at: %date.', [
'%date' => $this->dateFormatter
->format($last_checked),
]));
}
$form['enabled'] = [
'#type' => 'radios',
'#title' => $this
->t('Status'),
'#description' => $this
->t('Enable this setting to start 301 redirects to the domain below for all other domains.'),
'#options' => [
1 => $this
->t('Enabled'),
0 => $this
->t('Disabled'),
],
'#default_value' => $config
->get('enabled'),
];
$form['domain'] = [
'#type' => 'textfield',
'#title' => $this
->t('Domain'),
'#description' => $this
->t('This is the domain that all other domains pointing to this site will be 301 redirected to. This value should also include the scheme such as http or https but will redirect to http if not specified. Example: http://www.testsite.com'),
'#default_value' => $config
->get('domain'),
];
// Per path configuration settings to apply the redirect to specific paths.
$form['domain_check'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Domain check'),
'#open' => TRUE,
];
$form['domain_check']['check_period'] = [
'#type' => 'select',
'#title' => $this
->t('Check period'),
'#description' => $this
->t('This option selects the period between domain validation checks. If the domain no longer points to this site, domain redirection will be disabled.'),
'#options' => [
0 => $this
->t('Disabled'),
3600 => $this
->t('1 hour'),
7200 => $this
->t('2 hours'),
10800 => $this
->t('3 hours'),
21600 => $this
->t('6 hours'),
43200 => $this
->t('12 hours'),
86400 => $this
->t('1 day'),
],
'#default_value' => $config
->get('check_period'),
];
$form['domain_check']['domain_check_retries'] = [
'#type' => 'select',
'#title' => $this
->t('Retries'),
'#description' => $this
->t('Number of times to check domain availability before disabling redirects.'),
'#options' => [
1 => 1,
2 => 2,
3 => 3,
],
'#default_value' => $config
->get('domain_check_retries'),
];
$form['domain_check']['domain_check_reenable'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Re-enable domain redirection'),
'#description' => $this
->t('Turn domain redirection on when the domain becomes available.'),
'#default_value' => $config
->get('domain_check_reenable'),
];
// Per path configuration settings to apply the redirect to specific paths.
$form['applicability'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Pages'),
'#open' => TRUE,
];
$form['applicability']['pages'] = [
'#type' => 'textarea',
'#default_value' => $config
->get('pages'),
'#description' => $this
->t("Specify pages by using their paths. Enter one path per line. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", [
'%blog' => '/blog',
'%blog-wildcard' => '/blog/*',
'%front' => '<front>',
]),
];
$form['applicability']['applicability'] = [
'#type' => 'radios',
'#options' => [
Domain301Redirect::EXCLUDE_METHOD => $this
->t('Do not redirect for the listed pages'),
Domain301Redirect::INCLUDE_METHOD => $this
->t('Only redirect for the listed pages'),
],
'#default_value' => $config
->get('applicability'),
];
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$config = $this
->config('domain_301_redirect.settings');
$values = $form_state
->getValues();
if (!empty($values['enabled'])) {
// Clean & trim the domain.
$domain = trim(trim($values['domain']), '/');
if (!preg_match('|^https?://|', $domain)) {
$domain = 'http://' . $domain;
}
if (!UrlHelper::isValid($domain, TRUE)) {
$form_state
->setErrorByName('domain', $this
->t('Domain 301 redirection can not be enabled if a valid domain is not set.'));
}
else {
$domain_parts = parse_url($domain);
$domain = $domain_parts['scheme'] . '://' . $domain_parts['host'] . (!empty($domain_parts['port']) ? ':' . $domain_parts['port'] : '');
$form_state
->set('domain', $domain);
if (!$this->redirectManager
->checkDomain($domain)) {
$form_state
->setErrorByName('enabled', $this
->t('Domain 301 redirection can not be enabled as the domain you set does not currently point to this site.'));
}
else {
// Clean up if someone is manually disabling. We don't want the system
// to re-enable if the disabling was via the admin form.
$this
->config('domain_301_redirect.settings')
->set('disabled_by_check', FALSE)
->save();
}
$redirect_count = 0;
if ($this->redirectManager
->checkRedirectLoop($domain, $redirect_count)) {
$form_state
->setErrorByName('domain', $this
->t('Domain 301 redirection can not be enabled as the domain you set does not currently point to this site.', [
'@num' => $config
->get('loop_max_redirects'),
]));
}
elseif ($redirect_count > 0) {
$this
->messenger()
->addWarning($this
->t('The redirect domain follows @count redirects. You may want to specify the final domain.', [
'@count' => $redirect_count,
]));
}
}
}
parent::validateForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValues();
$this
->config('domain_301_redirect.settings')
->set('enabled', $values['enabled'])
->set('domain', $values['domain'])
->set('check_period', $values['check_period'])
->set('domain_check_retries', $values['domain_check_retries'])
->set('domain_check_reenable', $values['domain_check_reenable'])
->set('applicability', $values['applicability'])
->set('pages', $values['pages'])
->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 | |
Domain301RedirectConfigForm:: |
protected | property | The date formatter. | |
Domain301RedirectConfigForm:: |
protected | property | Domain 301 Redirect Manager. | |
Domain301RedirectConfigForm:: |
public | function |
Form constructor. Overrides ConfigFormBase:: |
|
Domain301RedirectConfigForm:: |
public static | function |
Instantiates a new instance of this class. Overrides ConfigFormBase:: |
|
Domain301RedirectConfigForm:: |
public | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
Domain301RedirectConfigForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
Domain301RedirectConfigForm:: |
public | function |
Form submission handler. Overrides ConfigFormBase:: |
|
Domain301RedirectConfigForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
Domain301RedirectConfigForm:: |
public | function |
Constructs a \Drupal\system\ConfigFormBase object. Overrides ConfigFormBase:: |
|
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. | |
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. |