FieldNormalizer.php in Drupal 10
File
core/modules/serialization/src/Normalizer/FieldNormalizer.php
View source
<?php
namespace Drupal\serialization\Normalizer;
use Drupal\Core\Field\FieldItemListInterface;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
class FieldNormalizer extends ListNormalizer implements DenormalizerInterface {
protected $supportedInterfaceOrClass = FieldItemListInterface::class;
public function denormalize($data, $class, $format = NULL, array $context = []) : mixed {
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.');
}
$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) {
$context['target_instance'] = $items
->appendItem();
$this->serializer
->denormalize($item_data, $item_class, $format, $context);
}
return $items;
}
}