function FieldItemNormalizer::denormalize

Same name in this branch
  1. 10 core/modules/serialization/src/Normalizer/FieldItemNormalizer.php \Drupal\serialization\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. 8.9.x core/modules/jsonapi/src/Normalizer/FieldItemNormalizer.php \Drupal\jsonapi\Normalizer\FieldItemNormalizer::denormalize()
  5. 8.9.x core/modules/serialization/src/Normalizer/FieldItemNormalizer.php \Drupal\serialization\Normalizer\FieldItemNormalizer::denormalize()
  6. 8.9.x core/modules/hal/src/Normalizer/FieldItemNormalizer.php \Drupal\hal\Normalizer\FieldItemNormalizer::denormalize()
  7. 11.x core/modules/jsonapi/src/Normalizer/FieldItemNormalizer.php \Drupal\jsonapi\Normalizer\FieldItemNormalizer::denormalize()
  8. 11.x core/modules/serialization/src/Normalizer/FieldItemNormalizer.php \Drupal\serialization\Normalizer\FieldItemNormalizer::denormalize()

File

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

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 = []) : mixed {
  $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)) {
    $writable_properties = array_keys(array_filter($property_definitions, function (DataDefinitionInterface $data_definition) : bool {
      return !$data_definition->isReadOnly();
    }));
    $invalid_property_names = [];
    foreach ($data as $property_name => $property_value) {
      if (!isset($property_definitions[$property_name])) {
        $alt = static::getAlternatives($property_name, $writable_properties);
        $invalid_property_names[$property_name] = reset($alt);
      }
    }
    if (!empty($invalid_property_names)) {
      $suggestions = array_values(array_filter($invalid_property_names));
      // Only use the "Did you mean"-style error message if there is a
      // suggestion for every invalid property name.
      if (count($suggestions) === count($invalid_property_names)) {
        $format = count($invalid_property_names) === 1 ? "The property '%s' does not exist on the '%s' field of type '%s'. Did you mean '%s'?" : "The properties '%s' do not exist on the '%s' field of type '%s'. Did you mean '%s'?";
        throw new UnexpectedValueException(sprintf($format, implode("', '", array_keys($invalid_property_names)), $item_definition->getFieldDefinition()
          ->getName(), $item_definition->getFieldDefinition()
          ->getType(), implode("', '", $suggestions)));
      }
      else {
        $format = count($invalid_property_names) === 1 ? "The property '%s' does not exist on the '%s' field of type '%s'. Writable properties are: '%s'." : "The properties '%s' do not exist on the '%s' field of type '%s'. Writable properties are: '%s'.";
        throw new UnexpectedValueException(sprintf($format, implode("', '", array_keys($invalid_property_names)), $item_definition->getFieldDefinition()
          ->getName(), $item_definition->getFieldDefinition()
          ->getType(), implode("', '", $writable_properties)));
      }
    }
    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.