Commit 13830d02 authored by catch's avatar catch
Browse files

Issue #1301040 by Gábor Hojtsy: Move language listing functionality from...

Issue #1301040 by Gábor Hojtsy: Move language listing functionality from locale.module to a new language.module.
parent 37edfd76
......@@ -2686,8 +2686,8 @@ function language_list($field = 'language') {
// Init language list
if (!isset($languages)) {
$default = language_default();
if (drupal_multilingual() || module_exists('locale')) {
$languages['language'] = db_query('SELECT * FROM {languages} ORDER BY weight ASC, name ASC')->fetchAllAssoc('language');
if (drupal_multilingual() || module_exists('language')) {
$languages['language'] = db_query('SELECT * FROM {language} ORDER BY weight ASC, name ASC')->fetchAllAssoc('language');
}
else {
// No locale module, so use the default language only.
......
......@@ -1146,7 +1146,7 @@ function install_find_translations() {
foreach ($files as $key => $file) {
// Strip off the file name component before the language code.
$files[$key]->langcode = preg_replace('!^(.+\.)?([^\.]+)$!', '\2', $file->name);
// Language codes cannot exceed 12 characters to fit into the {languages}
// Language codes cannot exceed 12 characters to fit into the {language}
// table.
if (strlen($files[$key]->langcode) > 12) {
unset($files[$key]);
......@@ -1432,7 +1432,7 @@ function install_import_translations(&$install_state) {
'name' => $langcode,
'default' => TRUE,
);
locale_language_save($language);
language_save($language);
}
else {
// A known predefined language, details will be filled in properly.
......@@ -1440,7 +1440,7 @@ function install_import_translations(&$install_state) {
'language' => $langcode,
'default' => TRUE,
);
locale_language_save($language);
language_save($language);
}
// Collect files to import for this language.
......
......@@ -534,63 +534,6 @@ function locale_string_is_safe($string) {
return decode_entities($string) == decode_entities(filter_xss($string, array('a', 'abbr', 'acronym', 'address', 'b', 'bdo', 'big', 'blockquote', 'br', 'caption', 'cite', 'code', 'col', 'colgroup', 'dd', 'del', 'dfn', 'dl', 'dt', 'em', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'i', 'ins', 'kbd', 'li', 'ol', 'p', 'pre', 'q', 'samp', 'small', 'span', 'strong', 'sub', 'sup', 'table', 'tbody', 'td', 'tfoot', 'th', 'thead', 'tr', 'tt', 'ul', 'var')));
}
/**
* API function to add or update a language.
*
* @param $language
* Language object with properties corresponding to 'languages' table columns.
*/
function locale_language_save($language) {
$language->is_new = !(bool) db_query_range('SELECT 1 FROM {languages} WHERE language = :language', 0, 1, array(':language' => $language->language))->fetchField();
// If name was not set, we add a predefined language.
if (!isset($language->name)) {
include_once DRUPAL_ROOT . '/core/includes/standard.inc';
$predefined = standard_language_list();
$language->name = $predefined[$language->language][0];
$language->direction = isset($predefined[$language->language][2]) ? $predefined[$language->language][2] : LANGUAGE_LTR;
}
// Set to enabled for the default language and unless specified otherwise.
if (!empty($language->default) || !isset($language->enabled)) {
$language->enabled = TRUE;
}
// Let other modules modify $language before saved.
module_invoke_all('locale_language_presave', $language);
// Save the record and inform others about the change.
if ($language->is_new) {
drupal_write_record('languages', $language);
module_invoke_all('locale_language_insert', $language);
watchdog('locale', 'The %language (%langcode) language has been created.', array('%language' => $language->name, '%langcode' => $language->language));
}
else {
drupal_write_record('languages', $language, array('language'));
module_invoke_all('locale_language_update', $language);
watchdog('locale', 'The %language (%langcode) language has been updated.', array('%language' => $language->name, '%langcode' => $language->language));
}
if (!empty($language->default)) {
// Set the new version of this language as default in a variable.
$default_language = language_default();
variable_set('language_default', $language);
}
// Update language count based on enabled language count.
variable_set('language_count', db_query('SELECT COUNT(language) FROM {languages} WHERE enabled = 1')->fetchField());
// Kill the static cache in language_list().
drupal_static_reset('language_list');
// @todo move these two cache clears out. See http://drupal.org/node/1293252
// Changing the language settings impacts the interface.
cache_clear_all('*', 'cache_page', TRUE);
// Force JavaScript translation file re-creation for the modified language.
_locale_invalidate_js($language->language);
return $language;
}
/**
* Parses a JavaScript file, extracts strings wrapped in Drupal.t() and
* Drupal.formatPlural() and inserts them into the database.
......@@ -865,34 +808,6 @@ function _locale_rebuild_js($langcode = NULL) {
}
}
/**
* @defgroup locale-api-predefined List of predefined languages
* @{
* API to provide a list of predefined languages.
*/
/**
* Prepares the language code list for a select form item with only the unsupported ones
*/
function _locale_prepare_predefined_list() {
include_once DRUPAL_ROOT . '/core/includes/standard.inc';
$languages = language_list();
$predefined = standard_language_list();
foreach ($predefined as $key => $value) {
if (isset($languages[$key])) {
unset($predefined[$key]);
continue;
}
$predefined[$key] = t($value[0]);
}
asort($predefined);
return $predefined;
}
/**
* @} End of "locale-api-languages-predefined"
*/
/**
* Get list of all predefined and custom countries.
*
......
......@@ -100,6 +100,91 @@ function update_prepare_d8_bootstrap() {
'description' => $has_required_schema ? '' : 'Please update your Drupal 7 installation to the most recent version before attempting to upgrade to Drupal 8',
),
);
if ($has_required_schema) {
// Update the environment for the language bootstrap if needed.
update_prepare_d8_language();
}
}
}
/**
* Prepare Drupal 8 language changes for the bootstrap if needed.
*/
function update_prepare_d8_language() {
if (db_table_exists('languages')) {
$languages = db_select('languages', 'l')
->fields('l')
->execute();
$plurals = array();
$javascript = array();
$prefixes = array();
$domains = array();
foreach ($languages as $language) {
$plurals[$language->language] = array(
'plurals' => $language->plurals,
'formula' => $language->formula,
);
$javascript[$language->language] = $language->javascript;
$prefixes[$language->language] = $language->prefix;
$domains[$language->language] = $language->domain;
}
variable_set('locale_translation_plurals', $plurals);
variable_set('locale_translation_javascript', $javascript);
variable_set('locale_language_negotiation_url_prefixes', $prefixes);
variable_set('locale_language_negotiation_url_domains', $domains);
// Drop now unneeded columns.
db_drop_field('languages', 'plurals');
db_drop_field('languages', 'formula');
db_drop_field('languages', 'javascript');
db_drop_field('languages', 'prefix');
db_drop_field('languages', 'domain');
db_drop_field('languages', 'native');
// Rename the languages table to language.
db_rename_table('languages', 'language');
// Finally install/enable the language module. We need to use the update
// specific version of this function to ensure schema conflicts don't happen
// due to our updated data.
$modules = array('language');
update_module_add_to_system($modules);
update_module_enable($modules);
}
}
/**
* Adds modules to the system table in a Drupal core update.
*
* @param $modules
* Array of module names.
*/
function update_module_add_to_system($modules = array()) {
// Insert module data, so we can enable the module. Calling a full module
// list rebuild so early is costly and complex, so we just have a stop-gap.
$info_defaults = array(
'dependencies' => array(),
'description' => '',
'package' => 'Other',
'version' => NULL,
'php' => DRUPAL_MINIMUM_PHP,
'files' => array(),
'bootstrap' => 0,
);
foreach ($modules as $module) {
$module_info = drupal_parse_info_file('core/modules/' . $module . '/' . $module . '.info');
db_insert('system')
->fields(array(
'filename' => 'core/modules/' . $module . '/' . $module . '.module',
'name' => $module,
'type' => 'module',
'status' => 0,
'bootstrap' => 0,
'schema_version' => -1,
'weight' => 0,
'info' => serialize($module_info + $info_defaults),
))
->execute();
}
}
......@@ -145,6 +230,9 @@ function update_module_enable(array $modules) {
->fields(array('schema_version' => 0, 'status' => 1))
->execute();
// system_list_reset() is in module.inc but that would only be available
// once the variable bootstrap is done.
require_once DRUPAL_ROOT . '/core/includes/module.inc';
system_list_reset();
// @todo: figure out what to do about hook_install() and hook_enable().
}
......
......@@ -168,7 +168,7 @@ function comment_schema() {
'description' => "The comment author's home page address from the comment form, if user is anonymous, and the 'Anonymous users may/must leave their contact information' setting is turned on.",
),
'language' => array(
'description' => 'The {languages}.language of this comment.',
'description' => 'The {language}.language of this comment.',
'type' => 'varchar',
'length' => 12,
'not null' => TRUE,
......
......@@ -63,23 +63,23 @@
*/
/**
* Implements hook_locale_language_insert().
* Implements hook_language_insert().
*/
function field_locale_language_insert() {
function field_language_insert() {
field_info_cache_clear();
}
/**
* Implements hook_locale_language_update().
* Implements hook_language_update().
*/
function field_locale_language_update() {
function field_language_update() {
field_info_cache_clear();
}
/**
* Implements hook_locale_language_delete().
* Implements hook_language_delete().
*/
function field_locale_language_delete() {
function field_language_delete() {
field_info_cache_clear();
}
......
......@@ -2648,7 +2648,7 @@ class FieldTranslationsTestCase extends FieldTestCase {
'language' => 'l' . $i,
'name' => $this->randomString(),
);
locale_language_save($language);
language_save($language);
}
}
......
<?php
/**
* @file
* Administration functions for language.module.
*/
/**
* User interface for the language overview screen.
*/
function language_admin_overview_form($form, &$form_state) {
drupal_static_reset('language_list');
$languages = language_list('language');
$default = language_default();
$form['languages'] = array(
'#languages' => $languages,
'#language_default' => $default,
'#tree' => TRUE,
'#header' => array(
t('Name'),
t('Enabled'),
t('Default'),
t('Weight'),
t('Operations'),
),
'#theme' => 'language_admin_overview_form_table',
);
foreach ($languages as $langcode => $language) {
$form['languages'][$langcode]['#weight'] = $language->weight;
$form['languages'][$langcode]['name'] = array(
'#markup' => check_plain($language->name),
);
$form['languages'][$langcode]['enabled'] = array(
'#type' => 'checkbox',
'#title' => t('Enable @title', array('@title' => $language->name)),
'#title_display' => 'invisible',
'#default_value' => (int) $language->enabled,
'#disabled' => $langcode == $default->language,
);
$form['languages'][$langcode]['default'] = array(
'#type' => 'radio',
'#parents' => array('site_default'),
'#title' => t('Set @title as default', array('@title' => $language->name)),
'#title_display' => 'invisible',
'#return_value' => $langcode,
'#default_value' => ($langcode == $default->language ? $langcode : NULL),
'#id' => 'edit-site-default-' . $langcode,
);
$form['languages'][$langcode]['weight'] = array(
'#type' => 'weight',
'#title' => t('Weight for @title', array('@title' => $language->name)),
'#title_display' => 'invisible',
'#default_value' => $language->weight,
'#attributes' => array(
'class' => array('language-order-weight'),
),
);
$form['languages'][$langcode]['operations'] = array(
'#theme_wrappers' => array('language_admin_operations'),
'#weight' => 100,
);
$form['languages'][$langcode]['operations']['edit'] = array(
'#type' => 'link',
'#title' => t('edit'),
'#href' => 'admin/config/regional/language/edit/' . $langcode,
);
$form['languages'][$langcode]['operations']['delete'] = array(
'#type' => 'link',
'#title' => t('delete'),
'#href' => 'admin/config/regional/language/delete/' . $langcode,
'#access' => $langcode != $default->language,
);
}
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save configuration'),
);
return $form;
}
/**
* Returns HTML for operation links in language_admin_overview_form() table.
*
* @todo Introduce #type '[table_]operations' or just simply #type 'links'.
*/
function theme_language_admin_operations($variables) {
$links = array();
foreach (element_children($variables['elements']) as $key) {
// Children are only rendered if the current user has access.
if (isset($variables['elements'][$key]['#children'])) {
$links[$key] = $variables['elements'][$key]['#children'];
}
}
// If there are links, render a link list.
if (!empty($links)) {
return theme('item_list__operations', array(
'items' => $links,
'attributes' => array('class' => array('links', 'inline')),
));
}
// Otherwise, ensure to produce no output.
return '';
}
/**
* Returns HTML for the language overview form.
*
* @param $variables
* An associative array containing:
* - form: A render element representing the form.
*
* @ingroup themeable
*/
function theme_language_admin_overview_form_table($variables) {
$form = $variables['form'];
$rows = array();
foreach (element_children($form, TRUE) as $langcode) {
$element = &$form[$langcode];
$row = array(
'class' => array('draggable'),
);
foreach (element_children($element, TRUE) as $column) {
$cell = &$element[$column];
$row['data'][] = drupal_render($cell);
}
$rows[] = $row;
}
$output = theme('table', array(
'header' => $form['#header'],
'rows' => $rows,
'attributes' => array('id' => 'language-order'),
));
$output .= drupal_render_children($form);
drupal_add_tabledrag('language-order', 'order', 'sibling', 'language-order-weight');
return $output;
}
/**
* Process language overview form submissions, updating existing languages.
*/
function language_admin_overview_form_submit($form, &$form_state) {
$languages = language_list();
$old_default = language_default();
foreach ($languages as $langcode => $language) {
$language->default = ($form_state['values']['site_default'] == $langcode);
$language->weight = $form_state['values']['languages'][$langcode]['weight'];
if ($language->default || $old_default->language == $langcode) {
// Automatically enable the default language and the language
// which was default previously (because we will not get the
// value from that disabled checkbox).
$form_state['values']['languages'][$langcode]['enabled'] = 1;
}
$language->enabled = (int) !empty($form_state['values']['languages'][$langcode]['enabled']);
language_save($language);
}
drupal_set_message(t('Configuration saved.'));
}
/**
* User interface for the language addition screen.
*/
function language_admin_add_form($form, &$form_state) {
$predefined_languages = language_admin_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' => $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('language_admin_add_predefined_form_validate'),
'#submit' => array('language_admin_add_predefined_form_submit'),
);
$form['custom_language'] = array(
'#type' => 'container',
'#states' => array(
'visible' => array(
'select#edit-predefined-langcode' => array('value' => 'custom'),
),
),
);
_language_admin_common_controls($form['custom_language']);
$form['custom_language']['submit'] = array(
'#type' => 'submit',
'#value' => t('Add custom language'),
'#validate' => array('language_admin_add_custom_form_validate'),
'#submit' => array('language_admin_add_custom_form_submit'),
);
return $form;
}
/**
* Editing screen for a particular language.
*
* @param $langcode
* Language code of the language to edit.
*/
function language_admin_edit_form($form, &$form_state, $language) {
_language_admin_common_controls($form, $language);
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save language')
);
$form['#submit'][] = 'language_admin_edit_form_submit';
$form['#validate'][] = 'language_admin_edit_form_validate';
return $form;
}
/**
* Common elements of the language addition and editing form.
*
* @param $form
* A parent form item (or empty array) to add items below.
* @param $language
* Language object to edit.
*/
function _language_admin_common_controls(&$form, $language = NULL) {
if (!is_object($language)) {
$language = new stdClass();
}
if (isset($language->language)) {
$form['langcode_view'] = array(
'#type' => 'item',
'#title' => t('Language code'),
'#markup' => $language->language
);
$form['langcode'] = array(
'#type' => 'value',
'#value' => $language->language
);
}
else {
$form['langcode'] = array(
'#type' => 'textfield',
'#title' => t('Language code'),
'#maxlength' => 12,
'#required' => TRUE,
'#default_value' => @$language->language,
'#disabled' => (isset($language->language)),
'#description' => t('Use language codes as <a href="@w3ctags">defined by the W3C</a> for interoperability. <em>Examples: "en", "en-gb" and "zh-hant".</em>', array('@w3ctags' => 'http://www.w3.org/International/articles/language-tags/')),
);
}
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Language name'),
'#maxlength' => 64,
'#default_value' => @$language->name,
'#required' => TRUE,
);
$form['direction'] = array(
'#type' => 'radios',
'#title' => t('Direction'),
'#required' => TRUE,
'#description' => t('Direction that text in this language is presented.'),
'#default_value' => @$language->direction,
'#options' => array(LANGUAGE_LTR => t('Left to right'), LANGUAGE_RTL => t('Right to left'))
);
return $form;
}
/**
* Element specific validator for the Add language button.
*/
function language_admin_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>.'));
}
else {
if (language_load($langcode)) {
form_set_error('predefined_langcode', t('The language %language (%langcode) already exists.', array('%language' => $languages[$langcode]->name, '%langcode' => $langcode)));
}
}
}
/**
* Validate the language addition form on custom language button.
*/
function language_admin_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.
language_admin_edit_form_validate($form, $form_state);
$languages = language_list();
if (isset($languages[$langcode])) {
form_set_error('langcode', t('The language %language (%langcode) already exists.', array('%language' => $languages[$langcode]->name, '%langcode' => $langcode)));
}
}
else {
form_set_error('predefined_langcode', t('Use the <em>Add language</em> button to save a predefined language.'));