function FieldNormalizer::denormalize

Same name in this branch
  1. 8.9.x core/modules/jsonapi/src/Normalizer/FieldNormalizer.php \Drupal\jsonapi\Normalizer\FieldNormalizer::denormalize()
Same name in other branches
  1. 9 core/modules/jsonapi/src/Normalizer/FieldNormalizer.php \Drupal\jsonapi\Normalizer\FieldNormalizer::denormalize()
  2. 9 core/modules/serialization/src/Normalizer/FieldNormalizer.php \Drupal\serialization\Normalizer\FieldNormalizer::denormalize()
  3. 10 core/modules/jsonapi/src/Normalizer/FieldNormalizer.php \Drupal\jsonapi\Normalizer\FieldNormalizer::denormalize()
  4. 10 core/modules/serialization/src/Normalizer/FieldNormalizer.php \Drupal\serialization\Normalizer\FieldNormalizer::denormalize()
  5. 11.x core/modules/jsonapi/src/Normalizer/FieldNormalizer.php \Drupal\jsonapi\Normalizer\FieldNormalizer::denormalize()
  6. 11.x core/modules/serialization/src/Normalizer/FieldNormalizer.php \Drupal\serialization\Normalizer\FieldNormalizer::denormalize()

File

core/modules/serialization/src/Normalizer/FieldNormalizer.php, line 29

Class

FieldNormalizer
Denormalizes data to Drupal field values.

Namespace

Drupal\serialization\Normalizer

Code

public function denormalize($data, $class, $format = NULL, array $context = []) {
    if (!isset($context['target_instance'])) {
        throw new InvalidArgumentException('$context[\'target_instance\'] must be set to denormalize with the FieldNormalizer');
    }
    if ($context['target_instance']->getParent() == NULL) {
        throw new InvalidArgumentException('The field passed in via $context[\'target_instance\'] must have a parent set.');
    }
    
    /** @var \Drupal\Core\Field\FieldItemListInterface $items */
    $items = $context['target_instance'];
    $item_class = $items->getItemDefinition()
        ->getClass();
    if (!is_array($data)) {
        throw new UnexpectedValueException(sprintf('Field values for "%s" must use an array structure', $items->getName()));
    }
    foreach ($data as $item_data) {
        // Create a new item and pass it as the target for the unserialization of
        // $item_data. All items in field should have removed before this method
        // was called.
        // @see \Drupal\serialization\Normalizer\ContentEntityNormalizer::denormalize().
        $context['target_instance'] = $items->appendItem();
        $this->serializer
            ->denormalize($item_data, $item_class, $format, $context);
    }
    return $items;
}

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