class BooleanItemNormalizer

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
  2. 10 core/modules/serialization/tests/modules/test_fieldtype_boolean_emoji_normalizer/src/Normalizer/BooleanItemNormalizer.php \Drupal\test_fieldtype_boolean_emoji_normalizer\Normalizer\BooleanItemNormalizer
  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

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

Hierarchy

Expanded class hierarchy of BooleanItemNormalizer

1 string reference to 'BooleanItemNormalizer'
test_fieldtype_boolean_emoji_normalizer.services.yml in core/modules/serialization/tests/modules/test_fieldtype_boolean_emoji_normalizer/test_fieldtype_boolean_emoji_normalizer.services.yml
core/modules/serialization/tests/modules/test_fieldtype_boolean_emoji_normalizer/test_fieldtype_boolean_emoji_normalizer.services.yml
1 service uses BooleanItemNormalizer
serializer.normalizer.boolean.fieldtype.emoji in core/modules/serialization/tests/modules/test_fieldtype_boolean_emoji_normalizer/test_fieldtype_boolean_emoji_normalizer.services.yml
Drupal\test_fieldtype_boolean_emoji_normalizer\Normalizer\BooleanItemNormalizer

File

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

Namespace

Drupal\test_fieldtype_boolean_emoji_normalizer\Normalizer
View source
class BooleanItemNormalizer extends FieldItemNormalizer implements DenormalizerInterface {
    
    /**
     * {@inheritdoc}
     */
    protected $supportedInterfaceOrClass = BooleanItem::class;
    
    /**
     * {@inheritdoc}
     */
    public function normalize($object, $format = NULL, array $context = []) {
        $data = parent::normalize($object, $format, $context);
        $data['value'] = $data['value'] ? '👍' : '👎';
        return $data;
    }
    
    /**
     * {@inheritdoc}
     */
    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;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
BooleanItemNormalizer::$supportedInterfaceOrClass protected property The interface or class that this Normalizer supports. Overrides FieldItemNormalizer::$supportedInterfaceOrClass
BooleanItemNormalizer::constructValue protected function Build the field item value using the incoming data. Overrides FieldableEntityNormalizerTrait::constructValue
BooleanItemNormalizer::normalize public function Overrides ComplexDataNormalizer::normalize
CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY constant Name of key for bubbling cacheability metadata via serialization context.
ComplexDataNormalizer::hasCacheableSupportsMethod public function Overrides NormalizerBase::hasCacheableSupportsMethod 1
FieldableEntityNormalizerTrait::$entityFieldManager protected property The entity field manager.
FieldableEntityNormalizerTrait::$entityTypeManager protected property The entity type manager. 1
FieldableEntityNormalizerTrait::$entityTypeRepository protected property The entity type repository.
FieldableEntityNormalizerTrait::denormalizeFieldData protected function Denormalizes entity data by denormalizing each field individually.
FieldableEntityNormalizerTrait::determineEntityTypeId protected function Determines the entity type ID to denormalize as.
FieldableEntityNormalizerTrait::extractBundleData protected function Denormalizes the bundle property so entity creation can use it.
FieldableEntityNormalizerTrait::getEntityFieldManager protected function Returns the entity field manager.
FieldableEntityNormalizerTrait::getEntityTypeDefinition protected function Gets the entity type definition.
FieldableEntityNormalizerTrait::getEntityTypeManager protected function Returns the entity type manager.
FieldableEntityNormalizerTrait::getEntityTypeRepository protected function Returns the entity type repository.
FieldItemNormalizer::denormalize public function
NormalizerBase::$format protected property List of formats which supports (de-)normalization. 3
NormalizerBase::addCacheableDependency protected function Adds cacheability if applicable.
NormalizerBase::checkFormat protected function Checks if the provided format is supported by this normalizer. 1
NormalizerBase::supportsDenormalization public function Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization() 1
NormalizerBase::supportsNormalization public function 1
SerializedColumnNormalizerTrait::checkForSerializedStrings protected function Checks if there is a serialized string for a column.
SerializedColumnNormalizerTrait::dataHasStringForSerializeColumn protected function Checks if the data contains string value for serialize column.
SerializedColumnNormalizerTrait::getCustomSerializedPropertyNames protected function Gets the names of all properties the plugin treats as serialized data.
SerializedColumnNormalizerTrait::getSerializedPropertyNames protected function Gets the names of all serialized properties.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.