Commit 3a3bce39 authored by Dries's avatar Dries
Browse files

- Patch #1649400 by vasi1186, Gábor Hojtsy: make locked/special languages fully extensible.

parent 83a9b89a
...@@ -204,9 +204,19 @@ ...@@ -204,9 +204,19 @@
const LANGUAGE_MULTIPLE = 'mul'; const LANGUAGE_MULTIPLE = 'mul';
/** /**
* The language code used when referring to all languages. * The language state when referring to configurable languages.
*/ */
const LANGUAGE_ALL = TRUE; const LANGUAGE_CONFIGURABLE = 1;
/**
* The language state when referring to locked languages.
*/
const LANGUAGE_LOCKED = 2;
/**
* The language state used when referring to all languages.
*/
const LANGUAGE_ALL = 3;
/** /**
* The type of language used to define the content language. * The type of language used to define the content language.
...@@ -2676,17 +2686,17 @@ function language_multilingual() { ...@@ -2676,17 +2686,17 @@ function language_multilingual() {
} }
/** /**
* Returns a list of configured languages. * Returns a list of languages set up on the site.
* *
* @param $all * @param $flags
* (optional) A flag depending on the need for locked languages in the * (optional) Specifies the state of the languages that have to be returned.
* returned list. * It can be: LANGUAGE_CONFIGURABLE, LANGUAGE_LOCKED, LANGUAGE_ALL.
* *
* @return * @return
* An associative array of languages, keyed by the language code, ordered by * An associative array of languages, keyed by the language code, ordered by
* weight ascending and name ascending. * weight ascending and name ascending.
*/ */
function language_list($all = FALSE) { function language_list($flags = LANGUAGE_CONFIGURABLE) {
$languages = &drupal_static(__FUNCTION__); $languages = &drupal_static(__FUNCTION__);
...@@ -2712,7 +2722,7 @@ function language_list($all = FALSE) { ...@@ -2712,7 +2722,7 @@ function language_list($all = FALSE) {
// No language module, so use the default language only. // No language module, so use the default language only.
$languages = array($default->langcode => $default); $languages = array($default->langcode => $default);
// Add the special languages, they will be filtered later if needed. // Add the special languages, they will be filtered later if needed.
$languages += language_locked_languages($default->weight); $languages += language_default_locked_languages($default->weight);
} }
} }
...@@ -2721,7 +2731,7 @@ function language_list($all = FALSE) { ...@@ -2721,7 +2731,7 @@ function language_list($all = FALSE) {
// those languages to be added as well. // those languages to be added as well.
$filtered_languages = array(); $filtered_languages = array();
foreach ($languages as $langcode => $language) { foreach ($languages as $langcode => $language) {
if ($language->locked && !$all) { if (($language->locked && !($flags & LANGUAGE_LOCKED)) || (!$language->locked && !($flags & LANGUAGE_CONFIGURABLE))) {
continue; continue;
} }
$filtered_languages[$langcode] = $language; $filtered_languages[$langcode] = $language;
...@@ -2731,7 +2741,7 @@ function language_list($all = FALSE) { ...@@ -2731,7 +2741,7 @@ function language_list($all = FALSE) {
} }
/** /**
* Returns a list with the locked languages. * Returns a list of the default locked languages.
* *
* @param int $weight * @param int $weight
* An integer value that is used as the start value for the weights of the * An integer value that is used as the start value for the weights of the
...@@ -2740,7 +2750,7 @@ function language_list($all = FALSE) { ...@@ -2740,7 +2750,7 @@ function language_list($all = FALSE) {
* @return * @return
* An array of language objects. * An array of language objects.
*/ */
function language_locked_languages($weight = 0) { function language_default_locked_languages($weight = 0) {
$locked_language = array( $locked_language = array(
'default' => FALSE, 'default' => FALSE,
'locked' => TRUE, 'locked' => TRUE,
...@@ -2810,7 +2820,8 @@ function language_name($langcode) { ...@@ -2810,7 +2820,8 @@ function language_name($langcode) {
* The language code. * The language code.
*/ */
function language_is_locked($langcode) { function language_is_locked($langcode) {
return array_key_exists($langcode, language_locked_languages()); $language = language_load($langcode);
return ($language ? $language->locked : FALSE);
} }
/** /**
......
...@@ -236,7 +236,7 @@ function update_prepare_d8_language() { ...@@ -236,7 +236,7 @@ function update_prepare_d8_language() {
db_add_field('language', 'locked', $locked_spec); db_add_field('language', 'locked', $locked_spec);
$max_language_weight = db_query('SELECT MAX(weight) FROM {language}')->fetchField(); $max_language_weight = db_query('SELECT MAX(weight) FROM {language}')->fetchField();
$languages = language_locked_languages($max_language_weight); $languages = language_default_locked_languages($max_language_weight);
foreach ($languages as $language) { foreach ($languages as $language) {
db_insert('language') db_insert('language')
->fields(array( ->fields(array(
......
...@@ -313,7 +313,7 @@ function field_language($entity_type, $entity, $field_name = NULL, $langcode = N ...@@ -313,7 +313,7 @@ function field_language($entity_type, $entity, $field_name = NULL, $langcode = N
// that language for display. If not, the default one will be // that language for display. If not, the default one will be
// LANGUAGE_NOT_SPECIFIED. // LANGUAGE_NOT_SPECIFIED.
$display_langcode[$instance['field_name']] = LANGUAGE_NOT_SPECIFIED; $display_langcode[$instance['field_name']] = LANGUAGE_NOT_SPECIFIED;
foreach (language_locked_languages() as $language_locked) { foreach (language_list(LANGUAGE_LOCKED) as $language_locked) {
if (isset($entity->{$instance['field_name']}[$language_locked->langcode])) { if (isset($entity->{$instance['field_name']}[$language_locked->langcode])) {
$display_langcode[$instance['field_name']] = $language_locked->langcode; $display_langcode[$instance['field_name']] = $language_locked->langcode;
break; break;
......
...@@ -15,7 +15,7 @@ function language_install() { ...@@ -15,7 +15,7 @@ function language_install() {
// Add the default language at first so that language_list() returns this in // Add the default language at first so that language_list() returns this in
// language_special_languages(). // language_special_languages().
$default_language = language_save(language_default()); $default_language = language_save(language_default());
$languages = language_locked_languages($default_language->weight); $languages = language_default_locked_languages($default_language->weight);
foreach ($languages as $language) { foreach ($languages as $language) {
language_save($language); language_save($language);
} }
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
namespace Drupal\language\Tests; namespace Drupal\language\Tests;
use Drupal\simpletest\WebTestBase; use Drupal\simpletest\WebTestBase;
use Drupal\Core\Language\Language;
/** /**
* Functional tests for the language list configuration forms. * Functional tests for the language list configuration forms.
...@@ -162,4 +163,27 @@ function testLanguageList() { ...@@ -162,4 +163,27 @@ function testLanguageList() {
$t_args = array('%language' => 'English', '%langcode' => 'en'); $t_args = array('%language' => 'English', '%langcode' => 'en');
$this->assertRaw(t('The %language (%langcode) language has been removed.', $t_args), t('The English language has been removed.')); $this->assertRaw(t('The %language (%langcode) language has been removed.', $t_args), t('The English language has been removed.'));
} }
/**
* Functional tests for the language states (locked or configurable).
*/
function testLanguageStates() {
// Add some languages, and also lock some of them.
language_save(new Language(array('name' => $this->randomName(), 'langcode' => 'l1')));
language_save(new Language(array('name' => $this->randomName(), 'langcode' => 'l2', 'locked' => TRUE)));
language_save(new Language(array('name' => $this->randomName(), 'langcode' => 'l3')));
language_save(new Language(array('name' => $this->randomName(), 'langcode' => 'l4', 'locked' => TRUE)));
$expected_locked_languages = array('l4' => 'l4', 'l2' => 'l2', 'und' => 'und', 'zxx' => 'zxx', 'mul' => 'mul');
$expected_all_languages = array('l4' => 'l4', 'l3' => 'l3', 'l2' => 'l2', 'l1' => 'l1', 'en' => 'en', 'und' => 'und', 'zxx' => 'zxx', 'mul' => 'mul');
$expected_conf_languages = array('l3' => 'l3', 'l1' => 'l1', 'en' => 'en');
$locked_languages = language_list(LANGUAGE_LOCKED);
$this->assertEqual(array_diff_key($expected_locked_languages, $locked_languages), array(), t('Locked languages loaded correctly.'));
$all_languages = language_list(LANGUAGE_ALL);
$this->assertEqual(array_diff_key($expected_all_languages, $all_languages), array(), t('All languages loaded correctly.'));
$conf_languages = language_list();
$this->assertEqual(array_diff_key($expected_conf_languages, $conf_languages), array(), t('Configurable languages loaded correctly.'));
}
} }
...@@ -137,7 +137,7 @@ function translation_form_node_type_form_alter(&$form, &$form_state) { ...@@ -137,7 +137,7 @@ function translation_form_node_type_form_alter(&$form, &$form_state) {
*/ */
function translation_node_type_language_translation_enabled_validate($element, &$form_state, $form) { function translation_node_type_language_translation_enabled_validate($element, &$form_state, $form) {
if (language_is_locked($form_state['values']['node_type_language_default']) && $form_state['values']['node_type_language_hidden'] && $form_state['values']['node_type_language_translation_enabled']) { if (language_is_locked($form_state['values']['node_type_language_default']) && $form_state['values']['node_type_language_hidden'] && $form_state['values']['node_type_language_translation_enabled']) {
foreach (language_locked_languages() as $language) { foreach (language_list(LANGUAGE_LOCKED) as $language) {
$locked_languages[] = $language->name; $locked_languages[] = $language->name;
} }
form_set_error('node_type_language_translation_enabled', t('Translation is not supported if language is always one of: @locked_languages', array('@locked_languages' => implode(", ", $locked_languages)))); form_set_error('node_type_language_translation_enabled', t('Translation is not supported if language is always one of: @locked_languages', array('@locked_languages' => implode(", ", $locked_languages))));
......
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