SelectLanguageForm.php 3.4 KB

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