From feb3a4df40f6397c5f33d9e211ad68047fbc915b Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Sat, 1 Oct 2016 08:04:00 +0100 Subject: [PATCH] Issue #2792205 by fago: Denormalization of entities with translations fails --- .../Normalizer/ContentEntityNormalizer.php | 16 +++- .../Kernel/EntityTranslationNormalizeTest.php | 89 +++++++++++++++++++ 2 files changed, 101 insertions(+), 4 deletions(-) create mode 100644 core/modules/hal/tests/src/Kernel/EntityTranslationNormalizeTest.php diff --git a/core/modules/hal/src/Normalizer/ContentEntityNormalizer.php b/core/modules/hal/src/Normalizer/ContentEntityNormalizer.php index 712d52cbd6..e1efc207de 100644 --- a/core/modules/hal/src/Normalizer/ContentEntityNormalizer.php +++ b/core/modules/hal/src/Normalizer/ContentEntityNormalizer.php @@ -129,14 +129,22 @@ public function denormalize($data, $class, $format = NULL, array $context = arra // Create the entity. $typed_data_ids = $this->getTypedDataIds($data['_links']['type'], $context); $entity_type = $this->entityManager->getDefinition($typed_data_ids['entity_type']); + $default_langcode_key = $entity_type->getKey('default_langcode'); $langcode_key = $entity_type->getKey('langcode'); $values = array(); // Figure out the language to use. - if (isset($data[$langcode_key])) { - $values[$langcode_key] = $data[$langcode_key][0]['value']; - // Remove the langcode so it does not get iterated over below. - unset($data[$langcode_key]); + if (isset($data[$default_langcode_key])) { + // Find the field item for which the default_lancode value is set to 1 and + // set the langcode the right default language. + foreach ($data[$default_langcode_key] as $item) { + if (!empty($item['value']) && isset($item['lang'])) { + $values[$langcode_key] = $item['lang']; + break; + } + } + // Remove the default langcode so it does not get iterated over below. + unset($data[$default_langcode_key]); } if ($entity_type->hasKey('bundle')) { diff --git a/core/modules/hal/tests/src/Kernel/EntityTranslationNormalizeTest.php b/core/modules/hal/tests/src/Kernel/EntityTranslationNormalizeTest.php new file mode 100644 index 0000000000..35e038f840 --- /dev/null +++ b/core/modules/hal/tests/src/Kernel/EntityTranslationNormalizeTest.php @@ -0,0 +1,89 @@ +installSchema('system', array('sequences')); + $this->installConfig(['node', 'content_translation']); + } + + /** + * Tests the normalization of node translations. + */ + public function testNodeTranslation() { + $node_type = NodeType::create(['type' => 'example_type']); + $node_type->save(); + $this->container->get('content_translation.manager')->setEnabled('node', 'example_type', TRUE); + + $user = User::create(['name' => $this->randomMachineName()]); + $user->save(); + + $node = Node::create([ + 'title' => $this->randomMachineName(), + 'uid' => $user->id(), + 'type' => $node_type->id(), + 'status' => NODE_PUBLISHED, + 'langcode' => 'en', + 'promote' => 1, + 'sticky' => 0, + 'body' => [ + 'value' => $this->randomMachineName(), + 'format' => $this->randomMachineName() + ], + 'revision_log' => $this->randomString(), + ]); + $node->addTranslation('de', [ + 'title' => 'German title', + 'body' => [ + 'value' => $this->randomMachineName(), + 'format' => $this->randomMachineName() + ], + ]); + $node->save(); + + $original_values = $node->toArray(); + $translation = $node->getTranslation('de'); + $original_translation_values = $node->getTranslation('en')->toArray(); + + $normalized = $this->serializer->normalize($node, $this->format); + + $this->assertContains(['lang' => 'en', 'value' => $node->getTitle()], $normalized['title'], 'Original language title has been normalized.'); + $this->assertContains(['lang' => 'de', 'value' => $translation->getTitle()], $normalized['title'], 'Translation language title has been normalized.'); + + /** @var \Drupal\node\NodeInterface $denormalized_node */ + $denormalized_node = $this->serializer->denormalize($normalized, 'Drupal\node\Entity\Node', $this->format); + + $this->assertSame($denormalized_node->language()->getId(), $denormalized_node->getUntranslated()->language()->getId(), 'Untranslated object is returned from serializer.'); + $this->assertSame('en', $denormalized_node->language()->getId()); + $this->assertTrue($denormalized_node->hasTranslation('de')); + + $this->assertSame($node->getTitle(), $denormalized_node->getTitle()); + $this->assertSame($translation->getTitle(), $denormalized_node->getTranslation('de')->getTitle()); + + $this->assertEquals($original_values, $denormalized_node->toArray(), 'Node values are restored after normalizing and denormalizing.'); + $this->assertEquals($original_translation_values, $denormalized_node->getTranslation('en')->toArray(), 'Node values are restored after normalizing and denormalizing.'); + } + +} -- GitLab