function EntityNormalizer::denormalize

Same name in other branches
  1. 9 core/modules/serialization/src/Normalizer/EntityNormalizer.php \Drupal\serialization\Normalizer\EntityNormalizer::denormalize()
  2. 8.9.x core/modules/serialization/src/Normalizer/EntityNormalizer.php \Drupal\serialization\Normalizer\EntityNormalizer::denormalize()
  3. 11.x core/modules/serialization/src/Normalizer/EntityNormalizer.php \Drupal\serialization\Normalizer\EntityNormalizer::denormalize()
1 call to EntityNormalizer::denormalize()
ConfigEntityNormalizer::denormalize in core/modules/serialization/src/Normalizer/ConfigEntityNormalizer.php
1 method overrides EntityNormalizer::denormalize()
ConfigEntityNormalizer::denormalize in core/modules/serialization/src/Normalizer/ConfigEntityNormalizer.php

File

core/modules/serialization/src/Normalizer/EntityNormalizer.php, line 38

Class

EntityNormalizer
Normalizes/denormalizes Drupal entity objects into an array structure.

Namespace

Drupal\serialization\Normalizer

Code

public function denormalize($data, $class, $format = NULL, array $context = []) : mixed {
    $entity_type_id = $this->determineEntityTypeId($class, $context);
    $entity_type_definition = $this->getEntityTypeDefinition($entity_type_id);
    // The bundle property will be required to denormalize a bundleable
    // fieldable entity.
    if ($entity_type_definition->entityClassImplements(FieldableEntityInterface::class)) {
        // Extract bundle data to pass into entity creation if the entity type uses
        // bundles.
        if ($entity_type_definition->hasKey('bundle')) {
            // Get an array containing the bundle only. This also remove the bundle
            // key from the $data array.
            $create_params = $this->extractBundleData($data, $entity_type_definition);
        }
        else {
            $create_params = [];
        }
        // Create the entity from bundle data only, then apply field values after.
        $entity = $this->entityTypeManager
            ->getStorage($entity_type_id)
            ->create($create_params);
        $this->denormalizeFieldData($data, $entity, $format, $context);
    }
    else {
        // Create the entity from all data.
        $entity = $this->entityTypeManager
            ->getStorage($entity_type_id)
            ->create($data);
    }
    // Pass the names of the fields whose values can be merged.
    // @todo https://www.drupal.org/node/2456257 remove this.
    $entity->_restSubmittedFields = array_keys($data);
    return $entity;
}

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