Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / locale / src / Form / LocaleSettingsForm.php
1 <?php
2
3 namespace Drupal\locale\Form;
4
5 use Drupal\Core\Form\ConfigFormBase;
6 use Drupal\Core\Form\FormStateInterface;
7
8 /**
9  * Configure locale settings for this site.
10  *
11  * @internal
12  */
13 class LocaleSettingsForm extends ConfigFormBase {
14
15   /**
16    * {@inheritdoc}
17    */
18   public function getFormId() {
19     return 'locale_translate_settings';
20   }
21
22   /**
23    * {@inheritdoc}
24    */
25   protected function getEditableConfigNames() {
26     return ['locale.settings'];
27   }
28
29   /**
30    * {@inheritdoc}
31    */
32   public function buildForm(array $form, FormStateInterface $form_state) {
33     $config = $this->config('locale.settings');
34
35     $form['update_interval_days'] = [
36       '#type' => 'radios',
37       '#title' => $this->t('Check for updates'),
38       '#default_value' => $config->get('translation.update_interval_days'),
39       '#options' => [
40         '0' => $this->t('Never (manually)'),
41         '7' => $this->t('Weekly'),
42         '30' => $this->t('Monthly'),
43       ],
44       '#description' => $this->t('Select how frequently you want to check for new interface translations for your currently installed modules and themes. <a href=":url">Check updates now</a>.', [':url' => $this->url('locale.check_translation')]),
45     ];
46
47     if ($directory = $config->get('translation.path')) {
48       $description = $this->t('Translation files are stored locally in the  %path directory. You can change this directory on the <a href=":url">File system</a> configuration page.', ['%path' => $directory, ':url' => $this->url('system.file_system_settings')]);
49     }
50     else {
51       $description = $this->t('Translation files will not be stored locally. Change the Interface translation directory on the <a href=":url">File system configuration</a> page.', [':url' => $this->url('system.file_system_settings')]);
52     }
53     $form['#translation_directory'] = $directory;
54     $form['use_source'] = [
55       '#type' => 'radios',
56       '#title' => $this->t('Translation source'),
57       '#default_value' => $config->get('translation.use_source'),
58       '#options' => [
59         LOCALE_TRANSLATION_USE_SOURCE_REMOTE_AND_LOCAL => $this->t('Drupal translation server and local files'),
60         LOCALE_TRANSLATION_USE_SOURCE_LOCAL => $this->t('Local files only'),
61       ],
62       '#description' => $this->t('The source of translation files for automatic interface translation.') . ' ' . $description,
63     ];
64
65     if ($config->get('translation.overwrite_not_customized') == FALSE) {
66       $default = LOCALE_TRANSLATION_OVERWRITE_NONE;
67     }
68     elseif ($config->get('translation.overwrite_customized') == TRUE) {
69       $default = LOCALE_TRANSLATION_OVERWRITE_ALL;
70     }
71     else {
72       $default = LOCALE_TRANSLATION_OVERWRITE_NON_CUSTOMIZED;
73     }
74     $form['overwrite'] = [
75       '#type' => 'radios',
76       '#title' => $this->t('Import behavior'),
77       '#default_value' => $default,
78       '#options' => [
79         LOCALE_TRANSLATION_OVERWRITE_NONE => $this->t("Don't overwrite existing translations."),
80         LOCALE_TRANSLATION_OVERWRITE_NON_CUSTOMIZED => $this->t('Only overwrite imported translations, customized translations are kept.'),
81         LOCALE_TRANSLATION_OVERWRITE_ALL => $this->t('Overwrite existing translations.'),
82       ],
83       '#description' => $this->t('How to treat existing translations when automatically updating the interface translations.'),
84     ];
85
86     return parent::buildForm($form, $form_state);
87   }
88
89   /**
90    * {@inheritdoc}
91    */
92   public function validateForm(array &$form, FormStateInterface $form_state) {
93     parent::validateForm($form, $form_state);
94
95     if (empty($form['#translation_directory']) && $form_state->getValue('use_source') == LOCALE_TRANSLATION_USE_SOURCE_LOCAL) {
96       $form_state->setErrorByName('use_source', $this->t('You have selected local translation source, but no <a href=":url">Interface translation directory</a> was configured.', [':url' => $this->url('system.file_system_settings')]));
97     }
98   }
99
100   /**
101    * {@inheritdoc}
102    */
103   public function submitForm(array &$form, FormStateInterface $form_state) {
104     $values = $form_state->getValues();
105
106     $config = $this->config('locale.settings');
107     $config->set('translation.update_interval_days', $values['update_interval_days'])->save();
108     $config->set('translation.use_source', $values['use_source'])->save();
109
110     switch ($values['overwrite']) {
111       case LOCALE_TRANSLATION_OVERWRITE_ALL:
112         $config
113           ->set('translation.overwrite_customized', TRUE)
114           ->set('translation.overwrite_not_customized', TRUE)
115           ->save();
116         break;
117
118       case LOCALE_TRANSLATION_OVERWRITE_NON_CUSTOMIZED:
119         $config
120           ->set('translation.overwrite_customized', FALSE)
121           ->set('translation.overwrite_not_customized', TRUE)
122           ->save();
123         break;
124
125       case LOCALE_TRANSLATION_OVERWRITE_NONE:
126         $config
127           ->set('translation.overwrite_customized', FALSE)
128           ->set('translation.overwrite_not_customized', FALSE)
129           ->save();
130         break;
131     }
132
133     // Invalidate the cached translation status when the configuration setting
134     // of 'use_source' changes.
135     if ($form['use_source']['#default_value'] != $form_state->getValue('use_source')) {
136       locale_translation_clear_status();
137     }
138
139     parent::submitForm($form, $form_state);
140   }
141
142 }