function BooleanItemNormalizer::constructValue
Same name and namespace in other branches
- 9 core/modules/serialization/tests/modules/test_fieldtype_boolean_emoji_normalizer/src/Normalizer/BooleanItemNormalizer.php \Drupal\test_fieldtype_boolean_emoji_normalizer\Normalizer\BooleanItemNormalizer::constructValue()
- 8.9.x core/modules/serialization/tests/modules/test_fieldtype_boolean_emoji_normalizer/src/Normalizer/BooleanItemNormalizer.php \Drupal\test_fieldtype_boolean_emoji_normalizer\Normalizer\BooleanItemNormalizer::constructValue()
- 10 core/modules/serialization/tests/modules/test_fieldtype_boolean_emoji_normalizer/src/Normalizer/BooleanItemNormalizer.php \Drupal\test_fieldtype_boolean_emoji_normalizer\Normalizer\BooleanItemNormalizer::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 \Drupal\Core\Field\FieldItemBase::setValue() or a subclass.
Overrides FieldableEntityNormalizerTrait::constructValue
File
-
core/
modules/ serialization/ tests/ modules/ test_fieldtype_boolean_emoji_normalizer/ src/ Normalizer/ BooleanItemNormalizer.php, line 28
Class
- BooleanItemNormalizer
- Normalizes boolean fields weirdly: renders them as 👍 (TRUE) or 👎 (FALSE).
Namespace
Drupal\test_fieldtype_boolean_emoji_normalizer\NormalizerCode
protected function constructValue($data, $context) {
// Just like \Drupal\serialization\Normalizer\FieldItemNormalizer's logic
// for denormalization, which uses TypedDataInterface::setValue(), allow the
// keying by main property name ("value") to be implied.
if (!is_array($data)) {
$data = [
'value' => $data,
];
}
if (!in_array($data['value'], [
'👍',
'👎',
], TRUE)) {
throw new \UnexpectedValueException('Only 👍 and 👎 are acceptable values.');
}
$data['value'] = $data['value'] === '👍';
return $data;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.