diff --git a/src/Normalizer/JsonApiDocumentTopLevelNormalizer.php b/src/Normalizer/JsonApiDocumentTopLevelNormalizer.php index 0b41751..189a64b 100644 --- a/src/Normalizer/JsonApiDocumentTopLevelNormalizer.php +++ b/src/Normalizer/JsonApiDocumentTopLevelNormalizer.php @@ -102,7 +102,7 @@ public function denormalize($data, $class, $format = NULL, array $context = []) // Work out the correct entity type ID from the relationship 'type' // value. - if (strpos($relationship['data']['type'], '--')) { + if (strpos($relationship['data'][0]['type'], '--') !== FALSE) { list($entity_type_id,) = explode('--', $relationship['data'][0]['type']); } @@ -110,19 +110,25 @@ public function denormalize($data, $class, $format = NULL, array $context = []) // is being used. else { $resource_type = $this->currentContext->getResourceType(); + $bundle_id = $resource_type->getBundle(); $type_id = $resource_type->getEntityTypeId(); // Compare the original with what is contained by the request data // and replace the type with the original if necessary. - if ($entity_type_id !== $type_id) { + if ($relationship['data'][0]['type'] == $bundle_id) { $entity_type_id = $type_id; } + else { + $entity_type_id = $relationship['data'][0]['type']; + } } + try { $entity_storage = $this->entityTypeManager->getStorage($entity_type_id); } catch (PluginNotFoundException $e) { throw new BadRequestHttpException("Invalid type specified for related resource: '" . $relationship['data'][0]['type'] . "'"); } + // In order to maintain the order ($delta) of the relationships, we need // to load the entities and explore the uuid value. $related_entities = array_values($entity_storage->loadByProperties(['uuid' => $id_list]));