function ComplexDataNormalizer::normalize

Same name in other branches
  1. 9 core/modules/serialization/src/Normalizer/ComplexDataNormalizer.php \Drupal\serialization\Normalizer\ComplexDataNormalizer::normalize()
  2. 8.9.x core/modules/serialization/src/Normalizer/ComplexDataNormalizer.php \Drupal\serialization\Normalizer\ComplexDataNormalizer::normalize()
  3. 11.x core/modules/serialization/src/Normalizer/ComplexDataNormalizer.php \Drupal\serialization\Normalizer\ComplexDataNormalizer::normalize()
7 methods override ComplexDataNormalizer::normalize()
BooleanItemNormalizer::normalize in core/modules/serialization/tests/modules/test_fieldtype_boolean_emoji_normalizer/src/Normalizer/BooleanItemNormalizer.php
ConfigEntityNormalizer::normalize in core/modules/serialization/src/Normalizer/ConfigEntityNormalizer.php
ContentEntityNormalizer::normalize in core/modules/serialization/src/Normalizer/ContentEntityNormalizer.php
EntityReferenceFieldItemNormalizer::normalize in core/modules/serialization/src/Normalizer/EntityReferenceFieldItemNormalizer.php
StringNormalizer::normalize in core/modules/jsonapi/tests/modules/jsonapi_test_field_type/src/Normalizer/StringNormalizer.php

... See full list

File

core/modules/serialization/src/Normalizer/ComplexDataNormalizer.php, line 23

Class

ComplexDataNormalizer
Converts the Drupal entity object structures to a normalized array.

Namespace

Drupal\serialization\Normalizer

Code

public function normalize($object, $format = NULL, array $context = []) : array|string|int|float|bool|\ArrayObject|null {
    $attributes = [];
    // $object will not always match getSupportedTypes().
    // @see \Drupal\serialization\Normalizer\EntityNormalizer
    // Other normalizers that extend this class may only provide $object that
    // implements \Traversable.
    if ($object instanceof ComplexDataInterface) {
        // If there are no properties to normalize, just normalize the value.
        $object = !empty($object->getProperties(TRUE)) ? TypedDataInternalPropertiesHelper::getNonInternalProperties($object) : $object->getValue();
    }
    
    /** @var \Drupal\Core\TypedData\TypedDataInterface $property */
    foreach ($object as $name => $property) {
        $attributes[$name] = $this->serializer
            ->normalize($property, $format, $context);
    }
    return $attributes;
}

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