Commit b35d190d authored by alexpott's avatar alexpott

Issue #2160965 by herom, dawehner, robertdbailey, Berdir, fran seva: Fixed...

Issue #2160965 by herom, dawehner, robertdbailey, Berdir, fran seva: Fixed Content entities are not upcast in the page language, inconsistent with config entities.
parent aa3ff14b
......@@ -7,7 +7,9 @@
namespace Drupal\Core\ParamConverter;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\TypedData\TranslatableInterface;
use Symfony\Component\Routing\Route;
/**
......@@ -64,7 +66,13 @@ public function __construct(EntityManagerInterface $entity_manager) {
public function convert($value, $definition, $name, array $defaults) {
$entity_type_id = $this->getEntityTypeFromDefaults($definition, $name, $defaults);
if ($storage = $this->entityManager->getStorage($entity_type_id)) {
return $storage->load($value);
$entity = $storage->load($value);
// If the entity type is translatable, ensure we return the proper
// translation object for the current context.
if ($entity instanceof EntityInterface && $entity instanceof TranslatableInterface) {
$entity = $this->entityManager->getTranslationFromContext($entity, NULL, array('operation' => 'entity_upcast'));
}
return $entity;
}
}
......
......@@ -72,6 +72,7 @@ public function getCancelUrl() {
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Remove the translated values.
$this->entity = $this->entity->getUntranslated();
$this->entity->removeTranslation($this->language->id);
$this->entity->save();
......
......@@ -107,6 +107,7 @@ function testEntityFormLanguage() {
// Create a body translation and check the form language.
$langcode2 = $this->langcodes[1];
$node->getTranslation($langcode2)->body->value = $this->randomMachineName(16);
$node->getTranslation($langcode2)->setOwnerId($web_user->id());
$node->save();
$this->drupalGet($langcode2 . '/node/' . $node->id() . '/edit');
$form_langcode = \Drupal::state()->get('entity_test.form_langcode');
......
......@@ -8,6 +8,7 @@
namespace Drupal\system\Tests\ParamConverter;
use Drupal\simpletest\WebTestBase;
use Drupal\language\Entity\ConfigurableLanguage;
/**
* Tests upcasting of url arguments to entities.
......@@ -16,7 +17,7 @@
*/
class UpcastingTest extends WebTestBase {
public static $modules = array('paramconverter_test', 'node');
public static $modules = array('paramconverter_test', 'node', 'language');
/**
* Confirms that all parameters are converted as expected.
......@@ -59,4 +60,26 @@ public function testSameTypes() {
$this->drupalGet("paramconverter_test/node/" . $node->id() . "/set/parent/" . $parent->id());
$this->assertRaw("Setting '" . $parent->getTitle() . "' as parent of '" . $node->getTitle() . "'.");
}
/**
* Confirms entity is shown in user's language by default.
*/
public function testEntityLanguage() {
$language = ConfigurableLanguage::createFromLangcode('de');
$language->save();
language_negotiation_url_prefixes_save(array('de' => 'de'));
// The container must be recreated after adding a new language.
$this->rebuildContainer();
$node = $this->drupalCreateNode(array('title' => 'English label'));
$translation = $node->addTranslation('de');
$translation->setTitle('Deutscher Titel')->save();
$this->drupalGet("/paramconverter_test/node/" . $node->id() . "/test_language");
$this->assertRaw("English label");
$this->drupalGet("paramconverter_test/node/" . $node->id() . "/test_language", array('language' => $language));
$this->assertRaw("Deutscher Titel");
}
}
......@@ -37,3 +37,10 @@ paramconverter_test.node_set_parent:
parameters:
parent:
type: 'entity:node'
paramconverter_test.node_check_language:
path: '/paramconverter_test/node/{node}/test_language'
requirements:
_access: 'TRUE'
defaults:
_content: '\Drupal\paramconverter_test\TestControllers::testEntityLanguage'
......@@ -25,4 +25,8 @@ public function testUserNodeFoo(EntityInterface $user, NodeInterface $node, Requ
public function testNodeSetParent(NodeInterface $node, NodeInterface $parent) {
return "Setting '{$parent->label()}' as parent of '{$node->label()}'.";
}
public function testEntityLanguage(NodeInterface $node) {
return $node->label();
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment