diff --git a/core/lib/Drupal/Core/Entity/ContentEntityBase.php b/core/lib/Drupal/Core/Entity/ContentEntityBase.php index 1572129..a25b8d0 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityBase.php @@ -571,7 +571,13 @@ protected function setDefaultLangcode() { } if (empty($this->defaultLangcode)) { // Make sure we return a proper language object. - $this->defaultLangcode = Language::LANGCODE_NOT_SPECIFIED; + if (\Drupal::moduleHandler()->moduleExists('language')) { + $this->defaultLangcode = language_get_default_langcode($this->getEntityTypeId(), $this->bundle()); + } + else { + // Make sure we return a proper language object. + $this->defaultLangcode = Language::LANGCODE_NOT_SPECIFIED; + } } // This needs to be initialized manually as it is skipped when instantiating // the language field object to avoid infinite recursion. diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/LanguageItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/LanguageItem.php index 3bae65b..b539dcf 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/LanguageItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/LanguageItem.php @@ -101,7 +101,11 @@ public function setValue($values, $notify = TRUE) { */ public function applyDefaultValue($notify = TRUE) { // Default to LANGCODE_NOT_SPECIFIED. - $this->setValue(array('value' => Language::LANGCODE_NOT_SPECIFIED), $notify); + $langcode = Language::LANGCODE_NOT_SPECIFIED; + if (\Drupal::moduleHandler()->moduleExists('language') && $entity = $this->getEntity()) { + $langcode = language_get_default_langcode($entity->getEntityTypeId(), $entity->bundle()); + } + $this->setValue(array('value' => $langcode), $notify); return $this; } diff --git a/core/modules/content_translation/lib/Drupal/content_translation/Tests/Views/TranslationLinkTest.php b/core/modules/content_translation/lib/Drupal/content_translation/Tests/Views/TranslationLinkTest.php index e1b939c..3297e28 100644 --- a/core/modules/content_translation/lib/Drupal/content_translation/Tests/Views/TranslationLinkTest.php +++ b/core/modules/content_translation/lib/Drupal/content_translation/Tests/Views/TranslationLinkTest.php @@ -10,6 +10,7 @@ use Drupal\views\Tests\ViewTestBase; use Drupal\content_translation\Tests\ContentTranslationTestBase; use Drupal\views\Tests\ViewTestData; +use Drupal\Core\Language\Language; /** * Tests the content translation overview link field handler. @@ -51,6 +52,11 @@ function setUp() { $user->langcode = 'en'; $user->save(); + // Assign user 2 LANGCODE_NOT_SPECIFIED code so entity can't be translated. + $user = user_load(2); + $user->langcode = Language::LANGCODE_NOT_SPECIFIED; + $user->save(); + ViewTestData::createTestViews(get_class($this), array('content_translation_test_views')); } diff --git a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityTranslationTest.php b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityTranslationTest.php index e819056..793366f 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityTranslationTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityTranslationTest.php @@ -45,6 +45,13 @@ protected function _testEntityLanguageMethods($entity_type) { 'name' => 'test', 'user_id' => $this->container->get('current_user')->id(), )); + $this->assertEqual($entity->language()->id, language_default()->id, format_string('%entity_type: Entity created with API has default language.', array('%entity_type' => $entity_type))); + + $entity = entity_create($entity_type, array( + 'name' => 'test', + 'user_id' => $GLOBALS['user']->id(), + 'langcode' => Language::LANGCODE_NOT_SPECIFIED, + )); $this->assertEqual($entity->language()->id, Language::LANGCODE_NOT_SPECIFIED, format_string('%entity_type: Entity language not specified.', array('%entity_type' => $entity_type))); $this->assertFalse($entity->getTranslationLanguages(FALSE), format_string('%entity_type: No translations are available', array('%entity_type' => $entity_type))); @@ -150,7 +157,7 @@ protected function _testMultilingualProperties($entity_type) { // Create a language neutral entity and check that properties are stored // as language neutral. - $entity = entity_create($entity_type, array('name' => $name, 'user_id' => $uid)); + $entity = entity_create($entity_type, array('name' => $name, 'user_id' => $uid, 'langcode' => Language::LANGCODE_NOT_SPECIFIED)); $entity->save(); $entity = entity_load($entity_type, $entity->id()); $default_langcode = $entity->language()->id; @@ -232,6 +239,7 @@ protected function _testMultilingualProperties($entity_type) { entity_create($entity_type, array( 'user_id' => $properties[$langcode]['user_id'], 'name' => 'some name', + 'langcode' => Language::LANGCODE_NOT_SPECIFIED, ))->save(); $entities = entity_load_multiple($entity_type); @@ -294,7 +302,7 @@ function testEntityTranslationAPI() { $langcode = $this->langcodes[1]; $entity = $this->entityManager ->getStorageController('entity_test_mul') - ->create(array('name' => $this->randomName())); + ->create(array('name' => $this->randomName(), 'langcode' => Language::LANGCODE_NOT_SPECIFIED)); $entity->save(); $hooks = $this->getHooksInfo();