You are here

protected function FieldableEntityNormalizerTrait::denormalizeFieldData in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/serialization/src/Normalizer/FieldableEntityNormalizerTrait.php \Drupal\serialization\Normalizer\FieldableEntityNormalizerTrait::denormalizeFieldData()
  2. 10 core/modules/serialization/src/Normalizer/FieldableEntityNormalizerTrait.php \Drupal\serialization\Normalizer\FieldableEntityNormalizerTrait::denormalizeFieldData()

Denormalizes entity data by denormalizing each field individually.

Parameters

array $data: The data to denormalize.

\Drupal\Core\Entity\FieldableEntityInterface $entity: The fieldable entity to set field values for.

string $format: The serialization format.

array $context: The context data.

2 calls to FieldableEntityNormalizerTrait::denormalizeFieldData()
ContentEntityNormalizer::denormalize in core/modules/hal/src/Normalizer/ContentEntityNormalizer.php
Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::denormalize().
EntityNormalizer::denormalize in core/modules/serialization/src/Normalizer/EntityNormalizer.php

File

core/modules/serialization/src/Normalizer/FieldableEntityNormalizerTrait.php, line 137

Class

FieldableEntityNormalizerTrait
A trait for providing fieldable entity normalization/denormalization methods.

Namespace

Drupal\serialization\Normalizer

Code

protected function denormalizeFieldData(array $data, FieldableEntityInterface $entity, $format, array $context) {
  foreach ($data as $field_name => $field_data) {
    $field_item_list = $entity
      ->get($field_name);

    // Remove any values that were set as a part of entity creation (e.g
    // uuid). If the incoming field data is set to an empty array, this will
    // also have the effect of emptying the field in REST module.
    $field_item_list
      ->setValue([]);
    $field_item_list_class = get_class($field_item_list);
    if ($field_data) {

      // The field instance must be passed in the context so that the field
      // denormalizer can update field values for the parent entity.
      $context['target_instance'] = $field_item_list;
      $this->serializer
        ->denormalize($field_data, $field_item_list_class, $format, $context);
    }
  }
}