diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockTypeFormController.php b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockTypeFormController.php index a2f3008..5b7c744 100644 --- a/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockTypeFormController.php +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockTypeFormController.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/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/hal/lib/Drupal/hal/Normalizer/ContentEntityNormalizer.php b/core/modules/hal/lib/Drupal/hal/Normalizer/ContentEntityNormalizer.php index f374fa3..e77be11 100644 --- a/core/modules/hal/lib/Drupal/hal/Normalizer/ContentEntityNormalizer.php +++ b/core/modules/hal/lib/Drupal/hal/Normalizer/ContentEntityNormalizer.php @@ -126,21 +126,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 = Language::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 71db626..67726c1 100644 --- a/core/modules/language/language.module +++ b/core/modules/language/language.module @@ -708,3 +708,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/lib/Drupal/language/DefaultLanguageItem.php b/core/modules/language/lib/Drupal/language/DefaultLanguageItem.php new file mode 100644 index 0000000..0ef14e2 --- /dev/null +++ b/core/modules/language/lib/Drupal/language/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/Drupal/language/Tests/EntityDefaultLanguageTest.php b/core/modules/language/tests/Drupal/language/Tests/EntityDefaultLanguageTest.php new file mode 100644 index 0000000..0b2a3ca --- /dev/null +++ b/core/modules/language/tests/Drupal/language/Tests/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/node/lib/Drupal/node/Controller/NodeController.php b/core/modules/node/lib/Drupal/node/Controller/NodeController.php index b16b93d..d0b74ed 100644 --- a/core/modules/node/lib/Drupal/node/Controller/NodeController.php +++ b/core/modules/node/lib/Drupal/node/Controller/NodeController.php @@ -63,13 +63,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/shortcut/lib/Drupal/shortcut/Controller/ShortcutController.php b/core/modules/shortcut/lib/Drupal/shortcut/Controller/ShortcutController.php index 8f3b481..943caa0 100644 --- a/core/modules/shortcut/lib/Drupal/shortcut/Controller/ShortcutController.php +++ b/core/modules/shortcut/lib/Drupal/shortcut/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/lib/Drupal/system/Tests/Entity/EntityTranslationTest.php b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityTranslationTest.php index 58ad4c3..b776ed9 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityTranslationTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityTranslationTest.php @@ -44,6 +44,13 @@ protected function _testEntityLanguageMethods($entity_type) { 'name' => 'test', 'user_id' => $this->container->get('current_user')->id(), )); + $this->assertEqual($entity->language()->id, $this->languageManager->getCurrentLanguage(Language::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' => $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))); @@ -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' => Language::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' => Language::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' => Language::LANGCODE_NOT_SPECIFIED)); $entity->save(); $hooks = $this->getHooksInfo(); diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Controller/TaxonomyController.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Controller/TaxonomyController.php index 0312a9d..0ddbce4 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Controller/TaxonomyController.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/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); }