trait FieldableEntityNormalizerTrait (View source)

A trait for providing fieldable entity normalization/denormalization methods.

Move this into a FieldableEntityNormalizer in Drupal 9. This is a trait used in \Drupal\serialization\Normalizer\EntityNormalizer to maintain BC. @see https://www.drupal.org/node/2834734

Properties

protected EntityFieldManagerInterface $entityFieldManager

The entity field manager.

protected EntityTypeManagerInterface $entityTypeManager

The entity type manager.

protected EntityTypeRepositoryInterface $entityTypeRepository

The entity type repository.

Methods

string
determineEntityTypeId(string $class, array $context)

Determines the entity type ID to denormalize as.

getEntityTypeDefinition(string $entity_type_id)

Gets the entity type definition.

string
extractBundleData(array $data, EntityTypeInterface $entity_type_definition)

Denormalizes the bundle property so entity creation can use it.

denormalizeFieldData(array $data, FieldableEntityInterface $entity, string $format, array $context)

Denormalizes entity data by denormalizing each field individually.

getEntityTypeRepository()

Returns the entity type repository.

getEntityFieldManager()

Returns the entity field manager.

getEntityTypeManager()

Returns the entity type manager.

mixed
constructValue(mixed $data, array $context)

Build the field item value using the incoming data.

Details

protected string determineEntityTypeId(string $class, array $context)

Determines the entity type ID to denormalize as.

Parameters

string $class

The entity type class to be denormalized to.

array $context

The serialization context data.

Return Value

string

The entity type ID.

protected EntityTypeInterface getEntityTypeDefinition(string $entity_type_id)

Gets the entity type definition.

Parameters

string $entity_type_id

The entity type ID to load the definition for.

Return Value

EntityTypeInterface

The loaded entity type definition.

Exceptions

UnexpectedValueException

protected string extractBundleData(array $data, EntityTypeInterface $entity_type_definition)

Denormalizes the bundle property so entity creation can use it.

Parameters

array $data

The data being denormalized.

EntityTypeInterface $entity_type_definition

The entity type definition.

Return Value

string

The valid bundle name.

Exceptions

UnexpectedValueException

protected denormalizeFieldData(array $data, FieldableEntityInterface $entity, string $format, array $context)

Denormalizes entity data by denormalizing each field individually.

Parameters

array $data

The data to denormalize.

FieldableEntityInterface $entity

The fieldable entity to set field values for.

string $format

The serialization format.

array $context

The context data.

protected EntityTypeRepositoryInterface getEntityTypeRepository()

Returns the entity type repository.

Return Value

EntityTypeRepositoryInterface

The entity type repository.

protected EntityFieldManagerInterface getEntityFieldManager()

Returns the entity field manager.

Return Value

EntityFieldManagerInterface

The entity field manager.

protected EntityTypeManagerInterface getEntityTypeManager()

Returns the entity type manager.

Return Value

EntityTypeManagerInterface

The entity type manager.

protected mixed constructValue(mixed $data, array $context)

Build the field item value using the incoming data.

Most normalizers that extend this class can simply use this method to construct the denormalized value without having to override denormalize() and reimplementing its validation logic or its call to set the field value.

It's recommended to not override this and instead provide a (de)normalizer at the DataType level.

Parameters

mixed $data

The incoming data for this field item.

array $context

The context passed into the Normalizer.

Return Value

mixed

The value to use in Entity::setValue().