class ContentEntityNormalizer extends NormalizerBase (View source)

Converts the Drupal entity object structure to a HAL array structure.

Traits

A trait for providing fieldable entity normalization/denormalization methods.

SerializerAwareTrait

Properties

protected $supportedInterfaceOrClass {@inheritdoc}
protected $format {@inheritdoc} from  NormalizerBase
protected EntityFieldManagerInterface $entityFieldManager

The entity field manager.

from  FieldableEntityNormalizerTrait
protected EntityTypeManagerInterface $entityTypeManager

The entity type manager.

from  FieldableEntityNormalizerTrait
protected EntityTypeRepositoryInterface $entityTypeRepository

The entity type repository.

from  FieldableEntityNormalizerTrait
protected LinkManagerInterface $linkManager

The hypermedia link manager.

protected ModuleHandlerInterface $moduleHandler

The module handler.

Methods

supportsNormalization($data, $format = NULL)

{@inheritdoc}

supportsDenormalization($data, $type, $format = NULL)

Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization()

bool
checkFormat(string $format = NULL)

Checks if the provided format is supported by this normalizer.

addCacheableDependency(array $context, $data)

Adds cacheability if applicable.

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.

mixed
constructValue(mixed $data, array $context)

Build the field item value using the incoming data.

__construct(LinkManagerInterface $link_manager, EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler, EntityTypeRepositoryInterface $entity_type_repository, EntityFieldManagerInterface $entity_field_manager)

Constructs a ContentEntityNormalizer object.

normalize($entity, $format = NULL, array $context = [])

{@inheritdoc}

denormalize(array $data, string $class, string $format = NULL, array $context = [])

Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::denormalize().

string
getEntityUri(EntityInterface $entity, array $context = [])

Constructs the entity URI.

array
getTypedDataIds(array $types, array $context = [])

Gets the typed data IDs for a type URI.

Details

supportsNormalization($data, $format = NULL)

{@inheritdoc}

Parameters

$data
$format

supportsDenormalization($data, $type, $format = NULL)

Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization()

This class doesn't implement DenormalizerInterface, but most of its child classes do. Therefore, this method is implemented at this level to reduce code duplication.

Parameters

$data
$type
$format

protected bool checkFormat(string $format = NULL)

Checks if the provided format is supported by this normalizer.

Parameters

string $format

The format to check.

Return Value

bool

TRUE if the format is supported, FALSE otherwise. If no format is specified this will return TRUE.

protected addCacheableDependency(array $context, $data)

Adds cacheability if applicable.

Parameters

array $context

Context options for the normalizer.

$data

The data that might have cacheability information.

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().

__construct(LinkManagerInterface $link_manager, EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler, EntityTypeRepositoryInterface $entity_type_repository, EntityFieldManagerInterface $entity_field_manager)

Constructs a ContentEntityNormalizer object.

Parameters

LinkManagerInterface $link_manager

The hypermedia link manager.

EntityTypeManagerInterface $entity_type_manager

The entity type manager.

ModuleHandlerInterface $module_handler

The module handler.

EntityTypeRepositoryInterface $entity_type_repository

The entity type repository.

EntityFieldManagerInterface $entity_field_manager

The entity field manager.

normalize($entity, $format = NULL, array $context = [])

{@inheritdoc}

Parameters

$entity
$format
array $context

EntityInterface denormalize(array $data, string $class, string $format = NULL, array $context = [])

Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::denormalize().

Parameters

array $data

Entity data to restore.

string $class

Unused parameter.

string $format

Format the given data was extracted from.

array $context

Options available to the denormalizer. Keys that can be used:

  • request_method: if set to "patch" the denormalization will clear out all default values for entity fields before applying $data to the entity.

Return Value

EntityInterface

An unserialized entity object containing the data in $data.

Exceptions

UnexpectedValueException

protected string getEntityUri(EntityInterface $entity, array $context = [])

Constructs the entity URI.

Parameters

EntityInterface $entity

The entity.

array $context

Normalization/serialization context.

Return Value

string

The entity URI.

protected array getTypedDataIds(array $types, array $context = [])

Gets the typed data IDs for a type URI.

Parameters

array $types

The type array(s) (value of the 'type' attribute of the incoming data).

array $context

Context from the normalizer/serializer operation.

Return Value

array

The typed data IDs.