function FieldableEntityNormalizerTrait::constructValue
Same name in other branches
- 8.9.x core/modules/serialization/src/Normalizer/FieldableEntityNormalizerTrait.php \Drupal\serialization\Normalizer\FieldableEntityNormalizerTrait::constructValue()
- 10 core/modules/serialization/src/Normalizer/FieldableEntityNormalizerTrait.php \Drupal\serialization\Normalizer\FieldableEntityNormalizerTrait::constructValue()
- 11.x 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().
5 calls to FieldableEntityNormalizerTrait::constructValue()
- EntityReferenceFieldItemNormalizer::constructValue in core/
modules/ serialization/ src/ Normalizer/ EntityReferenceFieldItemNormalizer.php - Build the field item value using the incoming data.
- FieldItemNormalizer::denormalize in core/
modules/ serialization/ src/ Normalizer/ FieldItemNormalizer.php - FieldItemNormalizer::denormalize in core/
modules/ hal/ src/ Normalizer/ FieldItemNormalizer.php - StringNormalizer::constructValue in core/
modules/ jsonapi/ tests/ modules/ jsonapi_test_field_type/ src/ Normalizer/ StringNormalizer.php - Build the field item value using the incoming data.
- TextItemSillyNormalizer::constructValue in core/
modules/ serialization/ tests/ modules/ field_normalization_test/ src/ Normalization/ TextItemSillyNormalizer.php - Build the field item value using the incoming data.
7 methods override FieldableEntityNormalizerTrait::constructValue()
- BooleanItemNormalizer::constructValue in core/
modules/ serialization/ tests/ modules/ test_fieldtype_boolean_emoji_normalizer/ src/ Normalizer/ BooleanItemNormalizer.php - Build the field item value using the incoming data.
- EntityReferenceFieldItemNormalizer::constructValue in core/
modules/ serialization/ src/ Normalizer/ EntityReferenceFieldItemNormalizer.php - Build the field item value using the incoming data.
- EntityReferenceItemNormalizer::constructValue in core/
modules/ hal/ src/ Normalizer/ EntityReferenceItemNormalizer.php - Build the field item value using the incoming data.
- StringNormalizer::constructValue in core/
modules/ jsonapi/ tests/ modules/ jsonapi_test_field_type/ src/ Normalizer/ StringNormalizer.php - Build the field item value using the incoming data.
- TextItemSillyNormalizer::constructValue in core/
modules/ serialization/ tests/ modules/ field_normalization_test/ src/ Normalization/ TextItemSillyNormalizer.php - Build the field item value using the incoming data.
File
-
core/
modules/ serialization/ src/ Normalizer/ FieldableEntityNormalizerTrait.php, line 206
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;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.