Commit 3a3bce39 authored by Dries's avatar Dries

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

parent 83a9b89a
......@@ -204,9 +204,19 @@
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.
......@@ -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
* (optional) A flag depending on the need for locked languages in the
* returned list.
* @param $flags
* (optional) Specifies the state of the languages that have to be returned.
* It can be: LANGUAGE_CONFIGURABLE, LANGUAGE_LOCKED, LANGUAGE_ALL.
*
* @return
* An associative array of languages, keyed by the language code, ordered by
* weight ascending and name ascending.
*/
function language_list($all = FALSE) {
function language_list($flags = LANGUAGE_CONFIGURABLE) {
$languages = &drupal_static(__FUNCTION__);
......@@ -2712,7 +2722,7 @@ function language_list($all = FALSE) {
// No language module, so use the default language only.
$languages = array($default->langcode => $default);
// 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) {
// those languages to be added as well.
$filtered_languages = array();
foreach ($languages as $langcode => $language) {
if ($language->locked && !$all) {
if (($language->locked && !($flags & LANGUAGE_LOCKED)) || (!$language->locked && !($flags & LANGUAGE_CONFIGURABLE))) {
continue;
}
$filtered_languages[$langcode] = $language;
......@@ -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
* An integer value that is used as the start value for the weights of the
......@@ -2740,7 +2750,7 @@ function language_list($all = FALSE) {
* @return
* An array of language objects.
*/
function language_locked_languages($weight = 0) {
function language_default_locked_languages($weight = 0) {
$locked_language = array(
'default' => FALSE,
'locked' => TRUE,
......@@ -2810,7 +2820,8 @@ function language_name($langcode) {
* The language code.
*/
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() {
db_add_field('language', 'locked', $locked_spec);
$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) {
db_insert('language')
->fields(array(
......
......@@ -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
// 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])) {
$display_langcode[$instance['field_name']] = $language_locked->langcode;
break;
......
......@@ -15,7 +15,7 @@ function language_install() {
// Add the default language at first so that language_list() returns this in
// language_special_languages().
$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) {
language_save($language);
}
......
......@@ -8,6 +8,7 @@
namespace Drupal\language\Tests;
use Drupal\simpletest\WebTestBase;
use Drupal\Core\Language\Language;
/**
* Functional tests for the language list configuration forms.
......@@ -162,4 +163,27 @@ function testLanguageList() {
$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.'));
}
/**
* 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) {
*/
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']) {
foreach (language_locked_languages() as $language) {
foreach (language_list(LANGUAGE_LOCKED) as $language) {
$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))));
......
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