Upgraded drupal core with security updates
[yaffs-website] / web / core / lib / Drupal / Core / Installer / Form / SelectLanguageForm.php
1 <?php
2
3 namespace Drupal\Core\Installer\Form;
4
5 use Drupal\Component\Utility\UserAgent;
6 use Drupal\Core\Form\FormBase;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Language\LanguageManager;
9 use Symfony\Component\HttpFoundation\Request;
10
11 /**
12  * Provides the language selection form.
13  *
14  * Note that hardcoded text provided by this form is not translated. This is
15  * because translations are downloaded as a result of submitting this form.
16  */
17 class SelectLanguageForm extends FormBase {
18
19   /**
20    * {@inheritdoc}
21    */
22   public function getFormId() {
23     return 'install_select_language_form';
24   }
25
26   /**
27    * {@inheritdoc}
28    */
29   public function buildForm(array $form, FormStateInterface $form_state, $install_state = NULL) {
30     if (count($install_state['translations']) > 1) {
31       $files = $install_state['translations'];
32     }
33     else {
34       $files = [];
35     }
36     $standard_languages = LanguageManager::getStandardLanguageList();
37     $select_options = [];
38     $browser_options = [];
39
40     $form['#title'] = 'Choose language';
41
42     // Build a select list with language names in native language for the user
43     // to choose from. And build a list of available languages for the browser
44     // to select the language default from.
45     // Select lists based on all standard languages.
46     foreach ($standard_languages as $langcode => $language_names) {
47       $select_options[$langcode] = $language_names[1];
48       $browser_options[$langcode] = $langcode;
49     }
50     // Add languages based on language files in the translations directory.
51     if (count($files)) {
52       foreach ($files as $langcode => $uri) {
53         $select_options[$langcode] = isset($standard_languages[$langcode]) ? $standard_languages[$langcode][1] : $langcode;
54         $browser_options[$langcode] = $langcode;
55       }
56     }
57     asort($select_options);
58     $request = Request::createFromGlobals();
59     $browser_langcode = UserAgent::getBestMatchingLangcode($request->server->get('HTTP_ACCEPT_LANGUAGE'), $browser_options);
60     $form['langcode'] = [
61       '#type' => 'select',
62       '#title' => 'Choose language',
63       '#title_display' => 'invisible',
64       '#options' => $select_options,
65       // Use the browser detected language as default or English if nothing found.
66       '#default_value' => !empty($browser_langcode) ? $browser_langcode : 'en',
67     ];
68     $link_to_english = install_full_redirect_url(['parameters' => ['langcode' => 'en']]);
69     $form['help'] = [
70       '#type' => 'item',
71       // #markup is XSS admin filtered which ensures unsafe protocols will be
72       // removed from the url.
73       '#markup' => '<p>Translations will be downloaded from the <a href="http://localize.drupal.org">Drupal Translation website</a>. If you do not want this, select <a href="' . $link_to_english . '">English</a>.</p>',
74       '#states' => [
75         'invisible' => [
76           'select[name="langcode"]' => ['value' => 'en'],
77         ],
78       ],
79     ];
80     $form['actions'] = ['#type' => 'actions'];
81     $form['actions']['submit'] = [
82       '#type' => 'submit',
83       '#value' => 'Save and continue',
84       '#button_type' => 'primary',
85     ];
86     return $form;
87   }
88
89   /**
90    * {@inheritdoc}
91    */
92   public function submitForm(array &$form, FormStateInterface $form_state) {
93     $build_info = $form_state->getBuildInfo();
94     $build_info['args'][0]['parameters']['langcode'] = $form_state->getValue('langcode');
95     $form_state->setBuildInfo($build_info);
96   }
97
98 }