EntityNormalizer.php in Drupal 9
File
core/modules/serialization/src/Normalizer/EntityNormalizer.php
View source
<?php
namespace Drupal\serialization\Normalizer;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityTypeRepositoryInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
class EntityNormalizer extends ComplexDataNormalizer implements DenormalizerInterface {
use FieldableEntityNormalizerTrait;
protected $supportedInterfaceOrClass = EntityInterface::class;
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeRepositoryInterface $entity_type_repository, EntityFieldManagerInterface $entity_field_manager) {
$this->entityTypeManager = $entity_type_manager;
$this->entityTypeRepository = $entity_type_repository;
$this->entityFieldManager = $entity_field_manager;
}
public function denormalize($data, $class, $format = NULL, array $context = []) {
$entity_type_id = $this
->determineEntityTypeId($class, $context);
$entity_type_definition = $this
->getEntityTypeDefinition($entity_type_id);
if ($entity_type_definition
->entityClassImplements(FieldableEntityInterface::class)) {
if ($entity_type_definition
->hasKey('bundle')) {
$create_params = $this
->extractBundleData($data, $entity_type_definition);
}
else {
$create_params = [];
}
$entity = $this->entityTypeManager
->getStorage($entity_type_id)
->create($create_params);
$this
->denormalizeFieldData($data, $entity, $format, $context);
}
else {
$entity = $this->entityTypeManager
->getStorage($entity_type_id)
->create($data);
}
$entity->_restSubmittedFields = array_keys($data);
return $entity;
}
}
Classes
Name |
Description |
EntityNormalizer |
Normalizes/denormalizes Drupal entity objects into an array structure. |