function BooleanItemNormalizer::constructValue

Same name and namespace in other branches
  1. 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()
  2. 10 core/modules/serialization/tests/modules/test_fieldtype_boolean_emoji_normalizer/src/Normalizer/BooleanItemNormalizer.php \Drupal\test_fieldtype_boolean_emoji_normalizer\Normalizer\BooleanItemNormalizer::constructValue()
  3. 11.x core/modules/serialization/tests/modules/test_fieldtype_boolean_emoji_normalizer/src/Normalizer/BooleanItemNormalizer.php \Drupal\test_fieldtype_boolean_emoji_normalizer\Normalizer\BooleanItemNormalizer::constructValue()

Overrides FieldableEntityNormalizerTrait::constructValue

File

core/modules/serialization/tests/modules/test_fieldtype_boolean_emoji_normalizer/src/Normalizer/BooleanItemNormalizer.php, line 31

Class

BooleanItemNormalizer
Normalizes boolean fields weirdly: renders them as 👍 (TRUE) or 👎 (FALSE).

Namespace

Drupal\test_fieldtype_boolean_emoji_normalizer\Normalizer

Code

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.