function EntityDenormalizerBase::denormalize

Same name in other branches
  1. 8.9.x core/modules/jsonapi/src/Normalizer/EntityDenormalizerBase.php \Drupal\jsonapi\Normalizer\EntityDenormalizerBase::denormalize()
  2. 10 core/modules/jsonapi/src/Normalizer/EntityDenormalizerBase.php \Drupal\jsonapi\Normalizer\EntityDenormalizerBase::denormalize()
  3. 11.x 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));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.