3 namespace Drupal\system\Form;
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Path\AliasManagerInterface;
8 use Drupal\Core\Form\ConfigFormBase;
9 use Drupal\Core\Path\PathValidatorInterface;
10 use Drupal\Core\Routing\RequestContext;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
14 * Configure site information settings for this site.
18 class SiteInformationForm extends ConfigFormBase {
21 * The path alias manager.
23 * @var \Drupal\Core\Path\AliasManagerInterface
25 protected $aliasManager;
30 * @var \Drupal\Core\Path\PathValidatorInterface
32 protected $pathValidator;
35 * The request context.
37 * @var \Drupal\Core\Routing\RequestContext
39 protected $requestContext;
42 * Constructs a SiteInformationForm object.
44 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
45 * The factory for configuration objects.
46 * @param \Drupal\Core\Path\AliasManagerInterface $alias_manager
47 * The path alias manager.
48 * @param \Drupal\Core\Path\PathValidatorInterface $path_validator
50 * @param \Drupal\Core\Routing\RequestContext $request_context
51 * The request context.
53 public function __construct(ConfigFactoryInterface $config_factory, AliasManagerInterface $alias_manager, PathValidatorInterface $path_validator, RequestContext $request_context) {
54 parent::__construct($config_factory);
56 $this->aliasManager = $alias_manager;
57 $this->pathValidator = $path_validator;
58 $this->requestContext = $request_context;
64 public static function create(ContainerInterface $container) {
66 $container->get('config.factory'),
67 $container->get('path.alias_manager'),
68 $container->get('path.validator'),
69 $container->get('router.request_context')
76 public function getFormId() {
77 return 'system_site_information_settings';
83 protected function getEditableConfigNames() {
84 return ['system.site'];
90 public function buildForm(array $form, FormStateInterface $form_state) {
91 $site_config = $this->config('system.site');
92 $site_mail = $site_config->get('mail');
93 if (empty($site_mail)) {
94 $site_mail = ini_get('sendmail_from');
97 $form['site_information'] = [
99 '#title' => t('Site details'),
102 $form['site_information']['site_name'] = [
103 '#type' => 'textfield',
104 '#title' => t('Site name'),
105 '#default_value' => $site_config->get('name'),
108 $form['site_information']['site_slogan'] = [
109 '#type' => 'textfield',
110 '#title' => t('Slogan'),
111 '#default_value' => $site_config->get('slogan'),
112 '#description' => t("How this is used depends on your site's theme."),
114 $form['site_information']['site_mail'] = [
116 '#title' => t('Email address'),
117 '#default_value' => $site_mail,
118 '#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.)"),
121 $form['front_page'] = [
122 '#type' => 'details',
123 '#title' => t('Front page'),
126 $front_page = $site_config->get('page.front') != '/user/login' ? $this->aliasManager->getAliasByPath($site_config->get('page.front')) : '';
127 $form['front_page']['site_frontpage'] = [
128 '#type' => 'textfield',
129 '#title' => t('Default front page'),
130 '#default_value' => $front_page,
132 '#description' => t('Optionally, specify a relative URL to display as the front page. Leave blank to display the default front page.'),
133 '#field_prefix' => $this->requestContext->getCompleteBaseUrl(),
135 $form['error_page'] = [
136 '#type' => 'details',
137 '#title' => t('Error pages'),
140 $form['error_page']['site_403'] = [
141 '#type' => 'textfield',
142 '#title' => t('Default 403 (access denied) page'),
143 '#default_value' => $site_config->get('page.403'),
145 '#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.'),
147 $form['error_page']['site_404'] = [
148 '#type' => 'textfield',
149 '#title' => t('Default 404 (not found) page'),
150 '#default_value' => $site_config->get('page.404'),
152 '#description' => t('This page is displayed when no other content matches the requested document. Leave blank to display a generic "page not found" page.'),
155 return parent::buildForm($form, $form_state);
161 public function validateForm(array &$form, FormStateInterface $form_state) {
162 // Check for empty front page path.
163 if ($form_state->isValueEmpty('site_frontpage')) {
164 // Set to default "user/login".
165 $form_state->setValueForElement($form['front_page']['site_frontpage'], '/user/login');
168 // Get the normal path of the front page.
169 $form_state->setValueForElement($form['front_page']['site_frontpage'], $this->aliasManager->getPathByAlias($form_state->getValue('site_frontpage')));
171 // Validate front page path.
172 if (($value = $form_state->getValue('site_frontpage')) && $value[0] !== '/') {
173 $form_state->setErrorByName('site_frontpage', $this->t("The path '%path' has to start with a slash.", ['%path' => $form_state->getValue('site_frontpage')]));
176 if (!$this->pathValidator->isValid($form_state->getValue('site_frontpage'))) {
177 $form_state->setErrorByName('site_frontpage', $this->t("Either the path '%path' is invalid or you do not have access to it.", ['%path' => $form_state->getValue('site_frontpage')]));
179 // Get the normal paths of both error pages.
180 if (!$form_state->isValueEmpty('site_403')) {
181 $form_state->setValueForElement($form['error_page']['site_403'], $this->aliasManager->getPathByAlias($form_state->getValue('site_403')));
183 if (!$form_state->isValueEmpty('site_404')) {
184 $form_state->setValueForElement($form['error_page']['site_404'], $this->aliasManager->getPathByAlias($form_state->getValue('site_404')));
186 if (($value = $form_state->getValue('site_403')) && $value[0] !== '/') {
187 $form_state->setErrorByName('site_403', $this->t("The path '%path' has to start with a slash.", ['%path' => $form_state->getValue('site_403')]));
189 if (($value = $form_state->getValue('site_404')) && $value[0] !== '/') {
190 $form_state->setErrorByName('site_404', $this->t("The path '%path' has to start with a slash.", ['%path' => $form_state->getValue('site_404')]));
192 // Validate 403 error path.
193 if (!$form_state->isValueEmpty('site_403') && !$this->pathValidator->isValid($form_state->getValue('site_403'))) {
194 $form_state->setErrorByName('site_403', $this->t("Either the path '%path' is invalid or you do not have access to it.", ['%path' => $form_state->getValue('site_403')]));
196 // Validate 404 error path.
197 if (!$form_state->isValueEmpty('site_404') && !$this->pathValidator->isValid($form_state->getValue('site_404'))) {
198 $form_state->setErrorByName('site_404', $this->t("Either the path '%path' is invalid or you do not have access to it.", ['%path' => $form_state->getValue('site_404')]));
201 parent::validateForm($form, $form_state);
207 public function submitForm(array &$form, FormStateInterface $form_state) {
208 $this->config('system.site')
209 ->set('name', $form_state->getValue('site_name'))
210 ->set('mail', $form_state->getValue('site_mail'))
211 ->set('slogan', $form_state->getValue('site_slogan'))
212 ->set('page.front', $form_state->getValue('site_frontpage'))
213 ->set('page.403', $form_state->getValue('site_403'))
214 ->set('page.404', $form_state->getValue('site_404'))
217 parent::submitForm($form, $form_state);