class DataReferenceDefinitionNormalizer in Schemata 8
Same name in this branch
- 8 schemata_json_schema/src/Normalizer/json/DataReferenceDefinitionNormalizer.php \Drupal\schemata_json_schema\Normalizer\json\DataReferenceDefinitionNormalizer
- 8 schemata_json_schema/src/Normalizer/hal/DataReferenceDefinitionNormalizer.php \Drupal\schemata_json_schema\Normalizer\hal\DataReferenceDefinitionNormalizer
Normalizer for Entity References.
DataReferenceDefinitions are embedded inside ComplexDataDefinitions, and represent a type property. The key for this is usually "entity", and it is found alongside a "target_id" value which refers to the specific entity instance for the reference. The target_id is not normalized by this class, instead it comes through the DataDefinitionNormalizer as a scalar value.
Hierarchy
- class \Drupal\serialization\Normalizer\NormalizerBase implements \Symfony\Component\Serializer\SerializerAwareInterface, CacheableNormalizerInterface uses \Symfony\Component\Serializer\SerializerAwareTrait
- class \Drupal\schemata\Normalizer\NormalizerBase implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface
- class \Drupal\schemata_json_schema\Normalizer\json\JsonNormalizerBase implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface
- class \Drupal\schemata_json_schema\Normalizer\json\DataDefinitionNormalizer
- class \Drupal\schemata_json_schema\Normalizer\json\DataReferenceDefinitionNormalizer
- class \Drupal\schemata_json_schema\Normalizer\json\DataDefinitionNormalizer
- class \Drupal\schemata_json_schema\Normalizer\json\JsonNormalizerBase implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface
- class \Drupal\schemata\Normalizer\NormalizerBase implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface
Expanded class hierarchy of DataReferenceDefinitionNormalizer
1 file declares its use of DataReferenceDefinitionNormalizer
- DataReferenceDefinitionNormalizer.php in schemata_json_schema/
src/ Normalizer/ hal/ DataReferenceDefinitionNormalizer.php
1 string reference to 'DataReferenceDefinitionNormalizer'
- schemata_json_schema.services.yml in schemata_json_schema/
schemata_json_schema.services.yml - schemata_json_schema/schemata_json_schema.services.yml
1 service uses DataReferenceDefinitionNormalizer
File
- schemata_json_schema/
src/ Normalizer/ json/ DataReferenceDefinitionNormalizer.php, line 19
Namespace
Drupal\schemata_json_schema\Normalizer\jsonView source
class DataReferenceDefinitionNormalizer extends DataDefinitionNormalizer {
/**
* The interface or class that this Normalizer supports.
*
* @var string
*/
protected $supportedInterfaceOrClass = DataReferenceDefinitionInterface::class;
/**
* EntityTypeManager.
*
* @var \Drupal\Core\Entity\EntityTypeManager
*/
protected $entityTypeManager;
/**
* Constructs an DataReferenceDefinitionNormalizer object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The Entity Type Manager.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public function normalize($entity, $format = NULL, array $context = []) {
/* @var $entity \Drupal\Core\TypedData\DataReferenceDefinitionInterface */
try {
$is_valid = $this
->validateEntity($entity);
} catch (PluginNotFoundException $exception) {
$is_valid = FALSE;
}
// DataDefinitionNormalizer::normalize() results in extraneous structures
// added to the schema for this field element (e.g., entity)
return $is_valid ? $this
->extractPropertyData($entity, $context) : [];
}
/**
* Ensure the entity type is one we support for schema reference.
*
* If somehow the entity does not exist, or is not a ContentEntity, skip it.
*
* @param mixed $entity
* The object to be normalized.
*
* @return bool
* TRUE if valid for use.
*
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
* If the plugin could not be found.
*/
protected function validateEntity($entity) {
// Only entity references have a schema.
// This leads to incompatibility with alternate reference modules such as
// Dynamic Entity Reference.
if ($entity
->getDataType() != 'entity_reference') {
return FALSE;
}
$entity_type_plugin = $this->entityTypeManager
->getDefinition($entity
->getConstraint('EntityType'), FALSE);
return !empty($entity_type_plugin) && $entity_type_plugin
->entityClassImplements(ContentEntityInterface::class);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CacheableNormalizerInterface:: |
constant | Name of key for bubbling cacheability metadata via serialization context. | ||
DataDefinitionNormalizer:: |
protected | function | Extracts property details from a data definition. | |
DataReferenceDefinitionNormalizer:: |
protected | property | EntityTypeManager. | |
DataReferenceDefinitionNormalizer:: |
protected | property |
The interface or class that this Normalizer supports. Overrides DataDefinitionNormalizer:: |
|
DataReferenceDefinitionNormalizer:: |
public | function |
Normalizes an object into a set of arrays/scalars. Overrides DataDefinitionNormalizer:: |
1 |
DataReferenceDefinitionNormalizer:: |
protected | function | Ensure the entity type is one we support for schema reference. | |
DataReferenceDefinitionNormalizer:: |
public | function | Constructs an DataReferenceDefinitionNormalizer object. | 1 |
JsonNormalizerBase:: |
protected | property | The formats that the Normalizer can handle. | 7 |
JsonNormalizerBase:: |
protected | property |
The formats that the Normalizer can handle. Overrides NormalizerBase:: |
7 |
NormalizerBase:: |
protected | function | Adds cacheability if applicable. | |
NormalizerBase:: |
protected | function |
Checks if the provided format is supported by this normalizer. Overrides NormalizerBase:: |
|
NormalizerBase:: |
public | function | Denormalizes data back into an object of the given class. | |
NormalizerBase:: |
protected | function | Normalize an array of data definitions. | |
NormalizerBase:: |
protected | function | Determine if the given property is a required element of the schema. | |
NormalizerBase:: |
public | function |
Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization() Overrides NormalizerBase:: |
|
NormalizerBase:: |
public | function | Checks whether the given class is supported for normalization by this normalizer. | 1 |