PageRenderTime 46ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/system/src/Form/SiteInformationForm.php

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