You are here

public function EntityDenormalizerBase::denormalize in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/jsonapi/src/Normalizer/EntityDenormalizerBase.php \Drupal\jsonapi\Normalizer\EntityDenormalizerBase::denormalize()

File

core/modules/jsonapi/src/Normalizer/EntityDenormalizerBase.php, line 84

Class

EntityDenormalizerBase
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, $format, $context));
}