4e4e1571fd54946ab0e81394055f30d8e6b14704
[yaffs-website] / web / core / modules / system / src / Form / SiteInformationForm.php
1 <?php
2
3 namespace Drupal\system\Form;
4
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;
12
13 /**
14  * Configure site information settings for this site.
15  *
16  * @internal
17  */
18 class SiteInformationForm extends ConfigFormBase {
19
20   /**
21    * The path alias manager.
22    *
23    * @var \Drupal\Core\Path\AliasManagerInterface
24    */
25   protected $aliasManager;
26
27   /**
28    * The path validator.
29    *
30    * @var \Drupal\Core\Path\PathValidatorInterface
31    */
32   protected $pathValidator;
33
34   /**
35    * The request context.
36    *
37    * @var \Drupal\Core\Routing\RequestContext
38    */
39   protected $requestContext;
40
41   /**
42    * Constructs a SiteInformationForm object.
43    *
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
49    *   The path validator.
50    * @param \Drupal\Core\Routing\RequestContext $request_context
51    *   The request context.
52    */
53   public function __construct(ConfigFactoryInterface $config_factory, AliasManagerInterface $alias_manager, PathValidatorInterface $path_validator, RequestContext $request_context) {
54     parent::__construct($config_factory);
55
56     $this->aliasManager = $alias_manager;
57     $this->pathValidator = $path_validator;
58     $this->requestContext = $request_context;
59   }
60
61   /**
62    * {@inheritdoc}
63    */
64   public static function create(ContainerInterface $container) {
65     return new static(
66       $container->get('config.factory'),
67       $container->get('path.alias_manager'),
68       $container->get('path.validator'),
69       $container->get('router.request_context')
70     );
71   }
72
73   /**
74    * {@inheritdoc}
75    */
76   public function getFormId() {
77     return 'system_site_information_settings';
78   }
79
80   /**
81    * {@inheritdoc}
82    */
83   protected function getEditableConfigNames() {
84     return ['system.site'];
85   }
86
87   /**
88    * {@inheritdoc}
89    */
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');
95     }
96
97     $form['site_information'] = [
98       '#type' => 'details',
99       '#title' => t('Site details'),
100       '#open' => TRUE,
101     ];
102     $form['site_information']['site_name'] = [
103       '#type' => 'textfield',
104       '#title' => t('Site name'),
105       '#default_value' => $site_config->get('name'),
106       '#required' => TRUE,
107     ];
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."),
113     ];
114     $form['site_information']['site_mail'] = [
115       '#type' => 'email',
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.)"),
119       '#required' => TRUE,
120     ];
121     $form['front_page'] = [
122       '#type' => 'details',
123       '#title' => t('Front page'),
124       '#open' => TRUE,
125     ];
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,
131       '#size' => 40,
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(),
134     ];
135     $form['error_page'] = [
136       '#type' => 'details',
137       '#title' => t('Error pages'),
138       '#open' => TRUE,
139     ];
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'),
144       '#size' => 40,
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.'),
146     ];
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'),
151       '#size' => 40,
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.'),
153     ];
154
155     return parent::buildForm($form, $form_state);
156   }
157
158   /**
159    * {@inheritdoc}
160    */
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');
166     }
167     else {
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')));
170     }
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')]));
174
175     }
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')]));
178     }
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')));
182     }
183     if (!$form_state->isValueEmpty('site_404')) {
184       $form_state->setValueForElement($form['error_page']['site_404'], $this->aliasManager->getPathByAlias($form_state->getValue('site_404')));
185     }
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')]));
188     }
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')]));
191     }
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')]));
195     }
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')]));
199     }
200
201     parent::validateForm($form, $form_state);
202   }
203
204   /**
205    * {@inheritdoc}
206    */
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'))
215       ->save();
216
217     parent::submitForm($form, $form_state);
218   }
219
220 }