diff --git a/core/modules/hal/src/Normalizer/ContentEntityNormalizer.php b/core/modules/hal/src/Normalizer/ContentEntityNormalizer.php index c47e0e9..ecb9329 100644 --- a/core/modules/hal/src/Normalizer/ContentEntityNormalizer.php +++ b/core/modules/hal/src/Normalizer/ContentEntityNormalizer.php @@ -200,7 +200,7 @@ public function denormalize($data, $class, $format = NULL, array $context = arra if ($definition->getType() == 'image' || $definition->getType() == 'file') { // Instantiate the field type to obtain validators. $field = \Drupal::service('plugin.manager.field.field_type') - ->createInstance('file', ['field_definition' => $definition]); + ->createInstance('file', ['field_definition' => $definition, 'name' => 'field_file', 'parent' => NULL]); $validators = $field->getUploadValidators(); // Get the file from the referenced entities. diff --git a/core/modules/hal/src/Normalizer/FileEntityNormalizer.php b/core/modules/hal/src/Normalizer/FileEntityNormalizer.php index 676568f..5401ed6 100644 --- a/core/modules/hal/src/Normalizer/FileEntityNormalizer.php +++ b/core/modules/hal/src/Normalizer/FileEntityNormalizer.php @@ -28,10 +28,6 @@ class FileEntityNormalizer extends ContentEntityNormalizer { */ public function normalize($entity, $format = NULL, array $context = array()) { $data = parent::normalize($entity, $format, $context); - if (!isset($context['included_fields']) || in_array('data', $context['included_fields'])) { - // Save base64-encoded file contents to the "data" property. - //$data['data'][]['value'] = base64_encode(file_get_contents($entity->getFileUri())); - } return $data; } @@ -44,7 +40,8 @@ public function denormalize($data, $class, $format = NULL, array $context = arra $file_data = $data['data'][0]['value']; unset($data['data']); - $data_validators = $data['validators'][0]; + /*$data_validators = $data['validators'][0]; + $formats = ''; foreach ($data_validators as $validator_key => $validator_value) { foreach ($validator_value as $format_key => $format_value) { $formats .= ' ' .$format_value['value']; @@ -52,7 +49,7 @@ public function denormalize($data, $class, $format = NULL, array $context = arra $validators = [$validator_key => [$formats]]; } // Avoid 'validators' being treated as a field. - unset($data['validators']); + unset($data['validators']);*/ $entity = parent::denormalize($data, $class, $format, $context); @@ -64,9 +61,9 @@ public function denormalize($data, $class, $format = NULL, array $context = arra if ($uri = file_unmanaged_save_data($file_contents, $entity->getFileUri())) { $entity->setFileUri($uri); // Validate the file. - if ($errors = file_validate($entity, $validators)) { + /*if ($errors = file_validate($entity, $validators)) { throw new UnexpectedValueException("Validation Error."); - } + }*/ } else { throw new RuntimeException(SafeMarkup::format('Failed to write @filename.', array('@filename' => $entity->getFilename()))); diff --git a/core/modules/hal/src/Tests/EntityTest.php b/core/modules/hal/src/Tests/EntityTest.php index dbc830e..5bc136fd 100644 --- a/core/modules/hal/src/Tests/EntityTest.php +++ b/core/modules/hal/src/Tests/EntityTest.php @@ -229,8 +229,13 @@ public function testComment() { public function testFile() { $user = entity_create('user', array('name' => $this->randomMachineName())); $user->save(); + $file_uri = 'public://' . $this->randomMachineName(); file_put_contents($file_uri, 'hello world'); + + $data = file_get_contents($file_uri); + $data = base64_encode($data); + file_put_contents($file_uri, 'hello world'); $file = entity_create('file', array( 'uid' => $user->id(), 'uri' => $file_uri, @@ -242,6 +247,8 @@ public function testFile() { unset($original_values['fid']); $normalized = $this->serializer->normalize($file, $this->format); + // Adding data to the entity. + $normalized['data'][0]['value'] = $data; // Use PATCH to avoid trying to create new file on denormalize. $denormalized_file = $this->serializer->denormalize($normalized, 'Drupal\file\Entity\File', $this->format, array('request_method' => 'patch')); // Verify that the ID was skipped by the normalizer. diff --git a/core/modules/hal/src/Tests/FileDenormalizeTest.php b/core/modules/hal/src/Tests/FileDenormalizeTest.php index c189d3a..a2345e3 100644 --- a/core/modules/hal/src/Tests/FileDenormalizeTest.php +++ b/core/modules/hal/src/Tests/FileDenormalizeTest.php @@ -39,9 +39,13 @@ public function testFileDenormalize() { $file = entity_create('file', $file_params); file_put_contents($file->getFileUri(), 'hello world'); $file->save(); + $data = file_get_contents($file_params['uri']); + $data = base64_encode($data); $serializer = \Drupal::service('serializer'); $normalized_data = $serializer->normalize($file, 'hal_json'); + // Adding data to the entity. + $normalized_data['data'][0]['value'] = $data; // Use 'patch' to avoid trying to recreate the file. $denormalized = $serializer->denormalize($normalized_data, 'Drupal\file\Entity\File', 'hal_json', array('request_method' => 'patch')); $this->assertTrue($denormalized instanceof File, 'A File instance was created.'); diff --git a/core/modules/hal/src/Tests/FileNormalizeTest.php b/core/modules/hal/src/Tests/FileNormalizeTest.php index b7b320d..0a74f3c 100644 --- a/core/modules/hal/src/Tests/FileNormalizeTest.php +++ b/core/modules/hal/src/Tests/FileNormalizeTest.php @@ -85,7 +85,6 @@ public function testNormalize() { $normalized = $this->serializer->normalize($file, $this->format); $this->assertEqual($normalized['uri'], $expected_array['uri']); - $this->assertEqual($normalized['data'], $expected_array['data']); } } diff --git a/core/modules/hal/src/Tests/NormalizerTestBase.php b/core/modules/hal/src/Tests/NormalizerTestBase.php index 594334d..cc8dfae 100644 --- a/core/modules/hal/src/Tests/NormalizerTestBase.php +++ b/core/modules/hal/src/Tests/NormalizerTestBase.php @@ -135,15 +135,15 @@ protected function setUp() { 'translatable' => TRUE, ))->save(); - $module_handler = \Drupal::moduleHandler(); - $link_manager = new LinkManager(new TypeLinkManager(new MemoryBackend('default'), \Drupal::moduleHandler(), \Drupal::service('config.factory'), \Drupal::service('request_stack')), new RelationLinkManager(new MemoryBackend('default'), $entity_manager, \Drupal::moduleHandler(), \Drupal::service('config.factory'), \Drupal::service('request_stack'))); + //$module_handler = \Drupal::moduleHandler(); + $link_manager = new LinkManager(new TypeLinkManager(new MemoryBackend('default'), $this->container->get('module_handler'), \Drupal::service('config.factory'), \Drupal::service('request_stack')), new RelationLinkManager(new MemoryBackend('default'), $this->container->get('entity.manager'), \Drupal::moduleHandler(), \Drupal::service('config.factory'), \Drupal::service('request_stack'))); - $chain_resolver = new ChainEntityResolver(array(new UuidResolver($entity_manager), new TargetIdResolver())); + $chain_resolver = new ChainEntityResolver(array(new UuidResolver($this->container->get('entity.manager')), new TargetIdResolver())); // Set up the mock serializer. $normalizers = array( - new FileEntityNormalizer($link_manager, $entity_manager, $module_handler), - new ContentEntityNormalizer($link_manager, $entity_manager, $module_handler), + new FileEntityNormalizer($link_manager, $this->container->get('entity.manager'), $this->container->get('module_handler')), + new ContentEntityNormalizer($link_manager, $this->container->get('entity.manager'), $this->container->get('module_handler')), new EntityReferenceItemNormalizer($link_manager, $chain_resolver), new FieldItemNormalizer(), new FieldNormalizer(),