Pull merge.
[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  * @internal
18  */
19 class SelectLanguageForm extends FormBase {
20
21   /**
22    * {@inheritdoc}
23    */
24   public function getFormId() {
25     return 'install_select_language_form';
26   }
27
28   /**
29    * {@inheritdoc}
30    */
31   public function buildForm(array $form, FormStateInterface $form_state, $install_state = NULL) {
32     if (count($install_state['translations']) > 1) {
33       $files = $install_state['translations'];
34     }
35     else {
36       $files = [];
37     }
38     $standard_languages = LanguageManager::getStandardLanguageList();
39     $select_options = [];
40     $browser_options = [];
41
42     $form['#title'] = 'Choose language';
43
44     // Build a select list with language names in native language for the user
45     // to choose from. And build a list of available languages for the browser
46     // to select the language default from.
47     // Select lists based on all standard languages.
48     foreach ($standard_languages as $langcode => $language_names) {
49       $select_options[$langcode] = $language_names[1];
50       $browser_options[$langcode] = $langcode;
51     }
52     // Add languages based on language files in the translations directory.
53     if (count($files)) {
54       foreach ($files as $langcode => $uri) {
55         $select_options[$langcode] = isset($standard_languages[$langcode]) ? $standard_languages[$langcode][1] : $langcode;
56         $browser_options[$langcode] = $langcode;
57       }
58     }
59     asort($select_options);
60     $request = Request::createFromGlobals();
61     $browser_langcode = UserAgent::getBestMatchingLangcode($request->server->get('HTTP_ACCEPT_LANGUAGE'), $browser_options);
62     $form['langcode'] = [
63       '#type' => 'select',
64       '#title' => 'Choose language',
65       '#title_display' => 'invisible',
66       '#options' => $select_options,
67       // Use the browser detected language as default or English if nothing found.
68       '#default_value' => !empty($browser_langcode) ? $browser_langcode : 'en',
69     ];
70     $link_to_english = install_full_redirect_url(['parameters' => ['langcode' => 'en']]);
71     $form['help'] = [
72       '#type' => 'item',
73       // #markup is XSS admin filtered which ensures unsafe protocols will be
74       // removed from the url.
75       '#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>',
76       '#states' => [
77         'invisible' => [
78           'select[name="langcode"]' => ['value' => 'en'],
79         ],
80       ],
81     ];
82     $form['actions'] = ['#type' => 'actions'];
83     $form['actions']['submit'] = [
84       '#type' => 'submit',
85       '#value' => 'Save and continue',
86       '#button_type' => 'primary',
87     ];
88     return $form;
89   }
90
91   /**
92    * {@inheritdoc}
93    */
94   public function submitForm(array &$form, FormStateInterface $form_state) {
95     $build_info = $form_state->getBuildInfo();
96     $build_info['args'][0]['parameters']['langcode'] = $form_state->getValue('langcode');
97     $form_state->setBuildInfo($build_info);
98   }
99
100 }