Commit 9c537ec4 authored by webchick's avatar webchick

Issue #1966436 by naveenvalecha, cesarmiquel, Berdir, mr.york, jlbellido,...

Issue #1966436 by naveenvalecha, cesarmiquel, Berdir, mr.york, jlbellido, segi, vasi1186, Leksat, Gábor Hojtsy, Schnitzel, grisendo, Aron Novak, likin, penyaskito, vijaycs85, kfritsche, fago: Fixed Default *content* entity languages are not set for entities created with the API.
parent 1b97ba13
...@@ -588,9 +588,16 @@ protected function setDefaultLangcode() { ...@@ -588,9 +588,16 @@ protected function setDefaultLangcode() {
if ($this->hasField('langcode') && ($item = $this->get('langcode')) && isset($item->language)) { if ($this->hasField('langcode') && ($item = $this->get('langcode')) && isset($item->language)) {
$this->defaultLangcode = $item->language->id; $this->defaultLangcode = $item->language->id;
} }
if (empty($this->defaultLangcode)) { if (empty($this->defaultLangcode)) {
// Make sure we return a proper language object. // Make sure we return a proper language object, if the entity has a
$this->defaultLangcode = LanguageInterface::LANGCODE_NOT_SPECIFIED; // langcode field, default to the site's default language.
if ($this->hasField('langcode')) {
$this->defaultLangcode = $this->languageManager()->getDefaultLanguage()->getId();
}
else {
$this->defaultLangcode = LanguageInterface::LANGCODE_NOT_SPECIFIED;
}
} }
// This needs to be initialized manually as it is skipped when instantiating // This needs to be initialized manually as it is skipped when instantiating
// the language field object to avoid infinite recursion. // the language field object to avoid infinite recursion.
......
...@@ -91,8 +91,9 @@ public function setValue($values, $notify = TRUE) { ...@@ -91,8 +91,9 @@ public function setValue($values, $notify = TRUE) {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function applyDefaultValue($notify = TRUE) { public function applyDefaultValue($notify = TRUE) {
// Default to LANGCODE_NOT_SPECIFIED. // Default to the site's default language. When language module is enabled,
$this->setValue(array('value' => LanguageInterface::LANGCODE_NOT_SPECIFIED), $notify); // this behavior is configurable, see language_field_info_alter().
$this->setValue(array('value' => \Drupal::languageManager()->getDefaultLanguage()->getId()), $notify);
return $this; return $this;
} }
......
...@@ -56,7 +56,7 @@ public function form(array $form, FormStateInterface $form_state) { ...@@ -56,7 +56,7 @@ public function form(array $form, FormStateInterface $form_state) {
'#description' => t('Create a new revision by default for this block type.') '#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( $form['language'] = array(
'#type' => 'details', '#type' => 'details',
'#title' => t('Language settings'), '#title' => t('Language settings'),
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
use Drupal\views\Tests\ViewTestBase; use Drupal\views\Tests\ViewTestBase;
use Drupal\content_translation\Tests\ContentTranslationTestBase; use Drupal\content_translation\Tests\ContentTranslationTestBase;
use Drupal\views\Tests\ViewTestData; use Drupal\views\Tests\ViewTestData;
use Drupal\Core\Language\Language;
/** /**
* Tests the content translation overview link field handler. * Tests the content translation overview link field handler.
...@@ -44,6 +45,11 @@ protected function setUp() { ...@@ -44,6 +45,11 @@ protected function setUp() {
$user->langcode = 'en'; $user->langcode = 'en';
$user->save(); $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')); ViewTestData::createTestViews(get_class($this), array('content_translation_test_views'));
} }
......
...@@ -7,8 +7,6 @@ ...@@ -7,8 +7,6 @@
namespace Drupal\field\Tests; namespace Drupal\field\Tests;
use Drupal\Core\Language\LanguageInterface;
/** /**
* Tests the field display API. * Tests the field display API.
* *
...@@ -141,7 +139,7 @@ function testFieldItemListView() { ...@@ -141,7 +139,7 @@ function testFieldItemListView() {
$setting = $display['settings']['test_formatter_setting_multiple']; $setting = $display['settings']['test_formatter_setting_multiple'];
$this->assertNoText($this->label, 'Label was not displayed.'); $this->assertNoText($this->label, 'Label was not displayed.');
$this->assertText('field_test_entity_display_build_alter', 'Alter fired, display passed.'); $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(); $array = array();
foreach ($this->values as $delta => $value) { foreach ($this->values as $delta => $value) {
$array[] = $delta . ':' . $value['value']; $array[] = $delta . ':' . $value['value'];
...@@ -161,7 +159,7 @@ function testFieldItemListView() { ...@@ -161,7 +159,7 @@ function testFieldItemListView() {
$setting = $display['settings']['test_formatter_setting_multiple']; $setting = $display['settings']['test_formatter_setting_multiple'];
$this->assertRaw('visually-hidden', 'Label was visually hidden.'); $this->assertRaw('visually-hidden', 'Label was visually hidden.');
$this->assertText('field_test_entity_display_build_alter', 'Alter fired, display passed.'); $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(); $array = array();
foreach ($this->values as $delta => $value) { foreach ($this->values as $delta => $value) {
$array[] = $delta . ':' . $value['value']; $array[] = $delta . ':' . $value['value'];
......
...@@ -40,12 +40,11 @@ function testFileSave() { ...@@ -40,12 +40,11 @@ function testFileSave() {
$this->assertEqual($loaded_file->isPermanent(), $file->isPermanent(), 'Status was saved correctly.'); $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->assertEqual($file->getSize(), filesize($file->getFileUri()), 'File size was set correctly.', 'File');
$this->assertTrue($file->getChangedTime() > 1, '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. // Resave the file, updating the existing record.
file_test_reset(); file_test_reset();
$file->status->value = 7; $file->status->value = 7;
$file->langcode = 'en';
$file->save(); $file->save();
// Check that the correct hooks were called. // Check that the correct hooks were called.
......
...@@ -10,7 +10,6 @@ ...@@ -10,7 +10,6 @@
use Drupal\Component\Utility\NestedArray; use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\rest\LinkManager\LinkManagerInterface; use Drupal\rest\LinkManager\LinkManagerInterface;
use Symfony\Component\Serializer\Exception\UnexpectedValueException; use Symfony\Component\Serializer\Exception\UnexpectedValueException;
...@@ -126,21 +125,15 @@ public function denormalize($data, $class, $format = NULL, array $context = arra ...@@ -126,21 +125,15 @@ public function denormalize($data, $class, $format = NULL, array $context = arra
// Create the entity. // Create the entity.
$typed_data_ids = $this->getTypedDataIds($data['_links']['type']); $typed_data_ids = $this->getTypedDataIds($data['_links']['type']);
$values = array();
// Figure out the language to use. // Figure out the language to use.
if (isset($data['langcode'])) { 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. // Remove the langcode so it does not get iterated over below.
unset($data['langcode']); 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']); $entity_type = $this->entityManager->getDefinition($typed_data_ids['entity_type']);
$values = array('langcode' => $langcode);
if ($entity_type->hasKey('bundle')) { if ($entity_type->hasKey('bundle')) {
$bundle_key = $entity_type->getKey('bundle'); $bundle_key = $entity_type->getKey('bundle');
......
...@@ -680,3 +680,11 @@ function language_system_regional_settings_form_submit($form, FormStateInterface ...@@ -680,3 +680,11 @@ function language_system_regional_settings_form_submit($form, FormStateInterface
$language->default = TRUE; $language->default = TRUE;
language_save($language); 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';
}
<?php
/**
* @file
* Contains \Drupal\language\DefaultLanguageItem.
*/
namespace Drupal\language;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\Plugin\Field\FieldType\LanguageItem;
use Drupal\Core\Language\Language;
/**
* Alternative plugin implementation of the 'language' field type.
*
* Replaces the Core 'language' entity field type implementation, changes the
* default values used.
*
* Required settings are:
* - target_type: The entity type to reference.
*
* @see language_field_info_alter().
*/
class DefaultLanguageItem extends LanguageItem {
/**
* {@inheritdoc}
*/
public function applyDefaultValue($notify = TRUE) {
// Default to LANGCODE_NOT_SPECIFIED.
$langcode = Language::LANGCODE_NOT_SPECIFIED;
if ($entity = $this->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());
}
}
<?php
/**
* @file
* Contains \Drupal\language\Tests\EntityDefaultLanguageTest.
*/
namespace Drupal\language\Tests;
use Drupal\Core\Language\LanguageInterface;
use Drupal\simpletest\KernelTestBase;
/**
* Tests default language code is properly generated for entities.
*/
class EntityDefaultLanguageTest extends KernelTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('language', 'node', 'field', 'text', 'user');
/**
* {@inheritdoc}
*/
public static function getInfo() {
return array(
'name' => '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 = $this->container->get('entity.manager')->getStorage('configurable_language')->create(array(
'id' => 'es',
));
$language->save();
// Create a new content type which has Undefined language by default.
$this->createContentType('ctund', LanguageInterface::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, LanguageInterface::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 site's default language code when language is not specified.
$node = $this->createNode('ctund');
$this->assertEqual($node->langcode->value, 'en');
// 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 site's
// default language code when language is not specified.
$node = $this->createNode('ctes');
$this->assertEqual($node->langcode->value, 'en');
// 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->randomString(),
);
if (!empty($langcode)) {
$values['langcode'] = $langcode;
}
$node = $this->container->get('entity.manager')->getStorage('node')->create($values);
return $node;
}
}
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
namespace Drupal\migrate_drupal\Tests\d6; namespace Drupal\migrate_drupal\Tests\d6;
use Drupal\aggregator\Entity\Feed; use Drupal\aggregator\Entity\Feed;
use Drupal\Core\Language\LanguageInterface;
use Drupal\migrate\MigrateExecutable; use Drupal\migrate\MigrateExecutable;
use Drupal\migrate_drupal\Tests\MigrateDrupalTestBase; use Drupal\migrate_drupal\Tests\MigrateDrupalTestBase;
...@@ -43,7 +42,7 @@ public function testAggregatorFeedImport() { ...@@ -43,7 +42,7 @@ public function testAggregatorFeedImport() {
$feed = entity_load('aggregator_feed', 5); $feed = entity_load('aggregator_feed', 5);
$this->assertNotNull($feed->uuid()); $this->assertNotNull($feed->uuid());
$this->assertEqual($feed->title->value, 'Know Your Meme'); $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->url->value, 'http://knowyourmeme.com/newsfeed.rss');
$this->assertEqual($feed->refresh->value, 900); $this->assertEqual($feed->refresh->value, 900);
$this->assertEqual($feed->checked->value, 1387659487); $this->assertEqual($feed->checked->value, 1387659487);
......
...@@ -67,7 +67,7 @@ public function testAggregatorItem() { ...@@ -67,7 +67,7 @@ public function testAggregatorItem() {
$this->assertEqual($item->getDescription(), "<h2 id='new'>What's new with Drupal 8?</h2>"); $this->assertEqual($item->getDescription(), "<h2 id='new'>What's new with Drupal 8?</h2>");
$this->assertEqual($item->getLink(), 'https://groups.drupal.org/node/395218'); $this->assertEqual($item->getLink(), 'https://groups.drupal.org/node/395218');
$this->assertEqual($item->getPostedTime(), 1389297196); $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'); $this->assertEqual($item->getGuid(), '395218 at https://groups.drupal.org');
} }
......
...@@ -51,7 +51,7 @@ public function testBlockMigration() { ...@@ -51,7 +51,7 @@ public function testBlockMigration() {
$this->assertEqual('My block 1', $block->label()); $this->assertEqual('My block 1', $block->label());
$this->assertEqual(1, $block->getRevisionId()); $this->assertEqual(1, $block->getRevisionId());
$this->assertTrue(REQUEST_TIME <= $block->getChangedTime() && $block->getChangedTime() <= time()); $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('<h3>My first custom block body</h3>', $block->body->value); $this->assertEqual('<h3>My first custom block body</h3>', $block->body->value);
$this->assertEqual('full_html', $block->body->format); $this->assertEqual('full_html', $block->body->format);
...@@ -59,7 +59,7 @@ public function testBlockMigration() { ...@@ -59,7 +59,7 @@ public function testBlockMigration() {
$this->assertEqual('My block 2', $block->label()); $this->assertEqual('My block 2', $block->label());
$this->assertEqual(2, $block->getRevisionId()); $this->assertEqual(2, $block->getRevisionId());
$this->assertTrue(REQUEST_TIME <= $block->getChangedTime() && $block->getChangedTime() <= time()); $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('<h3>My second custom block body</h3>', $block->body->value); $this->assertEqual('<h3>My second custom block body</h3>', $block->body->value);
$this->assertEqual('full_html', $block->body->format); $this->assertEqual('full_html', $block->body->format);
} }
......
...@@ -81,7 +81,7 @@ public function testComments() { ...@@ -81,7 +81,7 @@ public function testComments() {
$this->assertEqual(0, $comment->pid->target_id); $this->assertEqual(0, $comment->pid->target_id);
$this->assertEqual(1, $comment->getCommentedEntityId()); $this->assertEqual(1, $comment->getCommentedEntityId());
$this->assertEqual('node', $comment->getCommentedEntityTypeId()); $this->assertEqual('node', $comment->getCommentedEntityTypeId());
$this->assertEqual(LanguageInterface::LANGCODE_NOT_SPECIFIED, $comment->language()->id); $this->assertEqual('en', $comment->language()->id);
$this->assertEqual('comment_no_subject', $comment->getTypeId()); $this->assertEqual('comment_no_subject', $comment->getTypeId());
$comment = entity_load('comment', 2); $comment = entity_load('comment', 2);
......
...@@ -92,13 +92,11 @@ public function addPage() { ...@@ -92,13 +92,11 @@ public function addPage() {
*/ */
public function add(NodeTypeInterface $node_type) { public function add(NodeTypeInterface $node_type) {
$account = $this->currentUser(); $account = $this->currentUser();
$langcode = $this->moduleHandler()->invoke('language', 'get_default_langcode', array('node', $node_type->type));
$node = $this->entityManager()->getStorage('node')->create(array( $node = $this->entityManager()->getStorage('node')->create(array(
'uid' => $account->id(), 'uid' => $account->id(),
'name' => $account->getUsername() ?: '', 'name' => $account->getUsername() ?: '',
'type' => $node_type->type, 'type' => $node_type->type,
'langcode' => $langcode ? $langcode : $this->languageManager()->getCurrentLanguage()->id,
)); ));
$form = $this->entityFormBuilder()->getForm($node); $form = $this->entityFormBuilder()->getForm($node);
......
...@@ -77,7 +77,7 @@ public function testNormalize() { ...@@ -77,7 +77,7 @@ public function testNormalize() {
array('value' => $this->entity->uuid()), array('value' => $this->entity->uuid()),
), ),
'langcode' => array( 'langcode' => array(
array('value' => LanguageInterface::LANGCODE_NOT_SPECIFIED), array('value' => 'en'),
), ),
'name' => array( 'name' => array(
array('value' => $this->values['name']), array('value' => $this->values['name']),
...@@ -132,7 +132,7 @@ public function testSerialize() { ...@@ -132,7 +132,7 @@ public function testSerialize() {
$expected = array( $expected = array(
'id' => '<id><value>' . $this->entity->id() . '</value></id>', 'id' => '<id><value>' . $this->entity->id() . '</value></id>',
'uuid' => '<uuid><value>' . $this->entity->uuid() . '</value></uuid>', 'uuid' => '<uuid><value>' . $this->entity->uuid() . '</value></uuid>',
'langcode' => '<langcode><value>' . LanguageInterface::LANGCODE_NOT_SPECIFIED . '</value></langcode>', 'langcode' => '<langcode><value>en</value></langcode>',
'name' => '<name><value>' . $this->values['name'] . '</value></name>', 'name' => '<name><value>' . $this->values['name'] . '</value></name>',
'type' => '<type><value>entity_test_mulrev</value></type>', 'type' => '<type><value>entity_test_mulrev</value></type>',
'user_id' => '<user_id><target_id>' . $this->values['user_id'] . '</target_id></user_id>', 'user_id' => '<user_id><target_id>' . $this->values['user_id'] . '</target_id></user_id>',
......
...@@ -9,7 +9,6 @@ ...@@ -9,7 +9,6 @@
use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Controller\ControllerBase;
use Drupal\shortcut\ShortcutSetInterface; use Drupal\shortcut\ShortcutSetInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/** /**
* Provides route responses for taxonomy.module. * Provides route responses for taxonomy.module.
...@@ -28,9 +27,6 @@ class ShortcutController extends ControllerBase { ...@@ -28,9 +27,6 @@ class ShortcutController extends ControllerBase {
*/ */
public function addForm(ShortcutSetInterface $shortcut_set) { public function addForm(ShortcutSetInterface $shortcut_set) {
$shortcut = $this->entityManager()->getStorage('shortcut')->create(array('shortcut_set' => $shortcut_set->id())); $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'); return $this->entityFormBuilder()->getForm($shortcut, 'add');
} }
......
...@@ -49,7 +49,7 @@ public function testDefaultValues() { ...@@ -49,7 +49,7 @@ public function testDefaultValues() {
*/ */
protected function assertDefaultValues($entity_type) { protected function assertDefaultValues($entity_type) {
$entity = entity_create($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->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.'); $this->assertEqual($entity->name->getValue(), array(0 => array('value' => NULL)), 'Field has one empty value by default.');
} }
......
...@@ -106,6 +106,8 @@ public function testReadWrite() { ...@@ -106,6 +106,8 @@ public function testReadWrite() {
protected function assertReadWrite($entity_type) { protected function assertReadWrite($entity_type) {
$entity = $this->createTestEntity($entity_type); $entity = $this->createTestEntity($entity_type);
$langcode = 'en';
// Access the name field. // 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 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))); $this->assertTrue($entity->name[0] instanceof FieldItemInterface, format_string('%entity_type: Field item implements interface', array('%entity_type' => $entity_type)));
...@@ -194,8 +196,8 @@ protected function assertReadWrite($entity_type) { ...@@ -194,8 +196,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))); $this->assertFalse(isset($entity->name->value), format_string('%entity_type: Name is not set.', array('%entity_type' => $entity_type)));
// Access the language field. // 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($langcode, $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(\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. // Change the language by code.
$entity->langcode->value = \Drupal::languageManager()->getDefaultLanguage()->id; $entity->langcode->value = \Drupal::languageManager()->getDefaultLanguage()->id;
...@@ -203,7 +205,7 @@ protected function assertReadWrite($entity_type) { ...@@ -203,7 +205,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))); $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. // 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(); $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()->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))); $this->assertEqual(\Drupal::languageManager()->getDefaultLanguage(), $entity->langcode->language, format_string('%entity_type: Language object can be read.', array('%entity_type' => $entity_type)));
...@@ -333,8 +335,8 @@ protected function assertSave($entity_type) { ...@@ -333,8 +335,8 @@ protected function assertSave($entity_type) {
// Access the name field. // 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->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->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('en', $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(\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->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_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))); $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)));
...@@ -508,7 +510,7 @@ protected function assertDataStructureInterfaces($entity_type) { ...@@ -508,7 +510,7 @@ protected function assertDataStructureInterfaces($entity_type) {
// the user name and other user entity strings as well. // the user name and other user entity strings as well.
$target_strings = array( $target_strings = array(
$entity->uuid->value, $entity->uuid->value,
LanguageInterface::LANGCODE_NOT_SPECIFIED, 'en',
$this->entity_name, $this->entity_name,
// Bundle name. // Bundle name.
$entity->bundle(), $entity->bundle(),
......
...@@ -38,7 +38,13 @@ protected function _testEntityLanguageMethods($entity_type) { ...@@ -38,7 +38,13 @@ protected function _testEntityLanguageMethods($entity_type) {