3 namespace Drupal\serialization\Normalizer;
5 use Drupal\Core\Field\FieldItemInterface;
6 use Symfony\Component\Serializer\Exception\InvalidArgumentException;
7 use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
10 * Denormalizes field item object structure by updating the entity field values.
12 class FieldItemNormalizer extends ComplexDataNormalizer implements DenormalizerInterface {
17 protected $supportedInterfaceOrClass = FieldItemInterface::class;
22 public function denormalize($data, $class, $format = NULL, array $context = []) {
23 if (!isset($context['target_instance'])) {
24 throw new InvalidArgumentException('$context[\'target_instance\'] must be set to denormalize with the FieldItemNormalizer');
27 if ($context['target_instance']->getParent() == NULL) {
28 throw new InvalidArgumentException('The field item passed in via $context[\'target_instance\'] must have a parent set.');
31 /** @var \Drupal\Core\Field\FieldItemInterface $field_item */
32 $field_item = $context['target_instance'];
34 $field_item->setValue($this->constructValue($data, $context));
39 * Build the field item value using the incoming data.
41 * Most normalizers that extend this class can simply use this method to
42 * construct the denormalized value without having to override denormalize()
43 * and reimplementing its validation logic or its call to set the field value.
46 * The incoming data for this field item.
47 * @param array $context
48 * The context passed into the Normalizer.
51 * The value to use in Entity::setValue().
53 protected function constructValue($data, $context) {