You are here

protected function ContentEntityDenormalizer::prepareInput in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/jsonapi/src/Normalizer/ContentEntityDenormalizer.php \Drupal\jsonapi\Normalizer\ContentEntityDenormalizer::prepareInput()
  2. 9 core/modules/jsonapi/src/Normalizer/ContentEntityDenormalizer.php \Drupal\jsonapi\Normalizer\ContentEntityDenormalizer::prepareInput()

Prepares the input data to create the entity.

Parameters

array $data: The input data to modify.

\Drupal\jsonapi\ResourceType\ResourceType $resource_type: Contains the info about the resource type.

string $format: Format the given data was extracted from.

array $context: Options available to the denormalizer.

Return value

array The modified input data.

File

core/modules/jsonapi/src/Normalizer/ContentEntityDenormalizer.php, line 40

Class

ContentEntityDenormalizer
Converts a JSON:API array structure into a Drupal entity object.

Namespace

Drupal\jsonapi\Normalizer

Code

protected function prepareInput(array $data, ResourceType $resource_type, $format, array $context) {
  $data_internal = [];
  $field_map = $this->fieldManager
    ->getFieldMap()[$resource_type
    ->getEntityTypeId()];
  $entity_type_id = $resource_type
    ->getEntityTypeId();
  $entity_type_definition = $this->entityTypeManager
    ->getDefinition($entity_type_id);
  $bundle_key = $entity_type_definition
    ->getKey('bundle');
  $uuid_key = $entity_type_definition
    ->getKey('uuid');

  // User resource objects contain a read-only attribute that is not a real
  // field on the user entity type.
  // @see \Drupal\jsonapi\JsonApiResource\ResourceObject::extractContentEntityFields()
  // @todo: eliminate this special casing in https://www.drupal.org/project/drupal/issues/3079254.
  if ($entity_type_id === 'user') {
    $data = array_diff_key($data, array_flip([
      $resource_type
        ->getPublicName('display_name'),
    ]));
  }

  // Translate the public fields into the entity fields.
  foreach ($data as $public_field_name => $field_value) {
    $internal_name = $resource_type
      ->getInternalName($public_field_name);

    // Skip any disabled field, except the always required bundle key and
    // required-in-case-of-PATCHing uuid key.
    // @see \Drupal\jsonapi\ResourceType\ResourceTypeRepository::getFieldMapping()
    if ($resource_type
      ->hasField($internal_name) && !$resource_type
      ->isFieldEnabled($internal_name) && $bundle_key !== $internal_name && $uuid_key !== $internal_name) {
      continue;
    }
    if (!isset($field_map[$internal_name]) || !in_array($resource_type
      ->getBundle(), $field_map[$internal_name]['bundles'], TRUE)) {
      throw new UnprocessableEntityHttpException(sprintf('The attribute %s does not exist on the %s resource type.', $internal_name, $resource_type
        ->getTypeName()));
    }
    $field_type = $field_map[$internal_name]['type'];
    $field_class = $this->pluginManager
      ->getDefinition($field_type)['list_class'];
    $field_denormalization_context = array_merge($context, [
      'field_type' => $field_type,
      'field_name' => $internal_name,
      'field_definition' => $this->fieldManager
        ->getFieldDefinitions($resource_type
        ->getEntityTypeId(), $resource_type
        ->getBundle())[$internal_name],
    ]);
    $data_internal[$internal_name] = $this->serializer
      ->denormalize($field_value, $field_class, $format, $field_denormalization_context);
  }
  return $data_internal;
}