diff --git a/core/modules/hal/lib/Drupal/hal/Encoder/JsonEncoder.php b/core/modules/hal/lib/Drupal/hal/Encoder/JsonEncoder.php index 639ae32..4d932ff 100644 --- a/core/modules/hal/lib/Drupal/hal/Encoder/JsonEncoder.php +++ b/core/modules/hal/lib/Drupal/hal/Encoder/JsonEncoder.php @@ -2,7 +2,7 @@ /** * @file - * Definition of Drupal\hal\JsonEncoder. + * Contains \Drupal\hal\JsonEncoder. */ namespace Drupal\hal\Encoder; @@ -30,4 +30,11 @@ public function supportsEncoding($format) { return $format == $this->format; } + /** + * Overrides \Symfony\Component\Serializer\Encoder\JsonEncoder::supportsDecoding() + */ + public function supportsDecoding($format) { + return $format == $this->format; + } + } diff --git a/core/modules/hal/lib/Drupal/hal/Normalizer/EntityNormalizer.php b/core/modules/hal/lib/Drupal/hal/Normalizer/EntityNormalizer.php index 2ac921e..0fa3600 100644 --- a/core/modules/hal/lib/Drupal/hal/Normalizer/EntityNormalizer.php +++ b/core/modules/hal/lib/Drupal/hal/Normalizer/EntityNormalizer.php @@ -57,7 +57,9 @@ public function normalize($entity, $format = NULL, array $context = array()) { $properties = $entity->getProperties(); } foreach ($properties as $property) { - if ($property->getName() == 'id') { + // In some cases, Entity API will return NULL array items. Ensure this is + // a real property and that it is not the internal id. + if (!is_object($property) || $property->getName() == 'id') { continue; } $normalized_property = $this->serializer->normalize($property, $format, $context); @@ -122,20 +124,17 @@ public function denormalize($data, $class, $format = NULL, array $context = arra // Iterate through remaining items in data array. These should all // correspond to fields. foreach ($data as $field_name => $field_data) { - // If the incoming value is an empty array, set the property to mark it - // for deletion. - if (empty($field_data) && is_array($field_data)) { - $entity->{$field_name} = array(); - } + // Remove any values that were set as a part of entity creation (e.g + // uuid). If this field is set to an empty array in the data, this will + // also have the effect of marking the field for deletion in REST module. + $entity->{$field_name} = array(); // Get the class of the field. This will generally be the default Field // class. $field = $entity->get($field_name); $class = get_class($field); $context['target_instance'] = $field; - $field_value = $this->serializer->denormalize($field_data, $class, $format, $context); - //$entity->getTranslation($langcode) - //->set($field_name, $field_value); + $field = $this->serializer->denormalize($field_data, $class, $format, $context); } return $entity; diff --git a/core/modules/hal/lib/Drupal/hal/Tests/DenormalizeTest.php b/core/modules/hal/lib/Drupal/hal/Tests/DenormalizeTest.php index a046720..9ff32e7 100644 --- a/core/modules/hal/lib/Drupal/hal/Tests/DenormalizeTest.php +++ b/core/modules/hal/lib/Drupal/hal/Tests/DenormalizeTest.php @@ -105,6 +105,11 @@ public function testBasicFieldDenormalization() { 'href' => url('rest/types/entity_test/entity_test', array('absolute' => TRUE)), ), ), + 'uuid' => array( + array( + 'value' => 'e5c9fb96-3acf-4a8d-9417-23de1b6c3311', + ), + ), 'field_test_text' => array( array( 'value' => $this->randomName(), @@ -156,6 +161,7 @@ public function testBasicFieldDenormalization() { ), ); $denormalized = $this->container->get('serializer')->denormalize($data, $this->entityClass, $this->format); + $this->assertEqual($data['uuid'], $denormalized->get('uuid')->getValue(), 'A preset value (e.g. UUID) is overridden by incoming data.'); $this->assertEqual($data['field_test_text'], $denormalized->get('field_test_text')->getValue(), 'A basic text field is denormalized.'); $this->assertEqual($expected_value_en, $denormalized->get('field_test_translatable_text')->getValue(), 'Values in the default language are properly handled for a translatable field.'); $this->assertEqual($expected_value_de, $denormalized->getTranslation('de')->get('field_test_translatable_text')->getValue(), 'Values in a translation language are properly handled for a translatable field.');