DomainSettingsForm.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace Drupal\domain\Form;
  3. use Drupal\Core\Form\ConfigFormBase;
  4. use Drupal\Core\Form\FormStateInterface;
  5. /**
  6. * Class DomainSettingsForm.
  7. *
  8. * @package Drupal\domain\Form
  9. */
  10. class DomainSettingsForm extends ConfigFormBase {
  11. /**
  12. * {@inheritdoc}
  13. */
  14. public function getFormId() {
  15. return 'domain_settings';
  16. }
  17. /**
  18. * {@inheritdoc}
  19. */
  20. protected function getEditableConfigNames() {
  21. return ['domain.settings'];
  22. }
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public function buildForm(array $form, FormStateInterface $form_state) {
  27. $config = $this->config('domain.settings');
  28. $form['allow_non_ascii'] = [
  29. '#type' => 'checkbox',
  30. '#title' => $this->t('Allow non-ASCII characters in domains and aliases'),
  31. '#default_value' => $config->get('allow_non_ascii'),
  32. '#description' => $this->t('Domains may be registered with international character sets. Note that not all DNS server respect non-ascii characters.'),
  33. ];
  34. $form['www_prefix'] = [
  35. '#type' => 'checkbox',
  36. '#title' => $this->t('Ignore www prefix when negotiating domains'),
  37. '#default_value' => $config->get('www_prefix'),
  38. '#description' => $this->t('Domain negotiation will ignore any www prefixes for all requests.'),
  39. ];
  40. // Get the usable tokens for this field.
  41. $patterns = [];
  42. foreach (\Drupal::service('domain.token')->getCallbacks() as $key => $callback) {
  43. $patterns[] = "[domain:$key]";
  44. }
  45. $form['css_classes'] = [
  46. '#type' => 'textfield',
  47. '#size' => 80,
  48. '#title' => $this->t('Custom CSS classes'),
  49. '#default_value' => $config->get('css_classes'),
  50. '#description' => $this->t('Enter any CSS classes that should be added to the &lt;body&gt; tag. Available replacement patterns are: @patterns', [
  51. '@patterns' => implode(', ', $patterns),
  52. ]),
  53. ];
  54. $form['login_paths'] = [
  55. '#type' => 'textarea',
  56. '#rows' => 5,
  57. '#columns' => 40,
  58. '#title' => $this->t('Paths that should be accessible for inactive domains'),
  59. '#default_value' => $config->get('login_paths'),
  60. '#description' => $this->t('Inactive domains are only accessible to users with permission.
  61. Enter any paths that should be accessible, one per line. Normally, only the
  62. login path will be allowed.'),
  63. ];
  64. return parent::buildForm($form, $form_state);
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function submitForm(array &$form, FormStateInterface $form_state) {
  70. $config = $this->config('domain.settings');
  71. foreach ($this->settingsKeys() as $key) {
  72. $config->set($key, $form_state->getValue($key));
  73. }
  74. $config->save();
  75. parent::submitForm($form, $form_state);
  76. }
  77. /**
  78. * Returns an array of settings keys.
  79. */
  80. public function settingsKeys() {
  81. return [
  82. 'allow_non_ascii',
  83. 'www_prefix',
  84. 'login_paths',
  85. 'css_classes',
  86. ];
  87. }
  88. }