diff --git a/core/modules/serialization/src/Normalizer/EntityNormalizer.php b/core/modules/serialization/src/Normalizer/EntityNormalizer.php index 972786aeb4..b3a881e02f 100644 --- a/core/modules/serialization/src/Normalizer/EntityNormalizer.php +++ b/core/modules/serialization/src/Normalizer/EntityNormalizer.php @@ -61,13 +61,13 @@ public function denormalize($data, $class, $format = NULL, array $context = []) // 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 denormalizer - // can update field values. The entity itself does not need to be passed - // because nothing except the current field needs to be set. - $context['__target_field_instance'] = $field_item_list; - $this->serializer->denormalize($field_data, get_class($field_item_list), $format, $context); + // 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); } } } @@ -156,7 +156,7 @@ protected function extractBundleData(array &$data, EntityTypeInterface $entity_t // Make sure the submitted bundle is a valid bundle for the entity type. if ($bundle_types && !in_array($bundle_value, $bundle_types)) { - throw new UnexpectedValueException(sprintf('"%s" is not a valid bundle type for denormalization.', $data[$bundle_key])); + throw new UnexpectedValueException(sprintf('"%s" is not a valid bundle type for denormalization.', $bundle_value)); } return [$bundle_key => $bundle_value]; diff --git a/core/modules/serialization/src/Normalizer/FieldItemNormalizer.php b/core/modules/serialization/src/Normalizer/FieldItemNormalizer.php index b486f9e3cb..a3cfcf206f 100644 --- a/core/modules/serialization/src/Normalizer/FieldItemNormalizer.php +++ b/core/modules/serialization/src/Normalizer/FieldItemNormalizer.php @@ -2,6 +2,7 @@ namespace Drupal\serialization\Normalizer; +use Drupal\Core\Field\FieldItemInterface; use Symfony\Component\Serializer\Exception\InvalidArgumentException; use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; @@ -13,18 +14,22 @@ class FieldItemNormalizer extends ComplexDataNormalizer implements DenormalizerI /** * {@inheritdoc} */ - protected $supportedInterfaceOrClass = 'Drupal\Core\Field\FieldItemInterface'; + protected $supportedInterfaceOrClass = FieldItemInterface::class; /** * {@inheritdoc} */ public function denormalize($data, $class, $format = NULL, array $context = array()) { - if (!isset($context['__target_field_item_instance'])) { - throw new InvalidArgumentException('$context[\'__target_field_item_instance\'] must be set to denormalize with the FieldItemNormalizer'); + if (!isset($context['target_instance'])) { + throw new InvalidArgumentException('$context[\'target_instance\'] must be set to denormalize with the FieldItemNormalizer'); + } + + if ($context['target_instance']->getParent() == NULL) { + throw new InvalidArgumentException('The field item passed in via $context[\'target_instance\'] must have a parent set.'); } /** @var \Drupal\Core\Field\FieldItemInterface $field_item */ - $field_item = $context['__target_field_item_instance']; + $field_item = $context['target_instance']; $field_item->setValue($this->constructValue($data, $context)); return $field_item; diff --git a/core/modules/serialization/src/Normalizer/FieldNormalizer.php b/core/modules/serialization/src/Normalizer/FieldNormalizer.php index 78b28a9b49..1847379b23 100644 --- a/core/modules/serialization/src/Normalizer/FieldNormalizer.php +++ b/core/modules/serialization/src/Normalizer/FieldNormalizer.php @@ -26,19 +26,19 @@ class FieldNormalizer extends ListNormalizer implements DenormalizerInterface { * {@inheritdoc} */ public function denormalize($data, $class, $format = NULL, array $context = array()) { - if (!isset($context['__target_field_instance'])) { - throw new InvalidArgumentException('$context[\'__target_field_instance\'] must be set to denormalize with the FieldNormalizer'); + if (!isset($context['target_instance'])) { + throw new InvalidArgumentException('$context[\'target_instance\'] must be set to denormalize with the FieldNormalizer'); } /** @var FieldItemListInterface $items */ - $items = $context['__target_field_instance']; + $items = $context['target_instance']; $item_class = $items->getItemDefinition()->getClass(); 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_field_item_instance'] = $items->appendItem(); + $context['target_instance'] = $items->appendItem(); $this->serializer->denormalize($item_data, $item_class, $format, $context); } return $items;