View source
<?php
namespace Drupal\rename_admin_paths\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
class RenameAdminPathsSettingsForm extends ConfigFormBase {
public function getFormId() {
return 'rename_admin_paths_settings_form';
}
protected function getEditableConfigNames() {
return [
'rename_admin_paths.settings',
];
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('rename_admin_paths.settings');
$callbacks = new RenameAdminPathsCallbacks();
$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' => $config
->get('admin_path'),
'#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' => $config
->get('admin_path_value'),
'#description' => $this
->t('This value will replace "admin" in admin path.'),
'#element_validate' => [
[
$callbacks,
'validatePath',
],
],
];
$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' => $config
->get('user_path'),
'#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' => $config
->get('user_path_value'),
'#description' => $this
->t('This value will replace "user" in user path.'),
'#element_validate' => [
[
$callbacks,
'validatePath',
],
],
];
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = \Drupal::service('config.factory')
->getEditable('rename_admin_paths.settings');
$config
->set('admin_path', $form_state
->getValue('admin_path'));
$config
->set('admin_path_value', $form_state
->getValue('admin_path_value'));
$config
->set('user_path', $form_state
->getValue('user_path'));
$config
->set('user_path_value', $form_state
->getValue('user_path_value'));
$config
->save();
\Drupal::service('router.builder')
->rebuild();
}
}