diff --git a/core/core.services.yml b/core/core.services.yml index 43c6d24..dcda25d 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -185,6 +185,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: diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc index cf0e3dd..470cbb9 100644 --- a/core/includes/bootstrap.inc +++ b/core/includes/bootstrap.inc @@ -2446,10 +2446,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(); } /** diff --git a/core/includes/update.inc b/core/includes/update.inc index 3659caf..8c7db86 100644 --- a/core/includes/update.inc +++ b/core/includes/update.inc @@ -501,7 +501,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'); diff --git a/core/lib/Drupal/Core/Language/LanguageManager.php b/core/lib/Drupal/Core/Language/LanguageManager.php index 01f1d50..e676d20 100644 --- a/core/lib/Drupal/Core/Language/LanguageManager.php +++ b/core/lib/Drupal/Core/Language/LanguageManager.php @@ -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. @@ -22,6 +23,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. * * @var array @@ -46,6 +54,16 @@ class LanguageManager { protected $initializing = FALSE; /** + * Constructs an LanguageManager object. + * + * @param KeyValueStoreInterface $state + * The state keyvalue store. + */ + public function __construct(KeyValueStoreInterface $state = NULL) { + $this->state = $state; + } + + /** * Initializes each language type to a language object. */ public function init() { @@ -136,7 +154,11 @@ public function reset($type = NULL) { * 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 @@ protected function getLanguageTypes() { /** * 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() { diff --git a/core/modules/language/language.install b/core/modules/language/language.install index 17828fd..2fdeb47 100644 --- a/core/modules/language/language.install +++ b/core/modules/language/language.install @@ -33,7 +33,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'); @@ -104,7 +104,7 @@ function language_schema() { */ 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(); } @@ -112,10 +112,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); } /** diff --git a/core/modules/language/language.module b/core/modules/language/language.module index 6b45fa1..32fc1d7 100644 --- a/core/modules/language/language.module +++ b/core/modules/language/language.module @@ -497,15 +497,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 @@ -516,14 +516,14 @@ 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. * * @see language_multilingual() */ function language_update_count() { - variable_set('language_count', db_query('SELECT COUNT(langcode) FROM {language} WHERE locked = 0')->fetchField()); + Drupal::state()->set('language_count', db_query('SELECT COUNT(langcode) FROM {language} WHERE locked = 0')->fetchField()); } /** @@ -547,13 +547,13 @@ function language_delete($langcode) { ->condition('langcode', $language->langcode) ->execute(); + 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->langcode); watchdog('language', 'The %language (%langcode) language has been removed.', $t_args); return TRUE; diff --git a/core/modules/language/lib/Drupal/language/Tests/LanguageListTest.php b/core/modules/language/lib/Drupal/language/Tests/LanguageListTest.php index 570ae2a..8e255a9 100644 --- a/core/modules/language/lib/Drupal/language/Tests/LanguageListTest.php +++ b/core/modules/language/lib/Drupal/language/Tests/LanguageListTest.php @@ -116,10 +116,11 @@ function testLanguageList() { // 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 @@ function testLanguageList() { // 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 diff --git a/core/modules/locale/lib/Drupal/locale/Tests/LocaleUninstallTest.php b/core/modules/locale/lib/Drupal/locale/Tests/LocaleUninstallTest.php index 5675229..2a5e53b 100644 --- a/core/modules/locale/lib/Drupal/locale/Tests/LocaleUninstallTest.php +++ b/core/modules/locale/lib/Drupal/locale/Tests/LocaleUninstallTest.php @@ -87,7 +87,7 @@ function testUninstallProcess() { $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 @@ function testUninstallProcess() { $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 @@ function testUninstallProcess() { $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))); } }