Issue #1856976 by andypost, Soul88, ACF, aspilicious, vijaycs85, dawehner: Convert language_count() to the state system.

8.0.x
Alex Pott 2013-07-20 20:39:11 +01:00
parent 36d5732ff8
commit 566ebfd9de
9 changed files with 50 additions and 26 deletions

View File

@ -193,6 +193,7 @@ services:
arguments: ['@event_dispatcher', '@service_container', '@controller_resolver']
language_manager:
class: Drupal\Core\Language\LanguageManager
arguments: ['@state']
string_translator.custom_strings:
class: Drupal\Core\StringTranslation\Translator\CustomStrings
tags:

View File

@ -2430,10 +2430,7 @@ function language_types_get_default() {
* TRUE if more than one language is enabled.
*/
function language_multilingual() {
// The "language_count" variable stores the number of enabled languages to
// avoid unnecessarily querying the database when building the list of
// enabled languages on monolingual sites.
return variable_get('language_count', 1) > 1;
return Drupal::languageManager()->isMultilingual();
}
/**

View File

@ -386,7 +386,9 @@ function install_begin_request(&$install_state) {
->addArgument(new Reference('config.context'));
// Register the 'language_manager' service.
$container->register('language_manager', 'Drupal\Core\Language\LanguageManager');
$container
->register('language_manager', 'Drupal\Core\Language\LanguageManager')
->addArgument(NULL);
// Register the translation services.
install_register_translation_service($container);

View File

@ -511,7 +511,7 @@ function update_prepare_d8_language() {
db_drop_field('languages', 'enabled');
// Update language count.
variable_set('language_count', db_query('SELECT COUNT(language) FROM {languages}')->fetchField());
Drupal::state()->set('language_count', db_query('SELECT COUNT(language) FROM {languages}')->fetchField());
// Rename the languages table to language.
db_rename_table('languages', 'language');

View File

@ -8,6 +8,7 @@
namespace Drupal\Core\Language;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\KeyValueStore\KeyValueStoreInterface;
/**
* Class responsible for initializing each language type.
@ -21,6 +22,13 @@ class LanguageManager {
*/
protected $request;
/**
* The Key/Value Store to use for state.
*
* @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
*/
protected $state = NULL;
/**
* An array of language objects keyed by language type.
*
@ -45,6 +53,16 @@ class LanguageManager {
*/
protected $initializing = FALSE;
/**
* Constructs an LanguageManager object.
*
* @param \Drupal\Core\KeyValueStore\KeyValueStoreInterface $state
* The state keyvalue store.
*/
public function __construct(KeyValueStoreInterface $state = NULL) {
$this->state = $state;
}
/**
* Initializes each language type to a language object.
*/
@ -136,7 +154,11 @@ class LanguageManager {
* TRUE if more than one language is enabled, FALSE otherwise.
*/
public function isMultilingual() {
return variable_get('language_count', 1) > 1;
if (!isset($this->state)) {
// No state service in install time.
return FALSE;
}
return ($this->state->get('language_count') ?: 1) > 1;
}
/**
@ -152,7 +174,7 @@ class LanguageManager {
/**
* Returns a language object representing the site's default language.
*
* @return Drupal\Core\Language\Language
* @return \Drupal\Core\Language\Language
* A language object.
*/
protected function getLanguageDefault() {

View File

@ -29,7 +29,7 @@ function language_install() {
function language_uninstall() {
// Clear variables.
variable_del('language_default');
variable_del('language_count');
Drupal::state()->delete('language_count');
// Clear variables.
variable_del('language_types');
@ -51,7 +51,7 @@ function language_uninstall() {
*/
function language_enable() {
// Update the language count, if the module was disabled before, the
// language_count variable was forced to 1.
// language_count state was forced to 1.
language_update_count();
}
@ -59,10 +59,10 @@ function language_enable() {
* Implements hook_disable().
*/
function language_disable() {
// Force the language_count variable to be 1, so that the when checking if the
// Force the language_count state to be 1, so that the when checking if the
// site is multilingual (for example in language_multilingual()), the result
// will be FALSE, because the language module is disabled.
variable_set('language_count', 1);
Drupal::state()->set('language_count', 1);
}
/**

View File

@ -522,15 +522,15 @@ function language_save($language) {
variable_set('language_default', (array) $language);
}
// Kill the static cache in language_list().
drupal_static_reset('language_list');
// Update language count based on unlocked language count.
language_update_count();
// Update weight of locked system languages.
language_update_locked_weights();
// Kill the static cache in language_list().
drupal_static_reset('language_list');
language_negotiation_include();
// Update URL Prefixes for all languages after the new default language is
@ -541,7 +541,7 @@ function language_save($language) {
}
/**
* Updates the language_count variable.
* Updates the language_count state.
*
* This is used to check if a site is multilingual or not.
*
@ -554,7 +554,7 @@ function language_update_count() {
$count++;
}
}
variable_set('language_count', $count);
Drupal::state()->set('language_count', $count);
}
/**
@ -576,13 +576,13 @@ function language_delete($langcode) {
// Remove the language.
entity_delete_multiple('language_entity', array($language->id));
drupal_static_reset('language_list');
language_update_count();
// Update weight of locked system languages.
language_update_locked_weights();
drupal_static_reset('language_list');
$t_args = array('%language' => $language->name, '%langcode' => $language->id);
watchdog('language', 'The %language (%langcode) language has been removed.', $t_args);
return TRUE;

View File

@ -116,10 +116,11 @@ class LanguageListTest extends WebTestBase {
// Verify that language is no longer found.
$this->drupalGet('admin/config/regional/language/delete/' . $langcode);
$this->assertResponse(404, 'Language no longer found.');
// Make sure the "language_count" variable has been updated correctly.
// Make sure the "language_count" state has been updated correctly.
drupal_static_reset('language_list');
$languages = language_list();
$this->assertEqual(variable_get('language_count', 1), count($languages), 'Language count is correct.');
$language_count = $this->container->get('state')->get('language_count') ?: 1;
$this->assertEqual($language_count, count($languages), 'Language count is correct.');
// Delete French.
$this->drupalPost('admin/config/regional/language/delete/fr', array(), t('Delete'));
// Get the count of languages.
@ -132,8 +133,9 @@ class LanguageListTest extends WebTestBase {
// Verify that language is no longer found.
$this->drupalGet('admin/config/regional/language/delete/fr');
$this->assertResponse(404, 'Language no longer found.');
// Make sure the "language_count" variable has not changed.
$this->assertEqual(variable_get('language_count', 1), count($languages), 'Language count is correct.');
// Make sure the "language_count" state has not changed.
$language_count = $this->container->get('state')->get('language_count') ?: 1;
$this->assertEqual($language_count, count($languages), 'Language count is correct.');
// Ensure we can delete the English language. Right now English is the only
// language so we must add a new language and make it the default before

View File

@ -87,7 +87,7 @@ class LocaleUninstallTest extends WebTestBase {
$this->drupalPost('admin/config/regional/translate', $edit, t('Save translations'));
_locale_rebuild_js('fr');
$config = config('locale.settings');
$locale_javascripts = \Drupal::state()->get('locale.translation.javascript') ?: array();
$locale_javascripts = $this->container->get('state')->get('locale.translation.javascript') ?: array();
$js_file = 'public://' . $config->get('javascript.directory') . '/fr_' . $locale_javascripts['fr'] . '.js';
$this->assertTrue($result = file_exists($js_file), t('JavaScript file created: %file', array('%file' => $result ? $js_file : t('none'))));
@ -121,7 +121,7 @@ class LocaleUninstallTest extends WebTestBase {
$this->assertTrue($result = !file_exists($js_file), t('JavaScript file deleted: %file', array('%file' => $result ? $js_file : t('found'))));
// Check language count.
$language_count = variable_get('language_count', 1);
$language_count = $this->container->get('state')->get('language_count') ?: 1;
$this->assertEqual($language_count, 1, t('Language count: %count', array('%count' => $language_count)));
// Check language negotiation.
@ -139,7 +139,7 @@ class LocaleUninstallTest extends WebTestBase {
$this->assertFalse(config('language.negotiation')->get('session.parameter'), t('Visit language negotiation method settings cleared.'));
// Check JavaScript parsed.
$javascript_parsed_count = count(\Drupal::state()->get('system.javascript_parsed') ?: array());
$javascript_parsed_count = count($this->container->get('state')->get('system.javascript_parsed') ?: array());
$this->assertEqual($javascript_parsed_count, 0, t('JavaScript parsed count: %count', array('%count' => $javascript_parsed_count)));
}
}