Commit 04966043 authored by catch's avatar catch

Issue #1296566 by Gábor Hojtsy: Improve usability of add language screen.

parent c1269e84
......@@ -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 <em>Custom language</em> section below if your desired language does not appear in this list.'),
'#default_value' => $predefined_default,
'#options' => $predefined_languages,
);
$form['predefined_submit'] = array(
'#type' => 'submit',
'#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'),
);
$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,
$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']['actions'] = array('#type' => 'actions');
$form['custom language']['actions']['submit'] = array(
_locale_languages_common_controls($form['custom_language']);
$form['custom_language']['submit'] = array(
'#type' => 'submit',
'#value' => t('Add custom language')
'#value' => t('Add custom language'),
'#validate' => array('locale_languages_add_custom_form_validate'),
'#submit' => array('locale_languages_add_custom_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';
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)));
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 <em>Add custom language</em>.'));
}
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.'));
else {
$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)));
}
}
else {
}
/**
* Validate the language addition form on custom language button.
*/
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 {
form_set_error('predefined_langcode', t('Use the <em>Add language</em> button to save a predefined language.'));
}
}
/**
* Process the language addition form submission.
* Process the custom language addition form submission.
*/
function locale_languages_predefined_form_submit($form, &$form_state) {
function locale_languages_add_custom_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 <a href="@locale-help">help screen</a>.', array('%language' => t($form_state['values']['name']), '@locale-help' => url('admin/help/locale'))));
}
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 <a href="@locale-help">help screen</a>.', array('%language' => t($predefined[$langcode][0]), '@locale-help' => url('admin/help/locale'))));
}
// 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 <a href="@locale-help">help screen</a>.', 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 <a href="@locale-help">help screen</a>.', 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';
}
/**
......
......@@ -42,7 +42,7 @@ function locale_help($path, $arg) {
return $output;
case 'admin/config/regional/language/add':
return '<p>' . t('Add a language to be supported by your site. If your desired language is not available in the <em>Language name</em> drop-down, click <em>Custom language</em> 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.') . '</p>';
return '<p>' . t('Add a language to be supported by your site. If your desired language is not available, pick <em>Custom language...</em> at the end and provide a language code and other details manually.') . '</p>';
case 'admin/config/regional/language/configure':
$output = '<p>' . 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.") . '</p>';
......@@ -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,
......
......@@ -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'));
}
/**
......
......@@ -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.
......
......@@ -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.'));
......
......@@ -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,
......
......@@ -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.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment