diff --git a/core/modules/hal/src/Normalizer/ContentEntityNormalizer.php b/core/modules/hal/src/Normalizer/ContentEntityNormalizer.php index 313d930..788afca 100644 --- a/core/modules/hal/src/Normalizer/ContentEntityNormalizer.php +++ b/core/modules/hal/src/Normalizer/ContentEntityNormalizer.php @@ -124,8 +124,9 @@ public function denormalize($data, $class, $format = NULL, array $context = arra throw new UnexpectedValueException('The type link relation must be specified.'); } - // Create the entity. + // Identify entity type and bundle. $typed_data_ids = $this->getTypedDataIds($data['_links']['type']); + // Figure out the language to use. if (isset($data['langcode'])) { $langcode = $data['langcode'][0]['value']; @@ -139,26 +140,25 @@ public function denormalize($data, $class, $format = NULL, array $context = arra $langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED; } + // Get definition of entity type and bundle (if any). $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'); $values[$bundle_key] = $typed_data_ids['bundle']; - // Unset the bundle key from data, if it's there. - unset($data[$bundle_key]); } + // Create the entity. $entity = $this->entityManager->getStorage($typed_data_ids['entity_type'])->create($values); // Special handling for PATCH: destroy all possible default values that // might have been set on entity creation. We want an "empty" entity that - // will only get filled with fields from the data array. But keep langcode - // and the bundle key, because they are already passed in and removed from - // data. + // will only get filled with fields from the data array. But keep langcode, + // because it is already passed in and removed from $data. if (isset($context['request_method']) && $context['request_method'] == 'patch') { foreach ($entity as $field_name => $field) { - if (!in_array($field_name, array('langcode', $entity_type->getKey('bundle')))) { + if ($field_name != 'langcode') { $entity->set($field_name, NULL); } } diff --git a/core/modules/hal/src/Normalizer/EntityReferenceItemNormalizer.php b/core/modules/hal/src/Normalizer/EntityReferenceItemNormalizer.php index 72e3f1e..de58a8f 100644 --- a/core/modules/hal/src/Normalizer/EntityReferenceItemNormalizer.php +++ b/core/modules/hal/src/Normalizer/EntityReferenceItemNormalizer.php @@ -113,7 +113,7 @@ protected function constructValue($data, $context) { if ($id = $this->entityResolver->resolve($this, $data, $target_type)) { $constructed = array('target_id' => $id); foreach ($field_item->getProperties() as $property => $value) { - if ($property != 'target_id' && isset($data[$property])) { + if ($property != 'target_id' && array_key_exists($property, $data)) { $constructed[$property] = $data[$property]; } } diff --git a/core/modules/hal/src/Tests/DenormalizeTest.php b/core/modules/hal/src/Tests/DenormalizeTest.php index 761b804..95035af 100644 --- a/core/modules/hal/src/Tests/DenormalizeTest.php +++ b/core/modules/hal/src/Tests/DenormalizeTest.php @@ -178,7 +178,7 @@ public function testBasicFieldDenormalization() { /** * Verifies that only specified properties get populated in the PATCH context. */ - public function testPatchDenormailzation() { + public function testPatchDenormalization() { $data = array( '_links' => array( 'type' => array( diff --git a/core/modules/hal/src/Tests/EntityTest.php b/core/modules/hal/src/Tests/EntityTest.php index b3421cf..2d76217 100644 --- a/core/modules/hal/src/Tests/EntityTest.php +++ b/core/modules/hal/src/Tests/EntityTest.php @@ -174,10 +174,6 @@ public function testComment() { // Loop over the remaining fields and verify that they are identical. foreach ($original_values as $field_name => $field_values) { - // The target field comes with revision id which is not set. - if (array_key_exists('revision_id', $field_values[0])) { - unset($field_values[0]['revision_id']); - } $this->assertEqual($field_values, $denormalized_comment->get($field_name)->getValue()); } } diff --git a/core/modules/hal/src/Tests/FileFieldNormalizeTest.php b/core/modules/hal/src/Tests/FileFieldNormalizeTest.php index 6c53217..e787ab3 100644 --- a/core/modules/hal/src/Tests/FileFieldNormalizeTest.php +++ b/core/modules/hal/src/Tests/FileFieldNormalizeTest.php @@ -112,7 +112,6 @@ public function testImageFieldNormalize() { $serialized = $this->container->get('serializer')->serialize($entity, $this->format); $deserialized = $this->container->get('serializer')->deserialize($serialized, 'Drupal\entity_test\Entity\EntityTest', $this->format); - // @todo Fails because the field in $entity has display and description set (to NULL) $this->assertEqual($entity->toArray()['field_image'], $deserialized->toArray()['field_image'], "Image field is preserved."); } }