class EntityNormalizer in Drupal 9
Same name and namespace in other branches
- 8 core/modules/serialization/src/Normalizer/EntityNormalizer.php \Drupal\serialization\Normalizer\EntityNormalizer
- 10 core/modules/serialization/src/Normalizer/EntityNormalizer.php \Drupal\serialization\Normalizer\EntityNormalizer
Normalizes/denormalizes Drupal entity objects into an array structure.
Hierarchy
- class \Drupal\serialization\Normalizer\NormalizerBase implements \Symfony\Component\Serializer\SerializerAwareInterface, CacheableNormalizerInterface uses \Symfony\Component\Serializer\SerializerAwareTrait
- class \Drupal\serialization\Normalizer\ComplexDataNormalizer
- class \Drupal\serialization\Normalizer\EntityNormalizer implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface uses FieldableEntityNormalizerTrait
- class \Drupal\serialization\Normalizer\ComplexDataNormalizer
Expanded class hierarchy of EntityNormalizer
1 string reference to 'EntityNormalizer'
- serialization.services.yml in core/
modules/ serialization/ serialization.services.yml - core/modules/serialization/serialization.services.yml
1 service uses EntityNormalizer
- serializer.normalizer.entity in core/
modules/ serialization/ serialization.services.yml - Drupal\serialization\Normalizer\EntityNormalizer
File
- core/
modules/ serialization/ src/ Normalizer/ EntityNormalizer.php, line 15
Namespace
Drupal\serialization\NormalizerView source
class EntityNormalizer extends ComplexDataNormalizer implements DenormalizerInterface {
use FieldableEntityNormalizerTrait;
/**
* {@inheritdoc}
*/
protected $supportedInterfaceOrClass = EntityInterface::class;
/**
* Constructs an EntityNormalizer object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Entity\EntityTypeRepositoryInterface $entity_type_repository
* The entity type repository.
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
* The entity field manager.
*/
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;
}
/**
* {@inheritdoc}
*/
public function denormalize($data, $class, $format = NULL, array $context = []) {
$entity_type_id = $this
->determineEntityTypeId($class, $context);
$entity_type_definition = $this
->getEntityTypeDefinition($entity_type_id);
// The bundle property will be required to denormalize a bundleable
// fieldable entity.
if ($entity_type_definition
->entityClassImplements(FieldableEntityInterface::class)) {
// Extract bundle data to pass into entity creation if the entity type uses
// bundles.
if ($entity_type_definition
->hasKey('bundle')) {
// Get an array containing the bundle only. This also remove the bundle
// key from the $data array.
$create_params = $this
->extractBundleData($data, $entity_type_definition);
}
else {
$create_params = [];
}
// Create the entity from bundle data only, then apply field values after.
$entity = $this->entityTypeManager
->getStorage($entity_type_id)
->create($create_params);
$this
->denormalizeFieldData($data, $entity, $format, $context);
}
else {
// Create the entity from all data.
$entity = $this->entityTypeManager
->getStorage($entity_type_id)
->create($data);
}
// Pass the names of the fields whose values can be merged.
// @todo https://www.drupal.org/node/2456257 remove this.
$entity->_restSubmittedFields = array_keys($data);
return $entity;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CacheableNormalizerInterface:: |
constant | Name of key for bubbling cacheability metadata via serialization context. | ||
ComplexDataNormalizer:: |
public | function | 7 | |
EntityNormalizer:: |
protected | property |
The interface or class that this Normalizer supports. Overrides ComplexDataNormalizer:: |
2 |
EntityNormalizer:: |
public | function | 1 | |
EntityNormalizer:: |
public | function | Constructs an EntityNormalizer object. | |
FieldableEntityNormalizerTrait:: |
protected | property | The entity field manager. | |
FieldableEntityNormalizerTrait:: |
protected | property | The entity type manager. | 1 |
FieldableEntityNormalizerTrait:: |
protected | property | The entity type repository. | |
FieldableEntityNormalizerTrait:: |
protected | function | Build the field item value using the incoming data. | 7 |
FieldableEntityNormalizerTrait:: |
protected | function | Denormalizes entity data by denormalizing each field individually. | |
FieldableEntityNormalizerTrait:: |
protected | function | Determines the entity type ID to denormalize as. | |
FieldableEntityNormalizerTrait:: |
protected | function | Denormalizes the bundle property so entity creation can use it. | |
FieldableEntityNormalizerTrait:: |
protected | function | Returns the entity field manager. | |
FieldableEntityNormalizerTrait:: |
protected | function | Gets the entity type definition. | |
FieldableEntityNormalizerTrait:: |
protected | function | Returns the entity type manager. | |
FieldableEntityNormalizerTrait:: |
protected | function | Returns the entity type repository. | |
NormalizerBase:: |
protected | property | List of formats which supports (de-)normalization. | 3 |
NormalizerBase:: |
protected | function | Adds cacheability if applicable. | |
NormalizerBase:: |
protected | function | Checks if the provided format is supported by this normalizer. | 1 |
NormalizerBase:: |
public | function | Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization() | 1 |
NormalizerBase:: |
public | function | 1 |