Commit c0a7453a authored by catch's avatar catch

Issue #2339435 by YesCT, alexpott: Default no longer needs to be a property on...

Issue #2339435 by YesCT, alexpott: Default no longer needs to be a property on Language or ConfigurableLanguage.
parent 575a6480
......@@ -1550,12 +1550,12 @@ function install_import_translations(&$install_state) {
$langcode = $install_state['parameters']['langcode'];
$language = ConfigurableLanguage::createFromLangcode($langcode);
$language->set('default', TRUE);
$language->save();
// If a non-English language was selected, remove English and import the
// translations.
// If a non-English language was selected, change the default language,
// remove English and import the translations.
if ($langcode != 'en') {
\Drupal::config('system.site')->set('langcode', $langcode)->save();
entity_delete_multiple('configurable_language', array('en'));
// Set up a batch to import translations for the newly added language.
......
......@@ -1040,7 +1040,7 @@ protected function compileContainer() {
$default_language_values = Language::$defaultValues;
if ($system = $this->getConfigStorage()->read('system.site')) {
if ($default_language_values['id'] != $system['langcode']) {
$default_language_values = array('id' => $system['langcode'], 'default' => TRUE);
$default_language_values = array('id' => $system['langcode']);
}
}
$container->setParameter('language.default_values', $default_language_values);
......
......@@ -25,7 +25,6 @@ class Language implements LanguageInterface {
'direction' => self::DIRECTION_LTR,
'weight' => 0,
'locked' => FALSE,
'default' => TRUE,
);
// Properties within the Language are set up as the default language.
......@@ -60,13 +59,6 @@ class Language implements LanguageInterface {
*/
public $weight = 0;
/**
* Flag indicating if this is the only site default language.
*
* @var bool
*/
public $default = FALSE;
/**
* Locked indicates a language used by the system, not an actual language.
*
......@@ -136,7 +128,7 @@ public function getWeight() {
* {@inheritdoc}
*/
public function isDefault() {
return $this->default;
return static::getDefaultLangcode() == $this->getId();
}
/**
......@@ -156,4 +148,15 @@ public static function sort(&$languages) {
});
}
/**
* Gets the default langcode.
*
* @return string
* The current default langcode.
*/
protected static function getDefaultLangcode() {
$language = \Drupal::service('language.default')->get();
return $language->getId();
}
}
......@@ -53,7 +53,6 @@ public function get() {
* The default language.
*/
public function set(LanguageInterface $language) {
$language->default = TRUE;
$this->language = $language;
}
......
......@@ -119,7 +119,8 @@ function testPerUserLoginFloodControl() {
* Tests compatibility with locale/UI translation.
*/
function testLocale() {
ConfigurableLanguage::create(array('id' => 'de', 'label' => 'German', 'default' => TRUE))->save();
ConfigurableLanguage::createFromLangcode('de')->save();
\Drupal::config('system.site')->set('langcode', 'de')->save();
$account = $this->drupalCreateUser();
......
......@@ -67,9 +67,7 @@ function testSiteNameTranslation() {
// overrides still work.
$language_manager = \Drupal::languageManager()->reset();
$this->assertTrue($language_manager->isMultilingual(), 'The test site is multilingual.');
$language = ConfigurableLanguage::load('xx');
$language->set('default', TRUE);
$language->save();
\Drupal::config('system.site')->set('langcode', 'xx')->save();
ConfigurableLanguage::load('en')->delete();
$this->assertFalse($language_manager->isMultilingual(), 'The test site is monolingual.');
......
......@@ -444,10 +444,10 @@ function language_negotiation_url_prefixes_update() {
foreach (\Drupal::languageManager()->getLanguages() as $language) {
// The prefix for this language should be updated if it's not assigned yet
// or the prefix is set to the empty string.
if (empty($prefixes[$language->id])) {
if (empty($prefixes[$language->getId()])) {
// For the default language, set the prefix to the empty string,
// otherwise use the langcode.
$prefixes[$language->id] = !empty($language->default) ? '' : $language->id;
$prefixes[$language->getId()] = $language->isDefault() ? '' : $language->id;
}
// Otherwise we keep the configured prefix.
}
......@@ -592,9 +592,7 @@ function language_form_system_regional_settings_alter(&$form, FormStateInterface
* @see language_form_system_regional_settings_alter()
*/
function language_system_regional_settings_form_submit($form, FormStateInterface $form_state) {
$default_language = ConfigurableLanguage::load($form_state->getValue('site_default_language'));
$default_language->set('default', TRUE);
$default_language->save();
\Drupal::config('system.site')->set('langcode', $form_state->getValue('site_default_language'))->save();
}
/**
......
......@@ -9,6 +9,7 @@ services:
- [initLanguageManager]
language.config_subscriber:
class: Drupal\language\EventSubscriber\ConfigSubscriber
arguments: ['@language_manager', '@language.default']
tags:
- { name: event_subscriber }
language.config_factory_override:
......
......@@ -7,12 +7,10 @@
namespace Drupal\language\Entity;
use Drupal\Core\Language\Language as LanguageObject;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Language\LanguageManager;
use Drupal\language\ConfigurableLanguageManager;
use Drupal\Core\Language\Language;
use Drupal\language\ConfigurableLanguageManagerInterface;
use Drupal\language\Exception\DeleteDefaultLanguageException;
use Drupal\language\ConfigurableLanguageInterface;
......@@ -82,21 +80,6 @@ class ConfigurableLanguage extends ConfigEntityBase implements ConfigurableLangu
*/
public $locked = FALSE;
/**
* Flag to indicate if the language entity is the default site language.
*
* This property is not saved to the language entity since there can be only
* one default language. It is saved to system.site:langcode and set on the
* container using the language.default service in when the entity is saved.
* The value is set correctly when a language entity is created or loaded.
*
* @see \Drupal\language\Entity\ConfigurableLanguage::postSave()
* @see \Drupal\language\Entity\ConfigurableLanguage::isDefault()
*
* @var bool
*/
protected $default;
/**
* Used during saving to detect when the site becomes multilingual.
*
......@@ -111,17 +94,11 @@ class ConfigurableLanguage extends ConfigEntityBase implements ConfigurableLangu
protected $preSaveMultilingual;
/**
* Checks if the language entity is the site default language.
*
* @return bool
* TRUE if the language entity is the site default language, FALSE if not.
* {@inheritdoc}
*/
public function isDefault() {
if (!isset($this->default)) {
return static::getDefaultLangcode() == $this->id();
}
return $this->default;
}
/**
* {@inheritdoc}
......@@ -144,24 +121,14 @@ public function preSave(EntityStorageInterface $storage) {
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
// Only set the default language and save it to system.site configuration if
// it needs to updated.
if ($this->isDefault() && static::getDefaultLangcode() != $this->id()) {
// Update the config. Saving the configuration fires and event that causes
// the container to be rebuilt.
\Drupal::config('system.site')->set('langcode', $this->id())->save();
\Drupal::service('language.default')->set($this->toLanguageObject());
}
$language_manager = \Drupal::languageManager();
$language_manager->reset();
if ($language_manager instanceof ConfigurableLanguageManagerInterface) {
$language_manager->updateLockedLanguageWeights();
}
// Update URL Prefixes for all languages after the new default language is
// propagated and the LanguageManagerInterface::getLanguages() cache is
// flushed.
// Update URL Prefixes for all languages after the
// LanguageManagerInterface::getLanguages() cache is flushed.
language_negotiation_url_prefixes_update();
// If after adding this language the site will become multilingual, we need
......@@ -175,26 +142,6 @@ public function postSave(EntityStorageInterface $storage, $update = TRUE) {
}
}
/**
* Converts the ConfigurableLanguage entity to a Core Language value object.
*
* @todo fix return type hint after https://drupal.org/node/2246665 and
* https://drupal.org/node/2246679.
*
* @return \Drupal\Core\Language\LanguageInterface
* The language configuration entity expressed as a Language value object.
*/
protected function toLanguageObject() {
return new LanguageObject(array(
'id' => $this->id(),
'name' => $this->label(),
'direction' => $this->direction,
'weight' => $this->weight,
'locked' => $this->locked,
'default' => $this->default,
));
}
/**
* {@inheritdoc}
*
......@@ -228,18 +175,6 @@ public static function postDelete(EntityStorageInterface $storage, array $entiti
}
}
/**
* {@inheritdoc}
*/
public function get($property_name) {
if ($property_name == 'default') {
return $this->isDefault();
}
else {
return parent::get($property_name);
}
}
/**
* Gets the default langcode.
*
......
......@@ -7,6 +7,8 @@
namespace Drupal\language\EventSubscriber;
use Drupal\Core\Language\LanguageDefault;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\PhpStorage\PhpStorageFactory;
use Drupal\Core\Config\ConfigCrudEvent;
use Drupal\Core\Config\ConfigEvents;
......@@ -17,6 +19,33 @@
*/
class ConfigSubscriber implements EventSubscriberInterface {
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* The default language.
*
* @var \Drupal\Core\Language\LanguageDefault
*/
protected $languageDefault;
/**
* Constructs a new class object.
*
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
* @param \Drupal\Core\Language\LanguageDefault $language_default
* The default language.
*/
public function __construct(LanguageManagerInterface $language_manager, LanguageDefault $language_default) {
$this->languageManager = $language_manager;
$this->languageDefault = $language_default;
}
/**
* Causes the container to be rebuilt on the next request.
*
......@@ -26,6 +55,13 @@ class ConfigSubscriber implements EventSubscriberInterface {
public function onConfigSave(ConfigCrudEvent $event) {
$saved_config = $event->getConfig();
if ($saved_config->getName() == 'system.site' && $event->isChanged('langcode')) {
$language = $this->languageManager->getLanguage($saved_config->get('langcode'));
// During an import the language might not exist yet.
if ($language) {
$this->languageDefault->set($language);
$this->languageManager->reset();
language_negotiation_url_prefixes_update();
}
// Trigger a container rebuild on the next request by deleting compiled
// from PHP storage.
PhpStorageFactory::get('service_container')->deleteAll();
......
......@@ -76,7 +76,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
$t_args = array('%language' => $language->name, '%langcode' => $language->id);
$form['prefix'][$langcode] = array(
'#type' => 'textfield',
'#title' => $language->default ? $this->t('%language (%langcode) path prefix (Default language)', $t_args) : $this->t('%language (%langcode) path prefix', $t_args),
'#title' => $language->isDefault() ? $this->t('%language (%langcode) path prefix (Default language)', $t_args) : $this->t('%language (%langcode) path prefix', $t_args),
'#maxlength' => 64,
'#default_value' => isset($prefixes[$langcode]) ? $prefixes[$langcode] : '',
'#field_prefix' => $base_url . '/',
......@@ -106,7 +106,7 @@ public function validateForm(array &$form, FormStateInterface $form_state) {
$value = $form_state->getValue(array('prefix', $langcode));
if ($value === '') {
if (!$language->default && $form_state->getValue('language_negotiation_url_part') == LanguageNegotiationUrl::CONFIG_PATH_PREFIX) {
if (!$language->isDefault() && $form_state->getValue('language_negotiation_url_part') == LanguageNegotiationUrl::CONFIG_PATH_PREFIX) {
// Throw a form error if the prefix is blank for a non-default language,
// although it is required for selected negotiation type.
$form_state->setErrorByName("prefix][$langcode", $this->t('The prefix may only be left blank for the default language.'));
......@@ -130,7 +130,7 @@ public function validateForm(array &$form, FormStateInterface $form_state) {
$value = $form_state->getValue(array('domain', $langcode));
if ($value === '') {
if (!$language->default && $form_state->getValue('language_negotiation_url_part') == LanguageNegotiationUrl::CONFIG_DOMAIN) {
if (!$language->isDefault() && $form_state->getValue('language_negotiation_url_part') == LanguageNegotiationUrl::CONFIG_DOMAIN) {
// Throw a form error if the domain is blank for a non-default language,
// although it is required for selected negotiation type.
$form_state->setErrorByName("domain][$langcode", $this->t('The domain may only be left blank for the default language.'));
......
......@@ -100,7 +100,7 @@ protected function getDefaultLanguageValues() {
$system = $config_storage->read('system.site');
$default_language = $config_storage->read(static::CONFIG_PREFIX . $system['langcode']);
if (is_array($default_language)) {
return $default_language + array('default' => TRUE);
return $default_language;
}
return FALSE;
}
......
......@@ -83,18 +83,16 @@ public function testDefaultLangcode() {
$old_default = \Drupal::languageManager()->getDefaultLanguage();
// Ensure the language entity default value is correct.
$configurable_language = entity_load('configurable_language', $old_default->getId());
$this->assertTrue($configurable_language->get('default'), 'The en language entity is flagged as the default language.');
$this->assertTrue($configurable_language->isDefault(), 'The en language entity is flagged as the default language.');
$new_default = ConfigurableLanguage::load('cc');
$new_default->set('default', TRUE);
$new_default->save();
\Drupal::config('system.site')->set('langcode', 'cc')->save();
language_save_default_configuration('custom_type', 'custom_bundle', array('langcode' => 'site_default', 'language_show' => TRUE));
$langcode = language_get_default_langcode('custom_type', 'custom_bundle');
$this->assertEqual($langcode, 'cc');
// Ensure the language entity default value is correct.
$configurable_language = entity_load('configurable_language', $old_default->getId());
$this->assertFalse($configurable_language->get('default'), 'The en language entity is not flagged as the default language.');
$this->assertFalse($configurable_language->isDefault(), 'The en language entity is not flagged as the default language.');
$configurable_language = entity_load('configurable_language', 'cc');
// Check calling the
// \Drupal\language\Entity\ConfigurableLanguage::isDefault() method
......
......@@ -42,13 +42,8 @@ function testDependencyInjectedNewLanguage() {
function testDependencyInjectedNewDefaultLanguage() {
$default_language = ConfigurableLanguage::load(\Drupal::languageManager()->getDefaultLanguage()->getId());
// Change the language default object to different values.
ConfigurableLanguage::create(array(
'id' => 'fr',
'label' => 'French',
'direction' => Language::DIRECTION_LTR,
'weight' => 0,
'default' => TRUE,
))->save();
ConfigurableLanguage::createFromLangcode('fr')->save();
\Drupal::config('system.site')->set('langcode', 'fr')->save();
// The language system creates a Language object which contains the
// same properties as the new default language object.
......@@ -65,8 +60,8 @@ function testDependencyInjectedNewDefaultLanguage() {
}
// Re-save the previous default language and the delete should work.
$default_language->set('default', TRUE);
$default_language->save();
\Drupal::config('system.site')->set('langcode', $default_language->getId())->save();
entity_delete_multiple('configurable_language', array('fr'));
$result = \Drupal::languageManager()->getCurrentLanguage();
$this->assertIdentical($result->id, $default_language->id);
......
......@@ -89,9 +89,8 @@ function testUILanguageNegotiation() {
// is for some reason not found when doing translate search. This might
// be some bug.
$default_language = \Drupal::languageManager()->getDefaultLanguage();
$language = ConfigurableLanguage::createFromLangcode($langcode_browser_fallback);
$language->set('default', TRUE);
$language->save();
ConfigurableLanguage::createFromLangcode($langcode_browser_fallback)->save();
\Drupal::config('system.site')->set('langcode', $langcode_browser_fallback)->save();
ConfigurableLanguage::createFromLangcode($langcode)->save();
// We will look for this string in the admin/config screen to see if the
......@@ -104,9 +103,7 @@ function testUILanguageNegotiation() {
// Now the t()'ed string is in db so switch the language back to default.
// This will rebuild the container so we need to rebuild the container in
// the test environment.
$default_language = ConfigurableLanguage::load($default_language->getId());
$default_language->set('default', TRUE);
$default_language->save();
\Drupal::config('system.site')->set('langcode', $default_language->getId())->save();
\Drupal::config('language.negotiation')->set('url.prefixes.en', '')->save();
$this->rebuildContainer();
......
......@@ -31,9 +31,9 @@ public function setUp() {
parent::setUp();
// Change the language default object to different values.
$language = ConfigurableLanguage::createFromLangcode('fr');
$language->set('default', TRUE);
$language->save();
ConfigurableLanguage::createFromLangcode('fr')->save();
\Drupal::config('system.site')->set('langcode', 'fr')->save();
$this->drupalLogin($this->root_user);
}
......
......@@ -150,9 +150,7 @@ function testMenuLanguageRemovedEnglish() {
// Remove English language. To do that another language has to be set as
// default.
$language = ConfigurableLanguage::load('cs');
$language->set('default', TRUE);
$language->save();
\Drupal::config('system.site')->set('langcode', 'cs')->save();
entity_delete_multiple('configurable_language', array('en'));
// Save the menu again and check if the language is still the same.
......
......@@ -96,9 +96,7 @@ function testFormatDate() {
$this->assertIdentical(format_date($timestamp, 'custom', 'l, d-M-y H:i:s T', 'Europe/London', 'en'), 'Monday, 26-Mar-07 01:00:00 BST', 'Test a different time zone.');
// Change the default language and timezone.
$language = ConfigurableLanguage::load(static::LANGCODE);
$language->set('default', TRUE);
$language->save();
\Drupal::config('system.site')->set('langcode', static::LANGCODE)->save();
date_default_timezone_set('America/Los_Angeles');
$this->assertIdentical(format_date($timestamp, 'custom', 'l, d-M-y H:i:s T', 'America/Los_Angeles', 'en'), 'Sunday, 25-Mar-07 17:00:00 PDT', 'Test a different language.');
......
......@@ -70,9 +70,7 @@ protected function setUp() {
$this->installLanguages();
// Assign Lolspeak (xx) to be the default language.
$language = ConfigurableLanguage::load('xx');
$language->set('default', TRUE);
$language->save();
\Drupal::config('system.site')->set('langcode', 'xx')->save();
$this->rebuildContainer();
// Check that lolspeak is the default language for the site.
......
......@@ -95,9 +95,7 @@ function testDefaultTermLanguage() {
// Change the default language of the site and check if the default terms
// language is still correctly selected.
$new_default = ConfigurableLanguage::load('cc');
$new_default->set('default', TRUE);
$new_default->save();
\Drupal::config('system.site')->set('langcode', 'cc')->save();
$edit = array(
'default_language[langcode]' => 'site_default',
'default_language[language_show]' => TRUE,
......
......@@ -56,9 +56,8 @@ public function testCreateView() {
$random_description = $this->randomMachineName();
// Add a new language and mark it as default.
$language = ConfigurableLanguage::createFromLangcode('it');
$language->set('default', TRUE);
$language->save();
ConfigurableLanguage::createFromLangcode('it')->save();
\Drupal::config('system.site')->set('langcode', 'it')->save();
$form_state->setValues([
'id' => $random_id,
......
......@@ -49,9 +49,26 @@ public function testGetDirection() {
* @covers ::isDefault()
*/
public function testIsDefault() {
$language_code = $this->randomMachineName(2);
$language = new Language(array('id' => $language_code, 'default' => TRUE));
$language_default = $this->getMockBuilder('Drupal\Core\Language\LanguageDefault')->disableOriginalConstructor()->getMock();
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
$container->expects($this->any())
->method('get')
->with('language.default')
->will($this->returnValue($language_default));
\Drupal::setContainer($container);
$language = new Language(array('id' => $this->randomMachineName(2)));
// Set up the LanguageDefault to return different default languages on
// consecutive calls.
$language_default->expects($this->any())
->method('get')
->willReturnOnConsecutiveCalls(
$language,
new Language(array('id' => $this->randomMachineName(2)))
);
$this->assertTrue($language->isDefault());
$this->assertFalse($language->isDefault());
}
/**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment