From 566ebfd9de99e11be4144a74837fdd248d2ac646 Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Sat, 20 Jul 2013 20:39:11 +0100 Subject: [PATCH] Issue #1856976 by andypost, Soul88, ACF, aspilicious, vijaycs85, dawehner: Convert language_count() to the state system. --- core/core.services.yml | 1 + core/includes/bootstrap.inc | 5 +--- core/includes/install.core.inc | 4 ++- core/includes/update.inc | 2 +- .../Drupal/Core/Language/LanguageManager.php | 26 +++++++++++++++++-- core/modules/language/language.install | 8 +++--- core/modules/language/language.module | 14 +++++----- .../language/Tests/LanguageListTest.php | 10 ++++--- .../locale/Tests/LocaleUninstallTest.php | 6 ++--- 9 files changed, 50 insertions(+), 26 deletions(-) diff --git a/core/core.services.yml b/core/core.services.yml index 640440b37c2..0fca40adeb7 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -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: diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc index 9daa71a5a5b..58aebe949ff 100644 --- a/core/includes/bootstrap.inc +++ b/core/includes/bootstrap.inc @@ -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(); } /** diff --git a/core/includes/install.core.inc b/core/includes/install.core.inc index eefe9260bb7..5217e3778cc 100644 --- a/core/includes/install.core.inc +++ b/core/includes/install.core.inc @@ -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); diff --git a/core/includes/update.inc b/core/includes/update.inc index cf25b03bef5..8b57e2da960 100644 --- a/core/includes/update.inc +++ b/core/includes/update.inc @@ -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'); diff --git a/core/lib/Drupal/Core/Language/LanguageManager.php b/core/lib/Drupal/Core/Language/LanguageManager.php index a17ea10a438..86b43f5f6fb 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. @@ -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() { diff --git a/core/modules/language/language.install b/core/modules/language/language.install index b7b3c770108..42f9c37639f 100644 --- a/core/modules/language/language.install +++ b/core/modules/language/language.install @@ -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); } /** diff --git a/core/modules/language/language.module b/core/modules/language/language.module index 96746301518..7e5b87b0395 100644 --- a/core/modules/language/language.module +++ b/core/modules/language/language.module @@ -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; diff --git a/core/modules/language/lib/Drupal/language/Tests/LanguageListTest.php b/core/modules/language/lib/Drupal/language/Tests/LanguageListTest.php index 1483aa4ea53..4870f57abec 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 @@ 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 diff --git a/core/modules/locale/lib/Drupal/locale/Tests/LocaleUninstallTest.php b/core/modules/locale/lib/Drupal/locale/Tests/LocaleUninstallTest.php index 961a5dd58b2..9a81c2db897 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 @@ 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))); } }