Commit 21a11cf5 authored by catch's avatar catch

Issue #2336177 by alexpott, YesCT: Fixed ConfigurableLanguage cannot be...

Issue #2336177 by alexpott, YesCT: Fixed ConfigurableLanguage cannot be created/removed directly, integrate language_save() and language_delete().
parent 2ef89a6a
......@@ -19,6 +19,7 @@
use Drupal\Core\StringTranslation\Translator\FileTranslation;
use Drupal\Core\Extension\ExtensionDiscovery;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\language\Entity\ConfigurableLanguage;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
......@@ -1548,30 +1549,14 @@ function install_import_translations(&$install_state) {
include_once __DIR__ . '/../modules/locale/locale.bulk.inc';
$langcode = $install_state['parameters']['langcode'];
$standard_languages = LanguageManager::getStandardLanguageList();
if (!isset($standard_languages[$langcode])) {
// Drupal does not know about this language, so we prefill its values with
// our best guess. The user will be able to edit afterwards.
$language = new Language(array(
'id' => $langcode,
'name' => $langcode,
'default' => TRUE,
));
language_save($language);
}
else {
// A known predefined language, details will be filled in properly.
$language = new Language(array(
'id' => $langcode,
'default' => TRUE,
));
language_save($language);
}
$language = ConfigurableLanguage::createFromLangcode($langcode);
$language->set('default', TRUE);
$language->save();
// If a non-English language was selected, remove English and import the
// translations.
if ($langcode != 'en') {
language_delete('en');
entity_delete_multiple('configurable_language', array('en'));
// Set up a batch to import translations for the newly added language.
_install_prepare_import($langcode);
......
......@@ -7,7 +7,7 @@
namespace Drupal\aggregator\Tests;
use Drupal\Core\Language\Language;
use Drupal\language\Entity\ConfigurableLanguage;
/**
* Tests aggregator feeds in multiple languages.
......@@ -34,13 +34,13 @@ protected function setUp() {
parent::setUp();
// Create test languages.
$this->langcodes = array(language_load('en'));
$this->langcodes = array(ConfigurableLanguage::load('en'));
for ($i = 1; $i < 3; ++$i) {
$language = new Language(array(
$language = ConfigurableLanguage::create(array(
'id' => 'l' . $i,
'name' => $this->randomString(),
'label' => $this->randomString(),
));
language_save($language);
$language->save();
$this->langcodes[$i] = $language;
}
}
......
......@@ -7,7 +7,7 @@
namespace Drupal\basic_auth\Tests\Authentication;
use Drupal\Core\Language\Language;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\simpletest\WebTestBase;
/**
......@@ -119,8 +119,7 @@ function testPerUserLoginFloodControl() {
* Tests compatibility with locale/UI translation.
*/
function testLocale() {
$language = new Language(array('id' => 'de', 'default' => TRUE));
language_save($language);
ConfigurableLanguage::create(array('id' => 'de', 'label' => 'German', 'default' => TRUE))->save();
$account = $this->drupalCreateUser();
......
......@@ -8,7 +8,7 @@
namespace Drupal\block\Tests;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Language\Language;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\simpletest\WebTestBase;
/**
......@@ -36,13 +36,13 @@ protected function setUp() {
parent::setUp();
// Create test languages.
$this->langcodes = array(language_load('en'));
$this->langcodes = array(ConfigurableLanguage::load('en'));
for ($i = 1; $i < 3; ++$i) {
$language = new Language(array(
$language = ConfigurableLanguage::create(array(
'id' => 'l' . $i,
'name' => $this->randomString(),
'label' => $this->randomString(),
));
language_save($language);
$language->save();
$this->langcodes[$i] = $language;
}
}
......
......@@ -7,8 +7,8 @@
namespace Drupal\comment\Tests\Views;
use Drupal\Core\Language\Language;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\language\Entity\ConfigurableLanguage;
/**
* Tests comment field filters with translations.
......@@ -40,17 +40,8 @@ function setUp() {
parent::setUp();
// Add two new languages.
$language = new Language(array(
'id' => 'fr',
'name' => 'French',
));
language_save($language);
$language = new Language(array(
'id' => 'es',
'name' => 'Spanish',
));
language_save($language);
ConfigurableLanguage::createFromLangcode('fr')->save();
ConfigurableLanguage::createFromLangcode('es')->save();
// Make the comment body field translatable. The title is already
// translatable by definition.
......
......@@ -7,7 +7,7 @@
namespace Drupal\config\Tests;
use Drupal\Core\Language\Language;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\simpletest\DrupalUnitTestBase;
/**
......@@ -44,14 +44,8 @@ function testConfigLanguageOverride() {
$raw = $config->getRawData();
$this->assertIdentical($raw['foo'], 'bar');
language_save(new Language(array(
'name' => 'French',
'id' => 'fr',
)));
language_save(new Language(array(
'name' => 'German',
'id' => 'de',
)));
ConfigurableLanguage::createFromLangcode('fr')->save();
ConfigurableLanguage::createFromLangcode('de')->save();
\Drupal::languageManager()->setConfigOverrideLanguage(language_load('fr'));
$config = \Drupal::config('config_test.system');
......
......@@ -8,6 +8,7 @@
namespace Drupal\config\Tests;
use Drupal\Core\Language\LanguageInterface;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\simpletest\WebTestBase;
/**
......@@ -36,7 +37,7 @@ function testSiteNameTranslation() {
$edit = array(
'predefined_langcode' => 'custom',
'langcode' => $langcode,
'name' => $name,
'label' => $name,
'direction' => LanguageInterface::DIRECTION_LTR,
);
$this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
......@@ -66,10 +67,11 @@ function testSiteNameTranslation() {
// overrides still work.
$language_manager = \Drupal::languageManager()->reset();
$this->assertTrue($language_manager->isMultilingual(), 'The test site is multilingual.');
$language = \Drupal::languageManager()->getLanguage('xx');
$language->default = TRUE;
language_save($language);
language_delete('en');
$language = ConfigurableLanguage::load('xx');
$language->set('default', TRUE);
$language->save();
ConfigurableLanguage::load('en')->delete();
$this->assertFalse($language_manager->isMultilingual(), 'The test site is monolingual.');
$this->drupalGet('xx');
......
......@@ -7,7 +7,7 @@
namespace Drupal\config_translation\Tests;
use Drupal\Core\Language\Language;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\simpletest\WebTestBase;
/**
......@@ -45,8 +45,7 @@ protected function setUp() {
$this->pluginId = key($definitions);
$this->langcode = 'xx';
$language = new Language(array('id' => $this->langcode, 'name' => 'XX'));
language_save($language);
ConfigurableLanguage::create(array('id' => $this->langcode, 'label' => 'XX'))->save();
\Drupal::state()->set('config_translation_test_alter_form_alter', TRUE);
}
......
......@@ -8,7 +8,7 @@
namespace Drupal\config_translation\Tests;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Language\Language;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\simpletest\WebTestBase;
/**
......@@ -325,8 +325,7 @@ public function doUserRoleListTest() {
public function doLanguageListTest() {
// Create a test language to decouple looking for translate operations
// link so this does not test more than necessary.
$language = new Language(array('id' => 'ga', 'name' => 'Irish'));
language_save($language);
ConfigurableLanguage::createFromLangcode('ga')->save();
// Get the language listing.
$this->drupalGet('admin/config/regional/language');
......
......@@ -8,7 +8,7 @@
namespace Drupal\config_translation\Tests;
use Drupal\Component\Utility\String;
use Drupal\Core\Language\Language;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\simpletest\WebTestBase;
/**
......@@ -55,8 +55,7 @@ protected function setUp() {
// Add languages.
foreach ($this->langcodes as $langcode) {
$language = new Language(array('id' => $langcode));
language_save($language);
ConfigurableLanguage::createFromLangcode($langcode)->save();
}
$this->localeStorage = $this->container->get('locale.storage');
}
......
......@@ -12,6 +12,7 @@
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Language\Language;
use Drupal\Core\Language\LanguageInterface;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\simpletest\WebTestBase;
/**
......@@ -78,8 +79,7 @@ protected function setUp() {
// Add languages.
foreach ($this->langcodes as $langcode) {
$language = new Language(array('id' => $langcode));
language_save($language);
ConfigurableLanguage::createFromLangcode($langcode)->save();
}
$this->localeStorage = $this->container->get('locale.storage');
}
......@@ -570,7 +570,7 @@ public function testLocaleDBStorage() {
$edit = array(
'predefined_langcode' => 'custom',
'langcode' => $langcode,
'name' => $name,
'label' => $name,
'direction' => Language::DIRECTION_LTR,
);
$this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
......
......@@ -7,7 +7,7 @@
namespace Drupal\config_translation\Tests;
use Drupal\Core\Language\Language;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\simpletest\WebTestBase;
/**
......@@ -52,8 +52,7 @@ protected function setUp() {
// Add languages.
foreach ($this->langcodes as $langcode) {
$language = new Language(array('id' => $langcode));
language_save($language);
ConfigurableLanguage::createFromLangcode($langcode)->save();
}
}
......
......@@ -8,7 +8,7 @@
namespace Drupal\content_translation\Tests;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Language\Language;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\node\Entity\NodeType;
use Drupal\simpletest\WebTestBase;
......@@ -65,7 +65,7 @@ protected function setUp() {
parent::setUp();
// Set up an additional language.
$this->langcodes = array(language_default()->id, 'es');
language_save(new Language(array('id' => 'es')));
ConfigurableLanguage::createFromLangcode('es')->save();
// Create a content type.
$this->bundle = $this->randomMachineName();
......
......@@ -8,7 +8,7 @@
namespace Drupal\content_translation\Tests;
use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
use Drupal\Core\Language\Language;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\simpletest\WebTestBase;
/**
......@@ -101,7 +101,7 @@ protected function setUp() {
protected function setupLanguages() {
$this->langcodes = array('it', 'fr');
foreach ($this->langcodes as $langcode) {
language_save(new Language(array('id' => $langcode)));
ConfigurableLanguage::createFromLangcode($langcode)->save();
}
array_unshift($this->langcodes, \Drupal::languageManager()->getDefaultLanguage()->id);
}
......
......@@ -7,7 +7,7 @@
namespace Drupal\field\Tests;
use Drupal\Core\Language\Language;
use Drupal\language\Entity\ConfigurableLanguage;
/**
* Tests multilanguage fields logic.
......@@ -97,11 +97,10 @@ protected function setUp() {
$this->instance->save();
for ($i = 0; $i < 3; ++$i) {
$language = new Language(array(
ConfigurableLanguage::create(array(
'id' => 'l' . $i,
'name' => $this->randomString(),
));
language_save($language);
'label' => $this->randomString(),
))->save();
}
}
......
......@@ -7,7 +7,7 @@
namespace Drupal\field\Tests;
use Drupal\Core\Language\Language;
use Drupal\language\Entity\ConfigurableLanguage;
/**
* Tests multilanguage fields logic that require a full environment.
......@@ -78,11 +78,10 @@ protected function setUp() {
->save();
for ($i = 0; $i < 3; ++$i) {
$language = new Language(array(
ConfigurableLanguage::create(array(
'id' => 'l' . $i,
'name' => $this->randomString(),
));
language_save($language);
'label' => $this->randomString(),
))->save();
}
}
......
......@@ -8,12 +8,12 @@
namespace Drupal\hal\Tests;
use Drupal\Core\Cache\MemoryBackend;
use Drupal\Core\Language\Language;
use Drupal\hal\Encoder\JsonEncoder;
use Drupal\hal\Normalizer\ContentEntityNormalizer;
use Drupal\hal\Normalizer\EntityReferenceItemNormalizer;
use Drupal\hal\Normalizer\FieldItemNormalizer;
use Drupal\hal\Normalizer\FieldNormalizer;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\rest\LinkManager\LinkManager;
use Drupal\rest\LinkManager\RelationLinkManager;
use Drupal\rest\LinkManager\TypeLinkManager;
......@@ -66,19 +66,12 @@ protected function setUp() {
$this->installEntitySchema('entity_test');
$this->installConfig(array('field', 'language'));
// Add English as a language.
$english = new Language(array(
'id' => 'en',
'name' => 'English',
));
language_save($english);
// Add German as a language.
$german = new Language(array(
ConfigurableLanguage::create(array(
'id' => 'de',
'name' => 'Deutsch',
'label' => 'Deutsch',
'weight' => -1,
));
language_save($german);
))->save();
// Create the test text field.
entity_create('field_storage_config', array(
......
......@@ -6,11 +6,8 @@
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\PhpStorage\PhpStorageFactory;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\language\ConfigurableLanguageManager;
use Drupal\language\ConfigurableLanguageManagerInterface;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUI;
use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl;
......@@ -409,78 +406,6 @@ function language_get_default_langcode($entity_type, $bundle) {
return $configuration['langcode'];
}
/**
* API function to add or update a language.
*
* @param $language
* Language object with properties corresponding to the 'language'
* configuration properties.
*/
function language_save($language) {
$configurable_language = entity_load('configurable_language', $language->id);
if (!$configurable_language) {
$configurable_language = entity_create('configurable_language', array(
'id' => $language->id,
));
}
$language->is_new = $configurable_language->isNew();
// Assign language properties to language entity.
$configurable_language->label = isset($language->name) ? $language->name : '';
$configurable_language->direction = isset($language->direction) ? $language->direction : '0';
$configurable_language->locked = !empty($language->locked);
$configurable_language->weight = isset($language->weight) ? $language->weight : 0;
$configurable_language->setDefault(!empty($language->default));
$configurable_language->save();
$t_args = array('%language' => $language->name, '%langcode' => $language->id);
if ($language->is_new) {
// Install any available language configuration overrides for the language.
\Drupal::service('language.config_factory_override')->installLanguageOverrides($language->getId());
\Drupal::logger('language')->notice('The %language (%langcode) language has been created.', $t_args);
}
else {
\Drupal::logger('language')->notice('The %language (%langcode) language has been updated.', $t_args);
}
return $language;
}
/**
* Delete a language.
*
* @param $langcode
* Language code of the language to be deleted.
*
* @return
* TRUE if language is successfully deleted. Otherwise FALSE.
*/
function language_delete($langcode) {
$languages = \Drupal::languageManager()->getLanguages(LanguageInterface::STATE_ALL);
if (isset($languages[$langcode]) && !$languages[$langcode]->locked) {
$language = $languages[$langcode];
// Remove the language.
entity_delete_multiple('configurable_language', array($language->id));
$language_manager = \Drupal::languageManager();
$language_manager->reset();
if ($language_manager instanceof ConfigurableLanguageManagerInterface) {
$language_manager->updateLockedLanguageWeights();
}
// If after deleting this language the site will become monolingual, we need
// to rebuild language services.
if (!\Drupal::languageManager()->isMultilingual()) {
ConfigurableLanguageManager::rebuildServices();
}
$t_args = array('%language' => $language->name, '%langcode' => $language->id);
\Drupal::logger('language')->notice('The %language (%langcode) language has been removed.', $t_args);
return TRUE;
}
return FALSE;
}
/**
* Implements hook_language_types_info().
*
......@@ -678,10 +603,9 @@ 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) {
$languages = \Drupal::languageManager()->getLanguages();
$language = $languages[$form_state->getValue('site_default_language')];
$language->default = TRUE;
language_save($language);
$default_language = ConfigurableLanguage::load($form_state->getValue('site_default_language'));
$default_language->set('default', TRUE);
$default_language->save();
}
/**
......
......@@ -10,6 +10,8 @@
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\language\ConfigurableLanguageManagerInterface;
use Drupal\language\Exception\DeleteDefaultLanguageException;
use Drupal\language\ConfigurableLanguageInterface;
......@@ -187,6 +189,10 @@ public function postSave(EntityStorageInterface $storage, $update = TRUE) {
if (!$this->preSaveMultilingual && !$update && $language_manager instanceof ConfigurableLanguageManagerInterface) {
$language_manager::rebuildServices();
}
if (!$update) {
// Install any available language configuration overrides for the language.
\Drupal::service('language.config_factory_override')->installLanguageOverrides($this->id());
}
}
/**
......@@ -225,6 +231,23 @@ public static function preDelete(EntityStorageInterface $storage, array $entitie
}
}
/**
* {@inheritdoc}
*/
public static function postDelete(EntityStorageInterface $storage, array $entities) {
parent::postDelete($storage, $entities);
$language_manager = \Drupal::languageManager();
$language_manager->reset();
if ($language_manager instanceof ConfigurableLanguageManagerInterface) {
$language_manager->updateLockedLanguageWeights();
}
// If after deleting this language the site will become monolingual, we need
// to rebuild language services.
if (!\Drupal::languageManager()->isMultilingual()) {
ConfigurableLanguageManager::rebuildServices();
}
}
/**
* {@inheritdoc}
*/
......@@ -319,4 +342,34 @@ public function setNegotiationMethodId($method_id) {
return $this;
}
/**
* Creates a configurable language object from a langcode.
*
* @param string $langcode
* The language code to use to create the object.
*
* @return $this
*
* @see \Drupal\Core\Language\LanguageManager::getStandardLanguageList()
*/
public static function createFromLangcode($langcode) {
$standard_languages = LanguageManager::getStandardLanguageList();
if (!isset($standard_languages[$langcode])) {
// Drupal does not know about this language, so we set its values with the
// best guess. The user will be able to edit afterwards.
return static::create(array(
'id' => $langcode,
'label' => $langcode,
));
}
else {
// A known predefined language, details will be filled in properly.
return static::create(array(
'id' => $langcode,
'label' => $standard_languages[$langcode][0],
'direction' => isset($standard_languages[$langcode][2]) ? $standard_languages[$langcode][2] : static::DIRECTION_LTR,
));
}
}
}
......@@ -7,7 +7,10 @@
namespace Drupal\language\Form;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageManager;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\language\Form\LanguageFormBase;
use Drupal\Core\Language\Language;
......@@ -50,7 +53,7 @@ public function form(array $form, FormStateInterface $form_state) {
),
),
'#validate' => array('::validatePredefined'),
'#submit' => array('::submitForm'),
'#submit' => array('::submitForm', '::save'),
);
$custom_language_states_conditions = array(
......@@ -66,14 +69,14 @@ public function form(array $form, FormStateInterface $form_state) {
$form['custom_language']['langcode']['#states'] = array(
'required' => $custom_language_states_conditions,
);
$form['custom_language']['name']['#states'] = array(
$form['custom_language']['label']['#states'] = array(
'required' => $custom_language_states_conditions,
);
$form['custom_language']['submit'] = array(
'#type' => 'submit',
'#value' => $this->t('Add custom language'),
'#validate' => array('::validateCustom'),
'#submit' => array('::submitForm'),
'#submit' => array('::submitForm', '::save'),
);
return $form;
......@@ -82,24 +85,12 @@ public function form(array $form, FormStateInterface $form_state) {
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$langcode = $form_state->getValue('predefined_langcode');
if ($langcode == 'custom') {
$langcode = $form_state->getValue('langcode');
// Custom language form.
$language = new Language(array(
'id' => $langcode,
'name' => $form_state->getValue('name'),
'direction' => $form_state->getValue('direction'),
));
}
else {
$language = new Language(array('id' => $langcode));
}