ContentEntityDenormalizer.php in JSON:API 8.2
File
src/Normalizer/ContentEntityDenormalizer.php
View source
<?php
namespace Drupal\jsonapi\Normalizer;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\jsonapi\ResourceType\ResourceType;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
final class ContentEntityDenormalizer extends EntityDenormalizerBase {
protected $supportedInterfaceOrClass = ContentEntityInterface::class;
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');
foreach ($data as $public_field_name => $field_value) {
$internal_name = $resource_type
->getInternalName($public_field_name);
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;
}
}