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