View source
<?php
namespace Drupal\rename_admin_paths\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteBuilderInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\rename_admin_paths\Config;
use Drupal\rename_admin_paths\EventSubscriber\RenameAdminPathsEventSubscriber;
use Symfony\Component\DependencyInjection\ContainerInterface;
class RenameAdminPathsSettingsForm extends ConfigFormBase {
use StringTranslationTrait;
private $config;
private $routeBuilder;
public function getFormId() : string {
return 'rename_admin_paths_settings_form';
}
protected function getEditableConfigNames() : array {
return [
Config::CONFIG_KEY,
];
}
public function __construct(Config $config, RouteBuilderInterface $routeBuilder, TranslationInterface $stringTranslation) {
$this->config = $config;
$this->routeBuilder = $routeBuilder;
$this->stringTranslation = $stringTranslation;
}
public static function create(ContainerInterface $container) : self {
return new static($container
->get(Config::class), $container
->get('router.builder'), $container
->get('string_translation'));
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['admin_path'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Rename admin path'),
];
$form['admin_path']['admin_path'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Rename admin path'),
'#default_value' => $this->config
->isPathEnabled('admin'),
'#description' => $this
->t('If checked, "admin" will be replaced by the following term in admin path.'),
];
$form['admin_path']['admin_path_value'] = [
'#type' => 'textfield',
'#title' => $this
->t('Replace "admin" in admin path by'),
'#default_value' => $this->config
->getPathValue('admin'),
'#description' => $this
->t('This value will replace "admin" in admin path.'),
'#element_validate' => [
[
$this,
'validate',
],
],
];
$form['user_path'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Rename user path'),
];
$form['user_path']['user_path'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Rename user path'),
'#default_value' => $this->config
->isPathEnabled('user'),
'#description' => $this
->t('If checked, "user" will be replaced by the following term in user path.'),
];
$form['user_path']['user_path_value'] = [
'#type' => 'textfield',
'#title' => $this
->t('Replace "user" in user path by'),
'#default_value' => $this->config
->getPathValue('user'),
'#description' => $this
->t('This value will replace "user" in user path.'),
'#element_validate' => [
[
$this,
'validate',
],
],
];
return parent::buildForm($form, $form_state);
}
public function validate(&$element, FormStateInterface $formState) {
if (empty($element['#value'])) {
$formState
->setError($element, $this
->t('Path replacement value must contain a value.'));
}
elseif (!RenameAdminPathsValidator::isValidPath($element['#value'])) {
$formState
->setError($element, $this
->t('Path replacement value must contain only letters, numbers, hyphens and underscores.'));
}
elseif (RenameAdminPathsValidator::isDefaultPath($element['#value'])) {
$formState
->setError($element, sprintf($this
->t('Renaming to a default name (%s) is not allowed.'), implode(', ', RenameAdminPathsEventSubscriber::ADMIN_PATHS)));
}
}
public function submitForm(array &$form, FormStateInterface $formState) {
$this
->saveConfiguration($formState);
$this->routeBuilder
->rebuild();
parent::submitForm($form, $formState);
$formState
->setRedirect('rename_admin_paths.admin');
}
private function saveConfiguration(FormStateInterface $formState) {
$this->config
->setPathEnabled('admin', $formState
->getValue('admin_path'));
$this->config
->setPathValue('admin', $formState
->getValue('admin_path_value'));
$this->config
->setPathEnabled('user', $formState
->getValue('user_path'));
$this->config
->setPathValue('user', $formState
->getValue('user_path_value'));
$this->config
->save();
}
}