From 0496604301936ee6307240f646f7fa7f4464f9e2 Mon Sep 17 00:00:00 2001 From: catch Date: Thu, 20 Oct 2011 23:00:41 +0900 Subject: [PATCH] =?UTF-8?q?Issue=20#1296566=20by=20G=C3=A1bor=20Hojtsy:=20?= =?UTF-8?q?Improve=20usability=20of=20add=20language=20screen.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/locale/locale.admin.inc | 176 +++++++++++++++------------ modules/locale/locale.module | 5 +- modules/locale/locale.test | 27 ++-- modules/path/path.test | 6 +- modules/search/search.test | 2 +- modules/simpletest/tests/common.test | 1 + modules/translation/translation.test | 2 +- 7 files changed, 124 insertions(+), 95 deletions(-) diff --git a/modules/locale/locale.admin.inc b/modules/locale/locale.admin.inc index d5d4e6c07ca..8a20bf95e48 100644 --- a/modules/locale/locale.admin.inc +++ b/modules/locale/locale.admin.inc @@ -191,50 +191,45 @@ function locale_language_overview_form_submit($form, &$form_state) { /** * User interface for the language addition screen. */ -function locale_languages_add_screen() { - $build['predefined'] = drupal_get_form('locale_languages_predefined_form'); - $build['custom'] = drupal_get_form('locale_languages_custom_form'); - return $build; -} - -/** - * Predefined language setup form. - */ -function locale_languages_predefined_form($form) { - $predefined = _locale_prepare_predefined_list(); - $form['language list'] = array('#type' => 'fieldset', - '#title' => t('Predefined language'), - '#collapsible' => TRUE, - ); - $form['language list']['langcode'] = array('#type' => 'select', +function locale_languages_add_form($form, &$form_state) { + $predefined_languages = _locale_prepare_predefined_list(); + $predefined_languages['custom'] = t('Custom language...'); + $predefined_default = !empty($form_state['values']['predefined_langcode']) ? $form_state['values']['predefined_langcode'] : key($predefined_languages); + $form['predefined_langcode'] = array( + '#type' => 'select', '#title' => t('Language name'), - '#default_value' => key($predefined), - '#options' => $predefined, - '#description' => t('Use the Custom language section below if your desired language does not appear in this list.'), + '#default_value' => $predefined_default, + '#options' => $predefined_languages, ); - $form['language list']['actions'] = array('#type' => 'actions'); - $form['language list']['actions']['submit'] = array('#type' => 'submit', '#value' => t('Add language')); - return $form; -} - -/** - * Custom language addition form. - */ -function locale_languages_custom_form($form) { - $form['custom language'] = array('#type' => 'fieldset', - '#title' => t('Custom language'), - '#collapsible' => TRUE, - '#collapsed' => TRUE, - ); - _locale_languages_common_controls($form['custom language']); - $form['custom language']['actions'] = array('#type' => 'actions'); - $form['custom language']['actions']['submit'] = array( + $form['predefined_submit'] = array( '#type' => 'submit', - '#value' => t('Add custom language') + '#value' => t('Add language'), + '#limit_validation_errors' => array(array('predefined_langcode'), array('predefined_submit')), + '#states' => array( + 'invisible' => array( + 'select#edit-predefined-langcode' => array('value' => 'custom'), + ), + ), + '#validate' => array('locale_languages_add_predefined_form_validate'), + '#submit' => array('locale_languages_add_predefined_form_submit'), ); - // Reuse the validation and submit functions of the predefined language setup form. - $form['#submit'][] = 'locale_languages_predefined_form_submit'; - $form['#validate'][] = 'locale_languages_predefined_form_validate'; + + $form['custom_language'] = array( + '#type' => 'container', + '#states' => array( + 'visible' => array( + 'select#edit-predefined-langcode' => array('value' => 'custom'), + ), + ), + ); + _locale_languages_common_controls($form['custom_language']); + $form['custom_language']['submit'] = array( + '#type' => 'submit', + '#value' => t('Add custom language'), + '#validate' => array('locale_languages_add_custom_form_validate'), + '#submit' => array('locale_languages_add_custom_form_submit'), + ); + return $form; } @@ -321,64 +316,85 @@ function _locale_languages_common_controls(&$form, $language = NULL) { } /** - * Validate the language addition form. + * Element specific validator for the Add language button. */ -function locale_languages_predefined_form_validate($form, &$form_state) { - $langcode = $form_state['values']['langcode']; - - if (($duplicate = db_query("SELECT COUNT(*) FROM {languages} WHERE language = :language", array(':language' => $langcode))->fetchField()) != 0) { - form_set_error('langcode', t('The language %language (%code) already exists.', array('%language' => $form_state['values']['name'], '%code' => $langcode))); - } - - if (!isset($form_state['values']['name'])) { - // Predefined language selection. - include_once DRUPAL_ROOT . '/includes/standard.inc'; - $predefined = standard_language_list(); - if (!isset($predefined[$langcode])) { - form_set_error('langcode', t('Invalid language code.')); - } +function locale_languages_add_predefined_form_validate($form, &$form_state) { + $langcode = $form_state['values']['predefined_langcode']; + if ($langcode == 'custom') { + form_set_error('predefined_langcode', t('Fill in the language details and save the language with Add custom language.')); } else { - // Reuse the editing form validation routine if we add a custom language. - locale_languages_edit_form_validate($form, $form_state); + $languages = language_list(); + if (isset($languages[$langcode])) { + form_set_error('predefined_langcode', t('The language %language (%code) already exists.', array('%language' => $languages[$langcode]->name, '%code' => $langcode))); + } } } /** - * Process the language addition form submission. + * Validate the language addition form on custom language button. */ -function locale_languages_predefined_form_submit($form, &$form_state) { - $langcode = $form_state['values']['langcode']; - if (isset($form_state['values']['name'])) { - // Custom language form. - $language = (object) array( - 'language' => $langcode, - 'name' => $form_state['values']['name'], - 'native' => $form_state['values']['native'], - 'direction' => $form_state['values']['direction'], - ); - locale_language_save($language); - drupal_set_message(t('The language %language has been created and can now be used. More information is available on the help screen.', array('%language' => t($form_state['values']['name']), '@locale-help' => url('admin/help/locale')))); +function locale_languages_add_custom_form_validate($form, &$form_state) { + if ($form_state['values']['predefined_langcode'] == 'custom') { + $langcode = $form_state['values']['langcode']; + // Reuse the editing form validation routine if we add a custom language. + locale_languages_edit_form_validate($form, $form_state); + + $languages = language_list(); + if (isset($languages[$langcode])) { + form_set_error('langcode', t('The language %language (%code) already exists.', array('%language' => $languages[$langcode]->name, '%code' => $langcode))); + } } else { - // Predefined language selection. - include_once DRUPAL_ROOT . '/includes/standard.inc'; - $predefined = standard_language_list(); - $language = (object) array( - 'language' => $langcode, - ); - locale_language_save($language); - drupal_set_message(t('The language %language has been created and can now be used. More information is available on the help screen.', array('%language' => t($predefined[$langcode][0]), '@locale-help' => url('admin/help/locale')))); + form_set_error('predefined_langcode', t('Use the Add language button to save a predefined language.')); } +} +/** + * Process the custom language addition form submission. + */ +function locale_languages_add_custom_form_submit($form, &$form_state) { + $langcode = $form_state['values']['langcode']; + // Custom language form. + $language = (object) array( + 'language' => $langcode, + 'name' => $form_state['values']['name'], + 'native' => $form_state['values']['native'], + 'direction' => $form_state['values']['direction'], + ); + locale_language_save($language); + drupal_set_message(t('The language %language has been created and can now be used. More information is available on the help screen.', array('%language' => t($form_state['values']['name']), '@locale-help' => url('admin/help/locale')))); + locale_languages_add_set_batch($langcode); + $form_state['redirect'] = 'admin/config/regional/language'; +} + +/** + * Process the predefined language addition form submission. + */ +function locale_languages_add_predefined_form_submit($form, &$form_state) { + // Predefined language selection. + $langcode = $form_state['values']['predefined_langcode']; + include_once DRUPAL_ROOT . '/includes/standard.inc'; + $predefined = standard_language_list(); + $language = (object) array( + 'language' => $langcode, + ); + locale_language_save($language); + drupal_set_message(t('The language %language has been created and can now be used. More information is available on the help screen.', array('%language' => t($predefined[$langcode][0]), '@locale-help' => url('admin/help/locale')))); + locale_languages_add_set_batch($langcode); + $form_state['redirect'] = 'admin/config/regional/language'; +} + +/** + * Set a batch for newly added language. + */ +function locale_languages_add_set_batch($langcode) { // See if we have language files to import for the newly added // language, collect and import them. include_once drupal_get_path('module', 'locale') . '/locale.bulk.inc'; if ($batch = locale_batch_by_language($langcode, '_locale_batch_language_finished')) { batch_set($batch); } - - $form_state['redirect'] = 'admin/config/regional/language'; } /** diff --git a/modules/locale/locale.module b/modules/locale/locale.module index e291a2bd9ad..e3288da774d 100644 --- a/modules/locale/locale.module +++ b/modules/locale/locale.module @@ -42,7 +42,7 @@ function locale_help($path, $arg) { return $output; case 'admin/config/regional/language/add': - return '

