diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc index d85e31d..c7370ed 100644 --- a/core/includes/bootstrap.inc +++ b/core/includes/bootstrap.inc @@ -2379,33 +2379,6 @@ function language($type) { } /** - * Returns an array of the available language types. - * - * @return array - * An array of all language types where the keys of each are the language type - * name and its value is its configurability (TRUE/FALSE). - */ -function language_types_get_all() { - $types = Drupal::config('system.language.types')->get('all'); - return $types ? $types : array_keys(language_types_get_default()); -} - -/** - * Returns a list of the built-in language types. - * - * @return array - * An array of key-values pairs where the key is the language type name and - * the value is its configurability (TRUE/FALSE). - */ -function language_types_get_default() { - return array( - Language::TYPE_INTERFACE => TRUE, - Language::TYPE_CONTENT => FALSE, - Language::TYPE_URL => FALSE, - ); -} - -/** * Returns TRUE if there is more than one language enabled. * * @return bool @@ -2453,10 +2426,11 @@ function language_default_locked_languages($weight = 0) { * * @return \Drupal\core\Language\Language|null * A fully-populated language object or NULL. + * + * @see \Druoal\Core\Language\LanguageManager::loadLanguage(). */ function language_load($langcode) { - $languages = language_list(Language::STATE_ALL); - return isset($languages[$langcode]) ? $languages[$langcode] : NULL; + return Drupal::languageManager()->loadLanguage($langcode); } /** diff --git a/core/includes/language.inc b/core/includes/language.inc index 4d5ea02..5dddf0f 100644 --- a/core/includes/language.inc +++ b/core/includes/language.inc @@ -278,28 +278,7 @@ function language_negotiation_method_enabled($method_id, $type = NULL) { * A keyed array of links ready to be themed. */ function language_negotiation_get_switch_links($type, $path) { - $links = FALSE; - $negotiation = variable_get("language_negotiation_$type", array()); - - foreach ($negotiation as $method_id => $method) { - if (isset($method['callbacks']['language_switch'])) { - if (isset($method['file'])) { - require_once DRUPAL_ROOT . '/' . $method['file']; - } - - $callback = $method['callbacks']['language_switch']; - $result = $callback($type, $path); - - if (!empty($result)) { - // Allow modules to provide translations for specific links. - drupal_alter('language_switch_links', $result, $type, $path); - $links = (object) array('links' => $result, 'method_id' => $method_id); - break; - } - } - } - - return $links; + return Drupal::languageManager()->getLanguageNegotiationSwitchLinks($type, $path); } /** @@ -376,24 +355,6 @@ function language_negotiation_info() { return Drupal::service('plugin.manager.language_negotiation_method')->getDefinitions(); } - /** - * Identifies language from configuration. - * - * @param $languages - * An array of valid language objects. - * - * @return - * A valid language code on success, FALSE otherwise. - */ -function language_from_selected($languages) { - $langcode = (string) Drupal::config('language.negotiation')->get('selected_langcode'); - // Replace the site's default langcode by its real value. - if ($langcode == 'site_default') { - $langcode = language_default()->id; - } - return isset($languages[$langcode]) ? $langcode : language_default()->id; -} - /** * Returns the possible fallback languages ordered by language weight. * diff --git a/core/lib/Drupal/Core/Language/LanguageManager.php b/core/lib/Drupal/Core/Language/LanguageManager.php index a4273f3..8081cea 100644 --- a/core/lib/Drupal/Core/Language/LanguageManager.php +++ b/core/lib/Drupal/Core/Language/LanguageManager.php @@ -272,11 +272,63 @@ public function isMultilingual() { * Returns an array of the available language types. * * @return array - * An array of all language types. + * An array of all language types where the keys of each are the language type + * name and its value is its configurability (TRUE/FALSE). */ - protected function getLanguageTypes() { - // @todo convert to CMI https://drupal.org/node/1827038 - return language_types_get_all(); + public function getLanguageTypes() { + $types = \Drupal::config('system.language.types')->get('all'); + return $types ? $types : array_keys($this->getTypeDefaults()); + } + + /** + * Returns a list of the built-in language types. + * + * @return array + * An array of key-values pairs where the key is the language type name and + * the value is its configurability (TRUE/FALSE). + */ + public function getTypeDefaults() { + return array( + Language::TYPE_INTERFACE => TRUE, + Language::TYPE_CONTENT => FALSE, + Language::TYPE_URL => FALSE, + ); + } + + /** + * Returns the language switch links for the given language type. + * + * @param $type + * The language type. + * @param $path + * The internal path the switch links will be relative to. + * + * @return array + * A keyed array of links ready to be themed. + */ + function getLanguageNegotiationSwitchLinks($type, $path) { + $links = FALSE; + $negotiation = variable_get("language_negotiation_$type", array()); + + foreach ($negotiation as $method_id => $method) { + if (isset($method['callbacks']['language_switch'])) { + if (isset($method['file'])) { + require_once DRUPAL_ROOT . '/' . $method['file']; + } + + $callback = $method['callbacks']['language_switch']; + $result = $callback($type, $path); + + if (!empty($result)) { + // Allow modules to provide translations for specific links. + \Drupal::moduleHandler()->alter('language_switch_links', $result, $type, $path); + $links = (object) array('links' => $result, 'method_id' => $method_id); + break; + } + } + } + + return $links; } /** @@ -339,6 +391,20 @@ public function getLanguageList($flags = Language::STATE_CONFIGURABLE) { } /** + * Loads a language object from the database. + * + * @param string $langcode + * The language code. + * + * @return \Drupal\core\Language\Language|null + * A fully-populated language object or NULL. + */ + public function loadLanguage($langcode) { + $languages = $this->getLanguageList(Language::STATE_ALL); + return isset($languages[$langcode]) ? $languages[$langcode] : NULL; + } + + /** * Returns a list of the default locked languages. * * @param int $weight @@ -490,4 +556,14 @@ public static function getStandardLanguageList() { ); } + /** + * Returns an array of the available language types. + * + * @return array + * An array of all language types where the keys of each are the language type + * name and its value is its configurability (TRUE/FALSE). + */ + public function getTypes() { + } + } diff --git a/core/modules/language/language.install b/core/modules/language/language.install index 42f9c37..690a87e 100644 --- a/core/modules/language/language.install +++ b/core/modules/language/language.install @@ -36,7 +36,7 @@ function language_uninstall() { variable_del('language_content_type_default'); variable_del('language_content_type_negotiation'); - foreach (language_types_get_all() as $type) { + foreach (Drupal::languageManager()->getLanguageTypes() as $type) { variable_del("language_negotiation_$type"); variable_del("language_negotiation_methods_weight_$type"); } diff --git a/core/modules/language/lib/Drupal/language/Tests/LanguageNegotiationInfoTest.php b/core/modules/language/lib/Drupal/language/Tests/LanguageNegotiationInfoTest.php index 6ab71ba..98e7f33 100644 --- a/core/modules/language/lib/Drupal/language/Tests/LanguageNegotiationInfoTest.php +++ b/core/modules/language/lib/Drupal/language/Tests/LanguageNegotiationInfoTest.php @@ -96,7 +96,7 @@ function testInfoAlterations() { // Check language negotiation results. $this->drupalGet(''); $last = \Drupal::state()->get('language_test.language_negotiation_last'); - foreach (language_types_get_all() as $type) { + foreach (Drupal::languageManager()->getLanguageTypes() as $type) { $langcode = $last[$type]; $value = $type == Language::TYPE_CONTENT || strpos($type, 'test') !== FALSE ? 'it' : 'en'; $this->assertEqual($langcode, $value, format_string('The negotiated language for %type is %language', array('%type' => $type, '%language' => $value))); @@ -107,7 +107,7 @@ function testInfoAlterations() { $this->languageNegotiationUpdate('disable'); // Check that only the core language types are available. - foreach (language_types_get_all() as $type) { + foreach (Drupal::languageManager()->getLanguageTypes() as $type) { $this->assertTrue(strpos($type, 'test') === FALSE, format_string('The %type language is still available', array('%type' => $type))); } diff --git a/core/modules/language/tests/language_test/language_test.module b/core/modules/language/tests/language_test/language_test.module index d986388..6a5c3d9 100644 --- a/core/modules/language/tests/language_test/language_test.module +++ b/core/modules/language/tests/language_test/language_test.module @@ -69,8 +69,8 @@ function language_test_language_negotiation_info_alter(array &$negotiation_info) */ function language_test_store_language_negotiation() { $last = array(); - print_r(language_types_get_all()); - foreach (language_types_get_all() as $type) { + print_r(Drupal::languageManager()->getLanguageTypes()); + foreach (Drupal::languageManager()->getLanguageTypes() as $type) { $last[$type] = language($type)->id; } Drupal::state()->set('language_test.language_negotiation_last', $last); diff --git a/core/modules/locale/lib/Drupal/locale/Tests/LocaleUninstallTest.php b/core/modules/locale/lib/Drupal/locale/Tests/LocaleUninstallTest.php index 1cfb538..749b9d4 100644 --- a/core/modules/locale/lib/Drupal/locale/Tests/LocaleUninstallTest.php +++ b/core/modules/locale/lib/Drupal/locale/Tests/LocaleUninstallTest.php @@ -97,7 +97,7 @@ function testUninstallProcess() { // Change language negotiation options. drupal_load('module', 'locale'); - \Drupal::config('system.language.types')->set('configurable', language_types_get_default() + array('language_custom' => TRUE))->save(); + \Drupal::config('system.language.types')->set('configurable', $this->container->get('language_manager')->getTypeDefaults() + array('language_custom' => TRUE))->save(); variable_set('language_negotiation_' . Language::TYPE_INTERFACE, language_language_negotiation_info()); variable_set('language_negotiation_' . Language::TYPE_CONTENT, language_language_negotiation_info()); variable_set('language_negotiation_' . Language::TYPE_URL, language_language_negotiation_info()); @@ -127,7 +127,7 @@ function testUninstallProcess() { // Check language negotiation. require_once DRUPAL_ROOT . '/core/includes/language.inc'; - $this->assertTrue(count(language_types_get_all()) == count(language_types_get_default()), 'Language types reset'); + $this->assertTrue(count($this->container->get('language_manager')->getLanguageTypes()) == count($this->container->get('language_manager')->getTypeDefaults()), 'Language types reset'); $language_negotiation = language_negotiation_method_get_first(Language::TYPE_INTERFACE) == LANGUAGE_NEGOTIATION_SELECTED; $this->assertTrue($language_negotiation, String::format('Interface language negotiation: %setting', array('%setting' => $language_negotiation ? 'none' : 'set'))); $language_negotiation = language_negotiation_method_get_first(Language::TYPE_CONTENT) == LANGUAGE_NEGOTIATION_SELECTED;