You are here

public function EntityNormalizer::denormalize in JSON:API 8

File

src/Normalizer/EntityNormalizer.php, line 133

Class

EntityNormalizer
Converts the Drupal entity object to a JSON API array structure.

Namespace

Drupal\jsonapi\Normalizer

Code

public function denormalize($data, $class, $format = NULL, array $context = []) {
  if (empty($context['resource_type']) || !$context['resource_type'] instanceof ResourceType) {
    throw new PreconditionFailedHttpException('Missing context during denormalization.');
  }

  /* @var \Drupal\jsonapi\ResourceType\ResourceType $resource_type */
  $resource_type = $context['resource_type'];
  $entity_type_id = $resource_type
    ->getEntityTypeId();
  $bundle = $resource_type
    ->getBundle();
  $bundle_key = $this->entityTypeManager
    ->getDefinition($entity_type_id)
    ->getKey('bundle');
  if ($bundle_key && $bundle) {
    $data[$bundle_key] = $bundle;
  }
  return $this->entityTypeManager
    ->getStorage($entity_type_id)
    ->create($this
    ->prepareInput($data, $resource_type));
}