protected function FieldableEntityNormalizerTrait::constructValue in Drupal 10
Same name and namespace in other branches
- 8 core/modules/serialization/src/Normalizer/FieldableEntityNormalizerTrait.php \Drupal\serialization\Normalizer\FieldableEntityNormalizerTrait::constructValue()
- 9 core/modules/serialization/src/Normalizer/FieldableEntityNormalizerTrait.php \Drupal\serialization\Normalizer\FieldableEntityNormalizerTrait::constructValue()
Build the field item value using the incoming data.
Most normalizers that extend this class can simply use this method to construct the denormalized value without having to override denormalize() and re-implementing its validation logic or its call to set the field value.
It's recommended to not override this and instead provide a (de)normalizer at the DataType level.
Parameters
mixed $data: The incoming data for this field item.
array $context: The context passed into the Normalizer.
Return value
mixed The value to use in Entity::setValue().
1 call to FieldableEntityNormalizerTrait::constructValue()
- FieldItemNormalizer::denormalize in core/
modules/ serialization/ src/ Normalizer/ FieldItemNormalizer.php
File
- core/
modules/ serialization/ src/ Normalizer/ FieldableEntityNormalizerTrait.php, line 205
Class
- FieldableEntityNormalizerTrait
- A trait for providing fieldable entity normalization/denormalization methods.
Namespace
Drupal\serialization\NormalizerCode
protected function constructValue($data, $context) {
$field_item = $context['target_instance'];
// Get the property definitions.
assert($field_item instanceof FieldItemInterface);
$field_definition = $field_item
->getFieldDefinition();
$item_definition = $field_definition
->getItemDefinition();
assert($item_definition instanceof FieldItemDataDefinitionInterface);
$property_definitions = $item_definition
->getPropertyDefinitions();
$serialized_property_names = $this
->getCustomSerializedPropertyNames($field_item);
$denormalize_property = function ($property_name, $property_value, $property_value_class, $context) use ($serialized_property_names) {
if ($this->serializer
->supportsDenormalization($property_value, $property_value_class, NULL, $context)) {
return $this->serializer
->denormalize($property_value, $property_value_class, NULL, $context);
}
else {
if (in_array($property_name, $serialized_property_names, TRUE)) {
$property_value = serialize($property_value);
}
return $property_value;
}
};
if (!is_array($data)) {
$property_value = $data;
$property_name = $item_definition
->getMainPropertyName();
$property_value_class = $property_definitions[$property_name]
->getClass();
return $denormalize_property($property_name, $property_value, $property_value_class, $context);
}
$data_internal = [];
if (!empty($property_definitions)) {
foreach ($property_definitions as $property_name => $property_definition) {
// Not every property is required to be sent.
if (!array_key_exists($property_name, $data)) {
continue;
}
$property_value = $data[$property_name];
$property_value_class = $property_definition
->getClass();
$data_internal[$property_name] = $denormalize_property($property_name, $property_value, $property_value_class, $context);
}
}
else {
$data_internal = $data;
}
return $data_internal;
}