diff --git a/core/lib/Drupal/Core/Entity/ContentEntityBase.php b/core/lib/Drupal/Core/Entity/ContentEntityBase.php index 2ece2b5..0b0b4f8 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityBase.php @@ -8,7 +8,6 @@ namespace Drupal\Core\Entity; use Drupal\Component\Utility\String; -use Drupal\Core\Entity\Plugin\DataType\EntityReference; use Drupal\Core\Entity\TypedData\EntityDataDefinition; use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Session\AccountInterface; @@ -576,9 +575,16 @@ protected function setDefaultLangcode() { if ($this->hasField('langcode') && ($item = $this->get('langcode')) && isset($item->language)) { $this->defaultLangcode = $item->language->id; } + if (empty($this->defaultLangcode)) { - // Make sure we return a proper language object. - $this->defaultLangcode = LanguageInterface::LANGCODE_NOT_SPECIFIED; + // Make sure we return a proper language object, if the entity has a + // langcode field, default to the current language. + if ($this->hasField('langcode')) { + $this->defaultLangcode = $this->languageManager()->getCurrentLanguage()->getId(); + } + else { + $this->defaultLangcode = LanguageInterface::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 719a0d6..b2bcde1 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/LanguageItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/LanguageItem.php @@ -91,8 +91,8 @@ public function setValue($values, $notify = TRUE) { * {@inheritdoc} */ public function applyDefaultValue($notify = TRUE) { - // Default to LANGCODE_NOT_SPECIFIED. - $this->setValue(array('value' => LanguageInterface::LANGCODE_NOT_SPECIFIED), $notify); + // Default to the current site language. + $this->setValue(array('value' => \Drupal::languageManager()->getCurrentLanguage()->getId()), $notify); return $this; } diff --git a/core/modules/block_content/src/BlockContentTypeForm.php b/core/modules/block_content/src/BlockContentTypeForm.php index e64bfa3..cbb8fd5 100644 --- a/core/modules/block_content/src/BlockContentTypeForm.php +++ b/core/modules/block_content/src/BlockContentTypeForm.php @@ -55,7 +55,7 @@ public function form(array $form, array &$form_state) { '#description' => t('Create a new revision by default for this block type.') ); - if ($this->moduleHandler->moduleExists('content_translation')) { + if ($this->moduleHandler->moduleExists('language')) { $form['language'] = array( '#type' => 'details', '#title' => t('Language settings'), diff --git a/core/modules/content_translation/src/Tests/Views/TranslationLinkTest.php b/core/modules/content_translation/src/Tests/Views/TranslationLinkTest.php index e1b939c..3297e28 100644 --- a/core/modules/content_translation/src/Tests/Views/TranslationLinkTest.php +++ b/core/modules/content_translation/src/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/field/src/Tests/DisplayApiTest.php b/core/modules/field/src/Tests/DisplayApiTest.php index 0ee7c79..a9e3c3d 100644 --- a/core/modules/field/src/Tests/DisplayApiTest.php +++ b/core/modules/field/src/Tests/DisplayApiTest.php @@ -7,8 +7,6 @@ namespace Drupal\field\Tests; -use Drupal\Core\Language\LanguageInterface; - /** * Tests the field display API. */ @@ -149,7 +147,7 @@ function testFieldItemListView() { $setting = $display['settings']['test_formatter_setting_multiple']; $this->assertNoText($this->label, 'Label was not displayed.'); $this->assertText('field_test_entity_display_build_alter', 'Alter fired, display passed.'); - $this->assertText('entity language is ' . LanguageInterface::LANGCODE_NOT_SPECIFIED, 'Language is placed onto the context.'); + $this->assertText('entity language is en', 'Language is placed onto the context.'); $array = array(); foreach ($this->values as $delta => $value) { $array[] = $delta . ':' . $value['value']; diff --git a/core/modules/field/src/Tests/FieldAttachOtherTest.php b/core/modules/field/src/Tests/FieldAttachOtherTest.php index 73a3b1e..65bf3dc 100644 --- a/core/modules/field/src/Tests/FieldAttachOtherTest.php +++ b/core/modules/field/src/Tests/FieldAttachOtherTest.php @@ -7,8 +7,6 @@ namespace Drupal\field\Tests; -use Drupal\Core\Language\LanguageInterface; - /** * Unit test class for non-storage related entity field functions. * @@ -182,7 +180,7 @@ function testEntityDisplayViewMultiple() { function testFieldAttachCache() { // Initialize random values and a test entity. $entity_init = entity_create('entity_test', array('type' => $this->instance->bundle)); - $langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED; + $langcode = 'en'; $values = $this->_generateTestFieldValues($this->field->getCardinality()); // Non-cacheable entity type. diff --git a/core/modules/file/src/Tests/SaveTest.php b/core/modules/file/src/Tests/SaveTest.php index daa4048..9260154 100644 --- a/core/modules/file/src/Tests/SaveTest.php +++ b/core/modules/file/src/Tests/SaveTest.php @@ -46,12 +46,11 @@ function testFileSave() { $this->assertEqual($loaded_file->isPermanent(), $file->isPermanent(), 'Status was saved correctly.'); $this->assertEqual($file->getSize(), filesize($file->getFileUri()), 'File size was set correctly.', 'File'); $this->assertTrue($file->getChangedTime() > 1, 'File size was set correctly.', 'File'); - $this->assertEqual($loaded_file->langcode->value, LanguageInterface::LANGCODE_NOT_SPECIFIED, 'Langcode was defaulted correctly.'); + $this->assertEqual($loaded_file->langcode->value, 'en', 'Langcode was defaulted correctly.'); // Resave the file, updating the existing record. file_test_reset(); $file->status->value = 7; - $file->langcode = 'en'; $file->save(); // Check that the correct hooks were called. diff --git a/core/modules/hal/src/Normalizer/ContentEntityNormalizer.php b/core/modules/hal/src/Normalizer/ContentEntityNormalizer.php index d433656..cec6b4a 100644 --- a/core/modules/hal/src/Normalizer/ContentEntityNormalizer.php +++ b/core/modules/hal/src/Normalizer/ContentEntityNormalizer.php @@ -10,7 +10,6 @@ use Drupal\Component\Utility\NestedArray; use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Extension\ModuleHandlerInterface; -use Drupal\Core\Language\LanguageInterface; use Drupal\rest\LinkManager\LinkManagerInterface; use Symfony\Component\Serializer\Exception\UnexpectedValueException; @@ -126,21 +125,15 @@ public function denormalize($data, $class, $format = NULL, array $context = arra // Create the entity. $typed_data_ids = $this->getTypedDataIds($data['_links']['type']); + $values = array(); // Figure out the language to use. if (isset($data['langcode'])) { - $langcode = $data['langcode'][0]['value']; + $values['langcode'] = $data['langcode'][0]['value']; // Remove the langcode so it does not get iterated over below. unset($data['langcode']); } - elseif ($this->moduleHandler->moduleExists('language')) { - $langcode = language_get_default_langcode($typed_data_ids['entity_type'], $typed_data_ids['bundle']); - } - else { - $langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED; - } $entity_type = $this->entityManager->getDefinition($typed_data_ids['entity_type']); - $values = array('langcode' => $langcode); if ($entity_type->hasKey('bundle')) { $bundle_key = $entity_type->getKey('bundle'); diff --git a/core/modules/language/language.module b/core/modules/language/language.module index e23d73f..a561f0a 100644 --- a/core/modules/language/language.module +++ b/core/modules/language/language.module @@ -679,3 +679,11 @@ function language_system_regional_settings_form_submit($form, &$form_state) { $language->default = TRUE; language_save($language); } + +/** + * Implements hook_field_info_alter(). + */ +function language_field_info_alter(&$info) { + // Change the default behavior of language field. + $info['language']['class'] = '\Drupal\language\DefaultLanguageItem'; +} diff --git a/core/modules/language/src/DefaultLanguageItem.php b/core/modules/language/src/DefaultLanguageItem.php new file mode 100644 index 0000000..0ef14e2 --- /dev/null +++ b/core/modules/language/src/DefaultLanguageItem.php @@ -0,0 +1,54 @@ +getEntity()) { + $langcode = $this->getDefaultLangcode($entity); + } + // Always notify otherwise default langcode will not be set correctly. + $this->setValue(array('value' => $langcode), TRUE); + return $this; + } + + /** + * Provides default language code of given entity. + * + * @param \Drupal\Core\Entity\EntityInterface $entity + * The entity whose language code to be loaded. + * + * @return string + * A string language code. + */ + public function getDefaultLangcode(EntityInterface $entity) { + return language_get_default_langcode($entity->getEntityTypeId(), $entity->bundle()); + } + +} diff --git a/core/modules/language/tests/src/EntityDefaultLanguageTest.php b/core/modules/language/tests/src/EntityDefaultLanguageTest.php new file mode 100644 index 0000000..0b2a3ca --- /dev/null +++ b/core/modules/language/tests/src/EntityDefaultLanguageTest.php @@ -0,0 +1,151 @@ + 'Entity default language', + 'description' => 'Test that entities are created with correct language code.', + 'group' => 'Entity API', + ); + } + + /** + * {@inheritdoc} + */ + public function setUp() { + parent::setUp(); + + // Activate Spanish language, so there are two languages activated. + $language_entity = $this->container->get('entity.manager')->getStorage('language_entity')->create(array( + 'id' => 'es', + )); + $language_entity->save(); + + // Create a new content type which has Undefined language by default. + $this->createContentType('ctund', Language::LANGCODE_NOT_SPECIFIED); + // Create a new content type which has Spanish language by default. + $this->createContentType('ctes', 'es'); + } + + /** + * Tests that default language code is properly set for new nodes. + */ + public function testEntityTranslationDefaultLanguageViaCode() { + // With language module activated, and a content type that is configured to + // have no language by default, a new node of this content type will have + // "und" language code when language is not specified. + $node = $this->createNode('ctund'); + $this->assertEqual($node->langcode->value, Language::LANGCODE_NOT_SPECIFIED); + // With language module activated, and a content type that is configured to + // have no language by default, a new node of this content type will have + // "es" language code when language is specified as "es". + $node = $this->createNode('ctund', 'es'); + $this->assertEqual($node->langcode->value, 'es'); + + // With language module activated, and a content type that is configured to + // have language "es" by default, a new node of this content type will have + // "es" language code when language is not specified. + $node = $this->createNode('ctes'); + $this->assertEqual($node->langcode->value, 'es'); + // With language module activated, and a content type that is configured to + // have language "es" by default, a new node of this content type will have + // "en" language code when language "en" is specified. + $node = $this->createNode('ctes', 'en'); + $this->assertEqual($node->langcode->value, 'en'); + + // Disable language module. + $this->disableModules(array('language')); + + // With language module disabled, and a content type that is configured to + // have no language specified by default, a new node of this content type + // will have "und" language code when language is not specified. + $node = $this->createNode('ctund'); + $this->assertEqual($node->langcode->value, Language::LANGCODE_NOT_SPECIFIED); + // With language module disabled, and a content type that is configured to + // have no language specified by default, a new node of this type will have + // "es" language code when language "es" is specified. + $node = $this->createNode('ctund', 'es'); + $this->assertEqual($node->langcode->value, 'es'); + + // With language module disabled, and a content type that is configured to + // have language "es" by default, a new node of this type will have "und" + // language code when language is not specified. + $node = $this->createNode('ctes'); + $this->assertEqual($node->langcode->value, Language::LANGCODE_NOT_SPECIFIED); + // With language module disabled, and a content type that is configured to + // have language "es" by default, a new node of this type will have "en" + // language code when language "en" is specified. + $node = $this->createNode('ctes', 'en'); + $this->assertEqual($node->langcode->value, 'en'); + } + + /** + * Creates a new node content type. + * + * @param name + * The content type name. + * @param $langcode + * Default language code of the nodes of this type. + */ + protected function createContentType($name, $langcode) { + $content_type = $this->container->get('entity.manager')->getStorage('node_type')->create(array( + 'name' => 'Test ' . $name, + 'title_label' => 'Title', + 'type' => $name, + 'create_body' => FALSE, + )); + $content_type->save(); + language_save_default_configuration('node', $name, array( + 'langcode' => $langcode, + 'language_show' => FALSE, + )); + } + + /** + * Creates a new node of given type and language using Entity API. + * + * @param $type + * The node content type. + * @param $langcode + * (optional) Language code to pass to entity create. + * + * @return \Drupal\node\NodeInterface + * The node created. + */ + protected function createNode($type, $langcode = NULL) { + $values = array( + 'type' => $type, + 'title' => $this->randomName(), + ); + if (!empty($langcode)) { + $values['langcode'] = $langcode; + } + $node = $this->container->get('entity.manager')->getStorage('node')->create($values); + return $node; + } + +} diff --git a/core/modules/migrate_drupal/src/Tests/d6/MigrateAggregatorFeedTest.php b/core/modules/migrate_drupal/src/Tests/d6/MigrateAggregatorFeedTest.php index 6b57800..5d0295f 100644 --- a/core/modules/migrate_drupal/src/Tests/d6/MigrateAggregatorFeedTest.php +++ b/core/modules/migrate_drupal/src/Tests/d6/MigrateAggregatorFeedTest.php @@ -8,7 +8,6 @@ namespace Drupal\migrate_drupal\Tests\d6; use Drupal\aggregator\Entity\Feed; -use Drupal\Core\Language\LanguageInterface; use Drupal\migrate\MigrateExecutable; use Drupal\migrate_drupal\Tests\MigrateDrupalTestBase; @@ -52,7 +51,7 @@ public function testAggregatorFeedImport() { $feed = entity_load('aggregator_feed', 5); $this->assertNotNull($feed->uuid()); $this->assertEqual($feed->title->value, 'Know Your Meme'); - $this->assertEqual($feed->language()->id, LanguageInterface::LANGCODE_NOT_SPECIFIED); + $this->assertEqual($feed->language()->id, 'en'); $this->assertEqual($feed->url->value, 'http://knowyourmeme.com/newsfeed.rss'); $this->assertEqual($feed->refresh->value, 900); $this->assertEqual($feed->checked->value, 1387659487); diff --git a/core/modules/migrate_drupal/src/Tests/d6/MigrateAggregatorItemTest.php b/core/modules/migrate_drupal/src/Tests/d6/MigrateAggregatorItemTest.php index 520dccf..170e2bd 100644 --- a/core/modules/migrate_drupal/src/Tests/d6/MigrateAggregatorItemTest.php +++ b/core/modules/migrate_drupal/src/Tests/d6/MigrateAggregatorItemTest.php @@ -76,7 +76,7 @@ public function testAggregatorItem() { $this->assertEqual($item->getDescription(), "

What's new with Drupal 8?

"); $this->assertEqual($item->getLink(), 'https://groups.drupal.org/node/395218'); $this->assertEqual($item->getPostedTime(), 1389297196); - $this->assertEqual($item->language()->id, LanguageInterface::LANGCODE_NOT_SPECIFIED); + $this->assertEqual($item->language()->id, 'en'); $this->assertEqual($item->getGuid(), '395218 at https://groups.drupal.org'); } diff --git a/core/modules/migrate_drupal/src/Tests/d6/MigrateBlockContentTest.php b/core/modules/migrate_drupal/src/Tests/d6/MigrateBlockContentTest.php index 460371a..1a977e3 100644 --- a/core/modules/migrate_drupal/src/Tests/d6/MigrateBlockContentTest.php +++ b/core/modules/migrate_drupal/src/Tests/d6/MigrateBlockContentTest.php @@ -60,7 +60,7 @@ public function testBlockMigration() { $this->assertEqual('My block 1', $block->label()); $this->assertEqual(1, $block->getRevisionId()); $this->assertTrue(REQUEST_TIME <= $block->getChangedTime() && $block->getChangedTime() <= time()); - $this->assertEqual(LanguageInterface::LANGCODE_NOT_SPECIFIED, $block->language()->id); + $this->assertEqual('en', $block->language()->id); $this->assertEqual('

My first custom block body

', $block->body->value); $this->assertEqual('full_html', $block->body->format); @@ -68,7 +68,7 @@ public function testBlockMigration() { $this->assertEqual('My block 2', $block->label()); $this->assertEqual(2, $block->getRevisionId()); $this->assertTrue(REQUEST_TIME <= $block->getChangedTime() && $block->getChangedTime() <= time()); - $this->assertEqual(LanguageInterface::LANGCODE_NOT_SPECIFIED, $block->language()->id); + $this->assertEqual('en', $block->language()->id); $this->assertEqual('

My second custom block body

', $block->body->value); $this->assertEqual('full_html', $block->body->format); } diff --git a/core/modules/migrate_drupal/src/Tests/d6/MigrateCommentTest.php b/core/modules/migrate_drupal/src/Tests/d6/MigrateCommentTest.php index d07b02b..79ce2e9 100644 --- a/core/modules/migrate_drupal/src/Tests/d6/MigrateCommentTest.php +++ b/core/modules/migrate_drupal/src/Tests/d6/MigrateCommentTest.php @@ -82,7 +82,7 @@ public function testComments() { $this->assertEqual(0, $comment->pid->target_id); $this->assertEqual(1, $comment->entity_id->target_id); $this->assertEqual('node', $comment->entity_type->value); - $this->assertEqual(LanguageInterface::LANGCODE_NOT_SPECIFIED, $comment->language()->id); + $this->assertEqual('en', $comment->language()->id); $comment = entity_load('comment', 2); $this->assertEqual('The response to the second comment.', $comment->subject->value); diff --git a/core/modules/node/src/Controller/NodeController.php b/core/modules/node/src/Controller/NodeController.php index 4f2dfbf..e91b584 100644 --- a/core/modules/node/src/Controller/NodeController.php +++ b/core/modules/node/src/Controller/NodeController.php @@ -92,13 +92,11 @@ public function addPage() { */ public function add(NodeTypeInterface $node_type) { $account = $this->currentUser(); - $langcode = $this->moduleHandler()->invoke('language', 'get_default_langcode', array('node', $node_type->type)); $node = $this->entityManager()->getStorage('node')->create(array( 'uid' => $account->id(), 'name' => $account->getUsername() ?: '', 'type' => $node_type->type, - 'langcode' => $langcode ? $langcode : $this->languageManager()->getCurrentLanguage()->id, )); $form = $this->entityFormBuilder()->getForm($node); diff --git a/core/modules/serialization/src/Tests/EntitySerializationTest.php b/core/modules/serialization/src/Tests/EntitySerializationTest.php index fc01ced..2e2ea03 100644 --- a/core/modules/serialization/src/Tests/EntitySerializationTest.php +++ b/core/modules/serialization/src/Tests/EntitySerializationTest.php @@ -83,7 +83,7 @@ public function testNormalize() { array('value' => $this->entity->uuid()), ), 'langcode' => array( - array('value' => LanguageInterface::LANGCODE_NOT_SPECIFIED), + array('value' => 'en'), ), 'name' => array( array('value' => $this->values['name']), @@ -138,7 +138,7 @@ public function testSerialize() { $expected = array( 'id' => '' . $this->entity->id() . '', 'uuid' => '' . $this->entity->uuid() . '', - 'langcode' => '' . LanguageInterface::LANGCODE_NOT_SPECIFIED . '', + 'langcode' => 'en', 'name' => '' . $this->values['name'] . '', 'type' => 'entity_test_mulrev', 'user_id' => '' . $this->values['user_id'] . '', diff --git a/core/modules/shortcut/src/Controller/ShortcutController.php b/core/modules/shortcut/src/Controller/ShortcutController.php index 8f3b481..943caa0 100644 --- a/core/modules/shortcut/src/Controller/ShortcutController.php +++ b/core/modules/shortcut/src/Controller/ShortcutController.php @@ -9,7 +9,6 @@ use Drupal\Core\Controller\ControllerBase; use Drupal\shortcut\ShortcutSetInterface; -use Symfony\Component\DependencyInjection\ContainerInterface; /** * Provides route responses for taxonomy.module. @@ -28,9 +27,6 @@ class ShortcutController extends ControllerBase { */ public function addForm(ShortcutSetInterface $shortcut_set) { $shortcut = $this->entityManager()->getStorage('shortcut')->create(array('shortcut_set' => $shortcut_set->id())); - if ($this->moduleHandler()->moduleExists('language')) { - $shortcut->langcode = language_get_default_langcode('shortcut', $shortcut_set->id()); - } return $this->entityFormBuilder()->getForm($shortcut, 'add'); } diff --git a/core/modules/system/src/Tests/Entity/EntityFieldDefaultValueTest.php b/core/modules/system/src/Tests/Entity/EntityFieldDefaultValueTest.php index 61b9aae..5d14c29 100644 --- a/core/modules/system/src/Tests/Entity/EntityFieldDefaultValueTest.php +++ b/core/modules/system/src/Tests/Entity/EntityFieldDefaultValueTest.php @@ -55,7 +55,7 @@ public function testDefaultValues() { */ protected function assertDefaultValues($entity_type) { $entity = entity_create($entity_type); - $this->assertEqual($entity->langcode->value, LanguageInterface::LANGCODE_NOT_SPECIFIED, String::format('%entity_type: Default language', array('%entity_type' => $entity_type))); + $this->assertEqual($entity->langcode->value, 'en', String::format('%entity_type: Default language', array('%entity_type' => $entity_type))); $this->assertTrue(Uuid::isValid($entity->uuid->value), String::format('%entity_type: Default UUID', array('%entity_type' => $entity_type))); $this->assertEqual($entity->name->getValue(), array(0 => array('value' => NULL)), 'Field has one empty value by default.'); } diff --git a/core/modules/system/src/Tests/Entity/EntityFieldTest.php b/core/modules/system/src/Tests/Entity/EntityFieldTest.php index b6859dc..0efbaee 100644 --- a/core/modules/system/src/Tests/Entity/EntityFieldTest.php +++ b/core/modules/system/src/Tests/Entity/EntityFieldTest.php @@ -97,6 +97,8 @@ public function testReadWrite() { protected function assertReadWrite($entity_type) { $entity = $this->createTestEntity($entity_type); + $langcode = 'en'; + // Access the name field. $this->assertTrue($entity->name instanceof FieldItemListInterface, format_string('%entity_type: Field implements interface', array('%entity_type' => $entity_type))); $this->assertTrue($entity->name[0] instanceof FieldItemInterface, format_string('%entity_type: Field item implements interface', array('%entity_type' => $entity_type))); @@ -185,8 +187,8 @@ protected function assertReadWrite($entity_type) { $this->assertFalse(isset($entity->name->value), format_string('%entity_type: Name is not set.', array('%entity_type' => $entity_type))); // Access the language field. - $this->assertEqual(LanguageInterface::LANGCODE_NOT_SPECIFIED, $entity->langcode->value, format_string('%entity_type: Language code can be read.', array('%entity_type' => $entity_type))); - $this->assertEqual(\Drupal::languageManager()->getLanguage(LanguageInterface::LANGCODE_NOT_SPECIFIED), $entity->langcode->language, format_string('%entity_type: Language object can be read.', array('%entity_type' => $entity_type))); + $this->assertEqual($langcode, $entity->langcode->value, format_string('%entity_type: Language code can be read.', array('%entity_type' => $entity_type))); + $this->assertEqual(\Drupal::languageManager()->getLanguage($langcode), $entity->langcode->language, format_string('%entity_type: Language object can be read.', array('%entity_type' => $entity_type))); // Change the language by code. $entity->langcode->value = \Drupal::languageManager()->getDefaultLanguage()->id; @@ -194,7 +196,7 @@ protected function assertReadWrite($entity_type) { $this->assertEqual(\Drupal::languageManager()->getDefaultLanguage(), $entity->langcode->language, format_string('%entity_type: Language object can be read.', array('%entity_type' => $entity_type))); // Revert language by code then try setting it by language object. - $entity->langcode->value = LanguageInterface::LANGCODE_NOT_SPECIFIED; + $entity->langcode->value = $langcode; $entity->langcode->language = \Drupal::languageManager()->getDefaultLanguage(); $this->assertEqual(\Drupal::languageManager()->getDefaultLanguage()->id, $entity->langcode->value, format_string('%entity_type: Language code can be read.', array('%entity_type' => $entity_type))); $this->assertEqual(\Drupal::languageManager()->getDefaultLanguage(), $entity->langcode->language, format_string('%entity_type: Language object can be read.', array('%entity_type' => $entity_type))); @@ -324,8 +326,8 @@ protected function assertSave($entity_type) { // Access the name field. $this->assertEqual(1, $entity->id->value, format_string('%entity_type: ID value can be read.', array('%entity_type' => $entity_type))); $this->assertTrue(is_string($entity->uuid->value), format_string('%entity_type: UUID value can be read.', array('%entity_type' => $entity_type))); - $this->assertEqual(LanguageInterface::LANGCODE_NOT_SPECIFIED, $entity->langcode->value, format_string('%entity_type: Language code can be read.', array('%entity_type' => $entity_type))); - $this->assertEqual(\Drupal::languageManager()->getLanguage(LanguageInterface::LANGCODE_NOT_SPECIFIED), $entity->langcode->language, format_string('%entity_type: Language object can be read.', array('%entity_type' => $entity_type))); + $this->assertEqual('en', $entity->langcode->value, format_string('%entity_type: Language code can be read.', array('%entity_type' => $entity_type))); + $this->assertEqual(\Drupal::languageManager()->getLanguage('en'), $entity->langcode->language, format_string('%entity_type: Language object can be read.', array('%entity_type' => $entity_type))); $this->assertEqual($this->entity_user->id(), $entity->user_id->target_id, format_string('%entity_type: User id can be read.', array('%entity_type' => $entity_type))); $this->assertEqual($this->entity_user->getUsername(), $entity->user_id->entity->name->value, format_string('%entity_type: User name can be read.', array('%entity_type' => $entity_type))); $this->assertEqual($this->entity_field_text, $entity->field_test_text->value, format_string('%entity_type: Text field can be read.', array('%entity_type' => $entity_type))); @@ -499,7 +501,7 @@ protected function assertDataStructureInterfaces($entity_type) { // the user name and other user entity strings as well. $target_strings = array( $entity->uuid->value, - LanguageInterface::LANGCODE_NOT_SPECIFIED, + 'en', $this->entity_name, // Bundle name. $entity->bundle(), diff --git a/core/modules/system/src/Tests/Entity/EntityTranslationTest.php b/core/modules/system/src/Tests/Entity/EntityTranslationTest.php index 8c2a440..226f67b 100644 --- a/core/modules/system/src/Tests/Entity/EntityTranslationTest.php +++ b/core/modules/system/src/Tests/Entity/EntityTranslationTest.php @@ -44,7 +44,14 @@ protected function _testEntityLanguageMethods($entity_type) { 'name' => 'test', 'user_id' => $this->container->get('current_user')->id(), )); - $this->assertEqual($entity->language()->id, LanguageInterface::LANGCODE_NOT_SPECIFIED, format_string('%entity_type: Entity language not specified.', array('%entity_type' => $entity_type))); + $this->assertEqual($entity->language()->getId(), $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->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' => \Drupal::currentUser()->id(), + 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED, + )); + $this->assertEqual($entity->language()->getId(), LanguageInterface::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))); // Set the value in default language. @@ -149,7 +156,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' => LanguageInterface::LANGCODE_NOT_SPECIFIED)); $entity->save(); $entity = entity_load($entity_type, $entity->id()); $default_langcode = $entity->language()->id; @@ -234,6 +241,7 @@ protected function _testMultilingualProperties($entity_type) { entity_create($entity_type, array( 'user_id' => $properties[$langcode]['user_id'], 'name' => 'some name', + 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED, ))->save(); $entities = entity_load_multiple($entity_type); @@ -296,7 +304,7 @@ function testEntityTranslationAPI() { $langcode = $this->langcodes[1]; $entity = $this->entityManager ->getStorage('entity_test_mul') - ->create(array('name' => $this->randomName())); + ->create(array('name' => $this->randomName(), 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED)); $entity->save(); $hooks = $this->getHooksInfo(); diff --git a/core/modules/taxonomy/src/Controller/TaxonomyController.php b/core/modules/taxonomy/src/Controller/TaxonomyController.php index 0312a9d..0ddbce4 100644 --- a/core/modules/taxonomy/src/Controller/TaxonomyController.php +++ b/core/modules/taxonomy/src/Controller/TaxonomyController.php @@ -11,7 +11,6 @@ use Drupal\Core\Controller\ControllerBase; use Drupal\taxonomy\TermInterface; use Drupal\taxonomy\VocabularyInterface; -use Symfony\Component\DependencyInjection\ContainerInterface; /** * Provides route responses for taxonomy.module. @@ -29,9 +28,6 @@ class TaxonomyController extends ControllerBase { */ public function addForm(VocabularyInterface $taxonomy_vocabulary) { $term = $this->entityManager()->getStorage('taxonomy_term')->create(array('vid' => $taxonomy_vocabulary->id())); - if ($this->moduleHandler()->moduleExists('language')) { - $term->langcode = language_get_default_langcode('taxonomy_term', $taxonomy_vocabulary->id()); - } return $this->entityFormBuilder()->getForm($term); } diff --git a/core/tests/Drupal/Tests/Core/Entity/ContentEntityBaseUnitTest.php b/core/tests/Drupal/Tests/Core/Entity/ContentEntityBaseUnitTest.php index 08fa85c..9e95c08 100644 --- a/core/tests/Drupal/Tests/Core/Entity/ContentEntityBaseUnitTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/ContentEntityBaseUnitTest.php @@ -150,6 +150,9 @@ public function setUp() { ->method('getLanguage') ->with('en') ->will($this->returnValue($language)); + $this->languageManager->expects($this->any()) + ->method('getCurrentLanguage') + ->will($this->returnValue($language)); $this->fieldTypePluginManager = $this->getMockBuilder('\Drupal\Core\Field\FieldTypePluginManager') ->disableOriginalConstructor() diff --git a/core/tests/Drupal/Tests/Core/Entity/ContentEntityDatabaseStorageTest.php b/core/tests/Drupal/Tests/Core/Entity/ContentEntityDatabaseStorageTest.php index c743200..884cf4e 100644 --- a/core/tests/Drupal/Tests/Core/Entity/ContentEntityDatabaseStorageTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/ContentEntityDatabaseStorageTest.php @@ -9,6 +9,7 @@ use Drupal\Core\Entity\ContentEntityDatabaseStorage; use Drupal\Core\Field\FieldDefinition; +use Drupal\Core\Language\Language; use Drupal\Tests\UnitTestCase; use Symfony\Component\DependencyInjection\ContainerBuilder; @@ -998,6 +999,11 @@ public function testCreate() { $language_manager = $this->getMock('Drupal\Core\Language\LanguageManagerInterface'); $module_handler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface'); + $language = new Language(array('id' => 'en')); + $language_manager->expects($this->any()) + ->method('getCurrentLanguage') + ->will($this->returnValue($language)); + $container = new ContainerBuilder(); $container->set('language_manager', $language_manager); $container->set('entity.manager', $this->entityManager); diff --git a/core/tests/Drupal/Tests/Core/Entity/KeyValueStore/KeyValueEntityStorageTest.php b/core/tests/Drupal/Tests/Core/Entity/KeyValueStore/KeyValueEntityStorageTest.php index bc21d8a..9c20d1f 100644 --- a/core/tests/Drupal/Tests/Core/Entity/KeyValueStore/KeyValueEntityStorageTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/KeyValueStore/KeyValueEntityStorageTest.php @@ -126,9 +126,13 @@ protected function setUpKeyValueEntityStorage($uuid_key = 'uuid') { $this->moduleHandler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface'); $this->uuidService = $this->getMock('Drupal\Component\Uuid\UuidInterface'); $this->languageManager = $this->getMock('Drupal\Core\Language\LanguageManagerInterface'); + $language = new Language(array('langcode' => 'en')); $this->languageManager->expects($this->any()) ->method('getDefaultLanguage') - ->will($this->returnValue(new Language(array('langcode' => 'en')))); + ->will($this->returnValue($language)); + $this->languageManager->expects($this->any()) + ->method('getCurrentLanguage') + ->will($this->returnValue($language)); $this->entityStorage = new KeyValueEntityStorage($this->entityType, $this->keyValueStore, $this->uuidService, $this->languageManager); $this->entityStorage->setModuleHandler($this->moduleHandler);