/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state, $install_state = NULL) { if (count($install_state['translations']) > 1) { $files = $install_state['translations']; } else { $files = array(); } $standard_languages = LanguageManager::getStandardLanguageList(); $select_options = array(); $browser_options = array(); $form['#title'] = $this->t('Choose language'); // Build a select list with language names in native language for the user // to choose from. And build a list of available languages for the browser // to select the language default from. if (count($files)) { // Select lists based on available language files. foreach ($files as $langcode => $uri) { $select_options[$langcode] = isset($standard_languages[$langcode]) ? $standard_languages[$langcode][1] : $langcode; $browser_options[] = $langcode; } } else { // Select lists based on all standard languages. foreach ($standard_languages as $langcode => $language_names) { $select_options[$langcode] = $language_names[1]; $browser_options[] = $langcode; } } $request = Request::createFromGlobals(); $browser_langcode = UserAgent::getBestMatchingLangcode($request->server->get('HTTP_ACCEPT_LANGUAGE'), $browser_options); $form['langcode'] = array('#type' => 'select', '#title' => $this->t('Choose language'), '#title_display' => 'invisible', '#options' => $select_options, '#default_value' => !empty($browser_langcode) ? $browser_langcode : 'en'); if (empty($files)) { $form['help'] = array('#type' => 'item', '#markup' => String::format('<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="!english">English</a>.</p>', array('!english' => install_full_redirect_url(array('parameters' => array('langcode' => 'en'))))), '#states' => array('invisible' => array('select[name="langcode"]' => array('value' => 'en')))); } $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array('#type' => 'submit', '#value' => $this->t('Save and continue'), '#button_type' => 'primary'); return $form; }
public function buildForm(array $form, FormStateInterface $form_state, $install_state = NULL) { if (count($install_state['translations']) > 1) { $files = $install_state['translations']; } else { $files = array(); } $standard_languages = LanguageManager::getStandardLanguageList(); $select_options = array(); $browser_options = array(); $form['#title'] = 'Choose languages'; // Build a select list with language names in native language for the user // to choose from. And build a list of available languages for the browser // to select the language default from. // Select lists based on all standard languages. foreach ($standard_languages as $langcode => $language_names) { $select_options[$langcode] = $language_names[1]; $browser_options[$langcode] = $langcode; } // Add languages based on language files in the translations directory. if (count($files)) { foreach ($files as $langcode => $uri) { $select_options[$langcode] = isset($standard_languages[$langcode]) ? $standard_languages[$langcode][1] : $langcode; $browser_options[$langcode] = $langcode; } } asort($select_options); $request = Request::createFromGlobals(); $browser_langcode = UserAgent::getBestMatchingLangcode($request->server->get('HTTP_ACCEPT_LANGUAGE'), $browser_options); $form['langcode'] = array( '#type' => 'select', '#title' => 'Choose default language', '#title_display' => 'before', '#options' => $select_options, // Use the browser detected language as default or English if nothing found. '#default_value' => !empty($browser_langcode) ? $browser_langcode : 'en', ); $link_to_english = install_full_redirect_url(array('parameters' => array('langcode' => 'en'))); $form['help'] = array( '#type' => 'item', // #markup is XSS admin filtered which ensures unsafe protocols will be // removed from the url. '#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>', '#states' => array( 'invisible' => array( 'select[name="langcode"]' => array('value' => 'en'), ), ), ); $form['langcodes'] = array( '#type' => 'select', '#title' => 'Choose another languages', '#title_display' => 'before', '#options' => $select_options, '#multiple' => TRUE, '#description' => 'Select another languages if your site is multilingual', ); $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array( '#type' => 'submit', '#value' => 'Save and continue', '#button_type' => 'primary', ); return $form; }
/** * Run an individual installation task. * * @param $task * An array of information about the task to be run. * @param $install_state * An array of information about the current installation state. This is * passed in by reference so that it can be modified by the task. * @return * The output of the task function, if there is any. */ function install_run_task($task, &$install_state) { $function = $task['function']; if ($task['type'] == 'form') { require_once DRUPAL_ROOT . '/includes/form.inc'; if ($install_state['interactive']) { // For interactive forms, build the form and ensure that it will not // redirect, since the installer handles its own redirection only after // marking the form submission task complete. $form_state = array('args' => array(&$install_state), 'no_redirect' => TRUE); $form = drupal_build_form($function, $form_state); // If a successful form submission did not occur, the form needs to be // rendered, which means the task is not complete yet. if (empty($form_state['executed'])) { $install_state['task_not_complete'] = TRUE; return drupal_render($form); } // Otherwise, return nothing so the next task will run in the same // request. return; } else { // For non-interactive forms, submit the form programmatically with the // values taken from the installation state. Throw an exception if any // errors were encountered. $form_state = array('values' => !empty($install_state['forms'][$function]) ? $install_state['forms'][$function] : array()); drupal_form_submit($function, $form_state, $install_state); $errors = form_get_errors(); if (!empty($errors)) { throw new Exception(implode("\n", $errors)); } } } elseif ($task['type'] == 'batch') { // Start a new batch based on the task function, if one is not running // already. $current_batch = variable_get('install_current_batch'); if (!$install_state['interactive'] || !$current_batch) { $batch = $function($install_state); if (empty($batch)) { // If the task did some processing and decided no batch was necessary, // there is nothing more to do here. return; } batch_set($batch); // For interactive batches, we need to store the fact that this batch // task is currently running. Otherwise, we need to make sure the batch // will complete in one page request. if ($install_state['interactive']) { variable_set('install_current_batch', $function); } else { $batch =& batch_get(); $batch['progressive'] = FALSE; } // Process the batch. For progressive batches, this will redirect. // Otherwise, the batch will complete. batch_process(install_redirect_url($install_state), install_full_redirect_url($install_state)); } elseif ($current_batch == $function) { include_once DRUPAL_ROOT . '/includes/batch.inc'; $output = _batch_page(); // The task is complete when we try to access the batch page and receive // FALSE in return, since this means we are at a URL where we are no // longer requesting a batch ID. if ($output === FALSE) { // Return nothing so the next task will run in the same request. variable_del('install_current_batch'); return; } else { // We need to force the page request to end if the task is not // complete, since the batch API sometimes prints JSON output // rather than returning a themed page. $install_state['task_not_complete'] = $install_state['stop_page_request'] = TRUE; return $output; } } } else { // For normal tasks, just return the function result, whatever it is. return $function($install_state); } }