function FieldItemNormalizer::denormalize

Same name in this branch
  1. 8.9.x core/modules/serialization/src/Normalizer/FieldItemNormalizer.php \Drupal\serialization\Normalizer\FieldItemNormalizer::denormalize()
  2. 8.9.x core/modules/hal/src/Normalizer/FieldItemNormalizer.php \Drupal\hal\Normalizer\FieldItemNormalizer::denormalize()
Same name and namespace in other branches
  1. 9 core/modules/jsonapi/src/Normalizer/FieldItemNormalizer.php \Drupal\jsonapi\Normalizer\FieldItemNormalizer::denormalize()
  2. 9 core/modules/serialization/src/Normalizer/FieldItemNormalizer.php \Drupal\serialization\Normalizer\FieldItemNormalizer::denormalize()
  3. 9 core/modules/hal/src/Normalizer/FieldItemNormalizer.php \Drupal\hal\Normalizer\FieldItemNormalizer::denormalize()
  4. 10 core/modules/jsonapi/src/Normalizer/FieldItemNormalizer.php \Drupal\jsonapi\Normalizer\FieldItemNormalizer::denormalize()
  5. 10 core/modules/serialization/src/Normalizer/FieldItemNormalizer.php \Drupal\serialization\Normalizer\FieldItemNormalizer::denormalize()
  6. 11.x core/modules/jsonapi/src/Normalizer/FieldItemNormalizer.php \Drupal\jsonapi\Normalizer\FieldItemNormalizer::denormalize()
  7. 11.x core/modules/serialization/src/Normalizer/FieldItemNormalizer.php \Drupal\serialization\Normalizer\FieldItemNormalizer::denormalize()

File

core/modules/jsonapi/src/Normalizer/FieldItemNormalizer.php, line 90

Class

FieldItemNormalizer
Converts the Drupal field item object to a JSON:API array structure.

Namespace

Drupal\jsonapi\Normalizer

Code

public function denormalize($data, $class, $format = NULL, array $context = []) {
    $item_definition = $context['field_definition']->getItemDefinition();
    assert($item_definition instanceof FieldItemDataDefinitionInterface);
    $field_item = $this->getFieldItemInstance($context['resource_type'], $item_definition);
    $this->checkForSerializedStrings($data, $class, $field_item);
    $property_definitions = $item_definition->getPropertyDefinitions();
    $serialized_property_names = $this->getCustomSerializedPropertyNames($field_item);
    $denormalize_property = function ($property_name, $property_value, $property_value_class, $format, $context) use ($serialized_property_names) {
        if ($this->serializer
            ->supportsDenormalization($property_value, $property_value_class, $format, $context)) {
            return $this->serializer
                ->denormalize($property_value, $property_value_class, $format, $context);
        }
        else {
            if (in_array($property_name, $serialized_property_names, TRUE)) {
                $property_value = serialize($property_value);
            }
            return $property_value;
        }
    };
    // Because e.g. the 'bundle' entity key field requires field values to not
    // be expanded to an array of all properties, we special-case single-value
    // properties.
    if (!is_array($data)) {
        // The NULL normalization means there is no value, hence we can return
        // early. Note that this is not just an optimization but a necessity for
        // field types without main properties (such as the "map" field type).
        if ($data === NULL) {
            return $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, $format, $context);
    }
    $data_internal = [];
    if (!empty($property_definitions)) {
        foreach ($data as $property_name => $property_value) {
            $property_value_class = $property_definitions[$property_name]->getClass();
            $data_internal[$property_name] = $denormalize_property($property_name, $property_value, $property_value_class, $format, $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.