function FieldableEntityNormalizerTrait::denormalizeFieldData

Same name in other branches
  1. 9 core/modules/serialization/src/Normalizer/FieldableEntityNormalizerTrait.php \Drupal\serialization\Normalizer\FieldableEntityNormalizerTrait::denormalizeFieldData()
  2. 8.9.x core/modules/serialization/src/Normalizer/FieldableEntityNormalizerTrait.php \Drupal\serialization\Normalizer\FieldableEntityNormalizerTrait::denormalizeFieldData()
  3. 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.

1 call to FieldableEntityNormalizerTrait::denormalizeFieldData()
EntityNormalizer::denormalize in core/modules/serialization/src/Normalizer/EntityNormalizer.php

File

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

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);
        }
    }
}

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