' . t('Add a language to be supported by your site. If your desired language is not available in the Language name drop-down, click Custom language and provide a language code and other details manually. When providing a language code manually, be sure to enter a standardized language code, since this code may be used by browsers to determine an appropriate display language.') . '

'; + return '

' . t('Add a language to be supported by your site. If your desired language is not available, pick Custom language... at the end and provide a language code and other details manually.') . '

'; case 'admin/config/regional/language/configure': $output = '

' . t("Define how to decide which language is used to display page elements (primarily text provided by Drupal and modules, such as field labels and help text). This decision is made by evaluating a series of detection methods for languages; the first detection method that gets a result will determine which language is used for that type of text. Define the order of evaluation of language detection methods on this page.") . '

'; @@ -93,7 +93,8 @@ function locale_menu() { ); $items['admin/config/regional/language/add'] = array( 'title' => 'Add language', - 'page callback' => 'locale_languages_add_screen', // two forms concatenated + 'page callback' => 'drupal_get_form', + 'page arguments' => array('locale_languages_add_form'), 'access arguments' => array('administer languages'), 'weight' => 5, 'type' => MENU_LOCAL_ACTION, diff --git a/modules/locale/locale.test b/modules/locale/locale.test index 5da62cf038a..b94f5653622 100644 --- a/modules/locale/locale.test +++ b/modules/locale/locale.test @@ -49,7 +49,7 @@ class LocaleConfigurationTest extends DrupalWebTestCase { // Add predefined language. $edit = array( - 'langcode' => 'fr', + 'predefined_langcode' => 'fr', ); $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language')); $this->assertText('fr', t('Language added successfully.')); @@ -63,6 +63,7 @@ class LocaleConfigurationTest extends DrupalWebTestCase { // The native name for the language. $native = $this->randomName(16); $edit = array( + 'predefined_langcode' => 'custom', 'langcode' => $langcode, 'name' => $name, 'native' => $native, @@ -282,6 +283,7 @@ class LocaleTranslationFunctionalTest extends DrupalWebTestCase { // Add custom language. $this->drupalLogin($admin_user); $edit = array( + 'predefined_langcode' => 'custom', 'langcode' => $langcode, 'name' => $name, 'native' => $native, @@ -391,6 +393,7 @@ class LocaleTranslationFunctionalTest extends DrupalWebTestCase { // Add custom language. $edit = array( + 'predefined_langcode' => 'custom', 'langcode' => $langcode, 'name' => $name, 'native' => $native, @@ -462,6 +465,7 @@ class LocaleTranslationFunctionalTest extends DrupalWebTestCase { // Add custom language. $edit = array( + 'predefined_langcode' => 'custom', 'langcode' => $langcode, 'name' => $name, 'native' => $native, @@ -519,6 +523,7 @@ class LocaleTranslationFunctionalTest extends DrupalWebTestCase { // Add custom language. $this->drupalLogin($admin_user); $edit = array( + 'predefined_langcode' => 'custom', 'langcode' => $langcode, 'name' => $name, 'native' => $native, @@ -759,6 +764,7 @@ class LocaleImportFunctionalTest extends DrupalWebTestCase { // Create a custom language. $edit = array( + 'predefined_langcode' => 'custom', 'langcode' => $langcode, 'name' => $name, 'native' => $native, @@ -1295,7 +1301,7 @@ class LocaleLanguageSwitchingFunctionalTest extends DrupalWebTestCase { // Add language. $edit = array( - 'langcode' => 'fr', + 'predefined_langcode' => 'fr', ); $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language')); @@ -1494,6 +1500,7 @@ class LocaleUserLanguageFunctionalTest extends DrupalWebTestCase { // The native name for the language. $native = $this->randomName(16); $edit = array( + 'predefined_langcode' => 'custom', 'langcode' => $langcode, 'name' => $name, 'native' => $native, @@ -1509,6 +1516,7 @@ class LocaleUserLanguageFunctionalTest extends DrupalWebTestCase { // The native name for the language. $native_disabled = $this->randomName(16); $edit = array( + 'predefined_langcode' => 'custom', 'langcode' => $langcode_disabled, 'name' => $name_disabled, 'native' => $native_disabled, @@ -1576,7 +1584,7 @@ class LocaleUserCreationTest extends DrupalWebTestCase { // Add predefined language. $langcode = 'fr'; $edit = array( - 'langcode' => 'fr', + 'predefined_langcode' => 'fr', ); $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language')); $this->assertText($langcode, t('Language added successfully.')); @@ -1685,6 +1693,7 @@ class LocalePathFunctionalTest extends DrupalWebTestCase { // The domain prefix. $prefix = $langcode; $edit = array( + 'predefined_langcode' => 'custom', 'langcode' => $langcode, 'name' => $name, 'native' => $native, @@ -1829,6 +1838,7 @@ class LocaleContentFunctionalTest extends DrupalWebTestCase { // The native name for the language. $native = $this->randomName(16); $edit = array( + 'predefined_langcode' => 'custom', 'langcode' => $langcode, 'name' => $name, 'native' => $native, @@ -1844,6 +1854,7 @@ class LocaleContentFunctionalTest extends DrupalWebTestCase { // The native name for the language. $native_disabled = $this->randomName(16); $edit = array( + 'predefined_langcode' => 'custom', 'langcode' => $langcode_disabled, 'name' => $name_disabled, 'native' => $native_disabled, @@ -2183,12 +2194,12 @@ class LocaleUrlRewritingTest extends DrupalWebTestCase { // Install French language. $edit = array(); - $edit['langcode'] = 'fr'; + $edit['predefined_langcode'] = 'fr'; $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language')); // Install Italian language. $edit = array(); - $edit['langcode'] = 'it'; + $edit['predefined_langcode'] = 'it'; $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language')); // Disable Italian language. @@ -2387,7 +2398,7 @@ class LocaleCommentLanguageFunctionalTest extends DrupalWebTestCase { $this->drupalLogin($admin_user); // Add language. - $edit = array('langcode' => 'fr'); + $edit = array('predefined_langcode' => 'fr'); $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language')); // Set "Article" content type to use multilingual support. @@ -2485,7 +2496,7 @@ class LocaleDateFormatsFunctionalTest extends DrupalWebTestCase { function testLocalizeDateFormats() { // Add language. $edit = array( - 'langcode' => 'fr', + 'predefined_langcode' => 'fr', ); $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language')); @@ -2545,7 +2556,7 @@ class LocaleLanguageNegotiationInfoFunctionalTest extends DrupalWebTestCase { require_once DRUPAL_ROOT .'/includes/language.inc'; $admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages', 'view the administration theme')); $this->drupalLogin($admin_user); - $this->drupalPost('admin/config/regional/language/add', array('langcode' => 'it'), t('Add language')); + $this->drupalPost('admin/config/regional/language/add', array('predefined_langcode' => 'it'), t('Add language')); } /** diff --git a/modules/path/path.test b/modules/path/path.test index bf05d5ba3cb..8f0406ef804 100644 --- a/modules/path/path.test +++ b/modules/path/path.test @@ -246,7 +246,7 @@ class PathLanguageTestCase extends DrupalWebTestCase { // Enable French language. $edit = array(); - $edit['langcode'] = 'fr'; + $edit['predefined_langcode'] = 'fr'; $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language')); @@ -387,7 +387,7 @@ class PathLanguageUITestCase extends DrupalWebTestCase { // Enable French language. $edit = array(); - $edit['langcode'] = 'fr'; + $edit['predefined_langcode'] = 'fr'; $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language')); @@ -464,7 +464,7 @@ class PathMonolingualTestCase extends DrupalWebTestCase { // Enable French language. $edit = array(); - $edit['langcode'] = 'fr'; + $edit['predefined_langcode'] = 'fr'; $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language')); // Make French the default language. diff --git a/modules/search/search.test b/modules/search/search.test index ef73a302a44..f018d2a4ffe 100644 --- a/modules/search/search.test +++ b/modules/search/search.test @@ -1912,7 +1912,7 @@ class SearchLanguageTestCase extends DrupalWebTestCase { $this->assertNoText(t('Languages'), t('No languages to choose from.')); // Add predefined language. - $edit = array('langcode' => 'fr'); + $edit = array('predefined_langcode' => 'fr'); $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language')); $this->assertText('fr', t('Language added successfully.')); diff --git a/modules/simpletest/tests/common.test b/modules/simpletest/tests/common.test index 435811c9434..9b70bf1cc29 100644 --- a/modules/simpletest/tests/common.test +++ b/modules/simpletest/tests/common.test @@ -2246,6 +2246,7 @@ class FormatDateUnitTest extends DrupalWebTestCase { $admin_user = $this->drupalCreateUser(array('administer languages')); $this->drupalLogin($admin_user); $edit = array( + 'predefined_langcode' => 'custom', 'langcode' => self::LANGCODE, 'name' => self::LANGCODE, 'native' => self::LANGCODE, diff --git a/modules/translation/translation.test b/modules/translation/translation.test index 9beaba7478a..b1ea968f68e 100644 --- a/modules/translation/translation.test +++ b/modules/translation/translation.test @@ -281,7 +281,7 @@ class TranslationTestCase extends DrupalWebTestCase { if (strpos($this->drupalGetContent(), 'languages[' . $language_code . '][enabled]') === FALSE) { // Doesn't have language installed so add it. $edit = array(); - $edit['langcode'] = $language_code; + $edit['predefined_langcode'] = $language_code; $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language')); // Make sure we are not using a stale list.