DomainConfigSettingsForm.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. namespace Drupal\domain_site_settings\Form;
  3. use Drupal\Core\Form\ConfigFormBase;
  4. use Drupal\Core\Form\FormStateInterface;
  5. use Drupal\Core\Config\ConfigFactoryInterface;
  6. use Symfony\Component\DependencyInjection\ContainerInterface;
  7. use Drupal\Core\Path\AliasManagerInterface;
  8. use Drupal\Core\Path\PathValidatorInterface;
  9. use Drupal\Core\Routing\RequestContext;
  10. /**
  11. * Class DomainConfigSettingsForm.
  12. *
  13. * @package Drupal\domain_site_settings\Form
  14. */
  15. class DomainConfigSettingsForm extends ConfigFormBase {
  16. /**
  17. * The path alias manager.
  18. *
  19. * @var \Drupal\Core\Path\AliasManagerInterface
  20. */
  21. protected $aliasManager;
  22. /**
  23. * The path validator.
  24. *
  25. * @var \Drupal\Core\Path\PathValidatorInterface
  26. */
  27. protected $pathValidator;
  28. /**
  29. * The request context.
  30. *
  31. * @var \Drupal\Core\Routing\RequestContext
  32. */
  33. protected $requestContext;
  34. /**
  35. * Constructs a SiteInformationForm object.
  36. *
  37. * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
  38. * The factory for configuration objects.
  39. * @param \Drupal\Core\Path\AliasManagerInterface $alias_manager
  40. * The path alias manager.
  41. * @param \Drupal\Core\Path\PathValidatorInterface $path_validator
  42. * The path validator.
  43. * @param \Drupal\Core\Routing\RequestContext $request_context
  44. * The request context.
  45. */
  46. public function __construct(ConfigFactoryInterface $config_factory, AliasManagerInterface $alias_manager, PathValidatorInterface $path_validator, RequestContext $request_context) {
  47. parent::__construct($config_factory);
  48. $this->aliasManager = $alias_manager;
  49. $this->pathValidator = $path_validator;
  50. $this->requestContext = $request_context;
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public static function create(ContainerInterface $container) {
  56. return new static(
  57. $container->get('config.factory'), $container->get('path.alias_manager'), $container->get('path.validator'), $container->get('router.request_context')
  58. );
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. protected function getEditableConfigNames() {
  64. return [
  65. 'domain_site_settings.domainconfigsettings',
  66. ];
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function getFormId() {
  72. return 'domain_config_settings_form';
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. public function buildForm(array $form, FormStateInterface $form_state) {
  78. $config = $this->config('domain_site_settings.domainconfigsettings');
  79. $domain_id = $this->getRequest()->get('domain_id');
  80. $site_config = $this->config('system.site');
  81. $site_mail = $site_config->get('mail');
  82. if (empty($site_mail)) {
  83. $site_mail = ini_get('sendmail_from');
  84. }
  85. if ($config->get($domain_id) != NULL) {
  86. $site_mail = $config->get($domain_id . '.site_mail');
  87. }
  88. $form['site_information'] = [
  89. '#type' => 'details',
  90. '#title' => $this->t('Site details'),
  91. '#open' => TRUE,
  92. ];
  93. $form['site_information']['site_name'] = [
  94. '#type' => 'textfield',
  95. '#title' => $this->t('Site name'),
  96. '#default_value' => ($config->get($domain_id) != NULL) ? $config->get($domain_id . '.site_name') : $site_config->get('name'),
  97. '#required' => TRUE,
  98. ];
  99. $form['site_information']['site_slogan'] = [
  100. '#type' => 'textfield',
  101. '#title' => $this->t('Slogan'),
  102. '#default_value' => ($config->get($domain_id) != NULL) ? $config->get($domain_id . '.site_slogan') : $site_config->get('slogan'),
  103. '#description' => $this->t("How this is used depends on your site's theme."),
  104. ];
  105. $form['site_information']['site_mail'] = [
  106. '#type' => 'email',
  107. '#title' => $this->t('Email address'),
  108. '#default_value' => $site_mail,
  109. '#description' => $this->t("The <em>From</em> address in automated emails sent during registration and new password requests, and other notifications. (Use an address ending in your site's domain to help prevent this email being flagged as spam.)"),
  110. '#required' => TRUE,
  111. ];
  112. $form['front_page'] = [
  113. '#type' => 'details',
  114. '#title' => $this->t('Front page'),
  115. '#open' => TRUE,
  116. ];
  117. $front_page = $site_config->get('page.front') != '/user/login' ? $this->aliasManager->getAliasByPath($site_config->get('page.front')) : '';
  118. $front_page = ($config->get($domain_id) != NULL) ? $config->get($domain_id . '.site_frontpage') : $front_page;
  119. $form['front_page']['site_frontpage'] = [
  120. '#type' => 'textfield',
  121. '#title' => $this->t('Default front page'),
  122. '#default_value' => $front_page,
  123. '#size' => 40,
  124. '#description' => $this->t('Optionally, specify a relative URL to display as the front page. Leave blank to display the default front page.'),
  125. '#field_prefix' => $this->requestContext->getCompleteBaseUrl(),
  126. ];
  127. $form['error_page'] = [
  128. '#type' => 'details',
  129. '#title' => $this->t('Error pages'),
  130. '#open' => TRUE,
  131. ];
  132. $form['error_page']['site_403'] = [
  133. '#type' => 'textfield',
  134. '#title' => $this->t('Default 403 (access denied) page'),
  135. '#default_value' => ($config->get($domain_id) !== NULL) ? $config->get($domain_id . '.site_403') : $site_config->get('page.403'),
  136. '#size' => 40,
  137. '#description' => $this->t('This page is displayed when the requested document is denied to the current user. Leave blank to display a generic "access denied" page.'),
  138. ];
  139. $form['error_page']['site_404'] = [
  140. '#type' => 'textfield',
  141. '#title' => $this->t('Default 404 (not found) page'),
  142. '#default_value' => ($config->get($domain_id) !== NULL) ? $config->get($domain_id . '.site_404') : $site_config->get('page.404'),
  143. '#size' => 40,
  144. '#description' => $this->t('This page is displayed when no other content matches the requested document. Leave blank to display a generic "page not found" page.'),
  145. ];
  146. $form['domain_id'] = [
  147. '#type' => 'hidden',
  148. '#title' => $this->t('Domain ID'),
  149. '#default_value' => $domain_id,
  150. ];
  151. return parent::buildForm($form, $form_state);
  152. }
  153. /**
  154. * {@inheritdoc}
  155. */
  156. public function validateForm(array &$form, FormStateInterface $form_state) {
  157. // Check for empty front page path.
  158. if ($form_state->isValueEmpty('site_frontpage')) {
  159. // Set to default "user/login".
  160. $form_state->setValueForElement($form['front_page']['site_frontpage'], '/user/login');
  161. }
  162. else {
  163. // Get the normal path of the front page.
  164. $form_state->setValueForElement($form['front_page']['site_frontpage'], $this->aliasManager->getPathByAlias($form_state->getValue('site_frontpage')));
  165. }
  166. // Validate front page path.
  167. if (($value = $form_state->getValue('site_frontpage')) && $value[0] !== '/') {
  168. $form_state->setErrorByName('site_frontpage', $this->t("The path '%path' has to start with a slash.", ['%path' => $form_state->getValue('site_frontpage')]));
  169. }
  170. if (!$this->pathValidator->isValid($form_state->getValue('site_frontpage'))) {
  171. $form_state->setErrorByName('site_frontpage', $this->t("The path '%path' is either invalid or you do not have access to it.", ['%path' => $form_state->getValue('site_frontpage')]));
  172. }
  173. // Get the normal paths of both error pages.
  174. if (!$form_state->isValueEmpty('site_403')) {
  175. $form_state->setValueForElement($form['error_page']['site_403'], $this->aliasManager->getPathByAlias($form_state->getValue('site_403')));
  176. }
  177. if (!$form_state->isValueEmpty('site_404')) {
  178. $form_state->setValueForElement($form['error_page']['site_404'], $this->aliasManager->getPathByAlias($form_state->getValue('site_404')));
  179. }
  180. if (($value = $form_state->getValue('site_403')) && $value[0] !== '/') {
  181. $form_state->setErrorByName('site_403', $this->t("The path '%path' has to start with a slash.", ['%path' => $form_state->getValue('site_403')]));
  182. }
  183. if (($value = $form_state->getValue('site_404')) && $value[0] !== '/') {
  184. $form_state->setErrorByName('site_404', $this->t("The path '%path' has to start with a slash.", ['%path' => $form_state->getValue('site_404')]));
  185. }
  186. // Validate 403 error path.
  187. if (!$form_state->isValueEmpty('site_403') && !$this->pathValidator->isValid($form_state->getValue('site_403'))) {
  188. $form_state->setErrorByName('site_403', $this->t("The path '%path' is either invalid or you do not have access to it.", ['%path' => $form_state->getValue('site_403')]));
  189. }
  190. // Validate 404 error path.
  191. if (!$form_state->isValueEmpty('site_404') && !$this->pathValidator->isValid($form_state->getValue('site_404'))) {
  192. $form_state->setErrorByName('site_404', $this->t("The path '%path' is either invalid or you do not have access to it.", ['%path' => $form_state->getValue('site_404')]));
  193. }
  194. parent::validateForm($form, $form_state);
  195. }
  196. /**
  197. * {@inheritdoc}
  198. */
  199. public function submitForm(array &$form, FormStateInterface $form_state) {
  200. parent::submitForm($form, $form_state);
  201. $domain_id = $form_state->getValue('domain_id');
  202. $site_name = $form_state->getValue('site_name');
  203. $site_slogan = $form_state->getValue('site_slogan');
  204. $site_mail = $form_state->getValue('site_mail');
  205. $site_frontpage = $form_state->getValue('site_frontpage');
  206. $site_403 = $form_state->getValue('site_403');
  207. $site_404 = $form_state->getValue('site_404');
  208. $config = $this->config('domain_site_settings.domainconfigsettings');
  209. $config->set($domain_id . '.site_name', $site_name);
  210. $config->set($domain_id . '.site_slogan', $site_slogan);
  211. $config->set($domain_id . '.site_mail', $site_mail);
  212. $config->set($domain_id . '.site_frontpage', $site_frontpage);
  213. $config->set($domain_id . '.site_403', $site_403);
  214. $config->set($domain_id . '.site_404', $site_404);
  215. $config->save();
  216. }
  217. }