summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2013-07-20 19:39:11 (GMT)
committerAlex Pott2013-07-20 19:39:11 (GMT)
commit566ebfd9de99e11be4144a74837fdd248d2ac646 (patch)
tree6806afe53b0401a2fcf827d8a4c117a830187614
parent36d5732ff8a06e72fb4ec6abe519850ef0912711 (diff)
Issue #1856976 by andypost, Soul88, ACF, aspilicious, vijaycs85, dawehner: Convert language_count() to the state system.
-rw-r--r--core/core.services.yml1
-rw-r--r--core/includes/bootstrap.inc5
-rw-r--r--core/includes/install.core.inc4
-rw-r--r--core/includes/update.inc2
-rw-r--r--core/lib/Drupal/Core/Language/LanguageManager.php26
-rw-r--r--core/modules/language/language.install8
-rw-r--r--core/modules/language/language.module14
-rw-r--r--core/modules/language/lib/Drupal/language/Tests/LanguageListTest.php10
-rw-r--r--core/modules/locale/lib/Drupal/locale/Tests/LocaleUninstallTest.php6
9 files changed, 50 insertions, 26 deletions
diff --git a/core/core.services.yml b/core/core.services.yml
index 640440b..0fca40a 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 9daa71a..58aebe9 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 eefe926..5217e37 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 cf25b03..8b57e2d 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 a17ea10..86b43f5 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 \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.
*/
public function init() {
@@ -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 b7b3c77..42f9c37 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 9674630..7e5b87b 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 1483aa4..4870f57 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 961a5dd..9a81c2d 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)));
}
}