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 in HAL+JSON style.
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\hal\DataReferenceDefinitionNormalizer
- 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
File
- schemata_json_schema/
src/ Normalizer/ hal/ DataReferenceDefinitionNormalizer.php, line 13
Namespace
Drupal\schemata_json_schema\Normalizer\halView source
class DataReferenceDefinitionNormalizer extends JsonDataReferenceDefinitionNormalizer {
/**
* The formats that the Normalizer can handle.
*
* @var array
*/
protected $format = 'schema_json';
/**
* The formats that the Normalizer can handle.
*
* @var array
*/
protected $describedFormat = 'hal_json';
/**
* The hypermedia link manager.
*
* @var \Drupal\hal\LinkManager\LinkManagerInterface
*/
protected $linkManager;
/**
* Constructs an DataReferenceDefinitionNormalizer object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The Entity Type Manager.
* @param \Drupal\hal\LinkManager\LinkManagerInterface $link_manager
* The hypermedia link manager.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, LinkManagerInterface $link_manager) {
parent::__construct($entity_type_manager);
$this->linkManager = $link_manager;
}
/**
* {@inheritdoc}
*/
public function normalize($entity, $format = NULL, array $context = []) {
/* @var $entity \Drupal\Core\TypedData\DataReferenceDefinitionInterface */
if (!$this
->validateEntity($entity)) {
return [];
}
// Collect data about the reference field.
$parentProperty = $this
->extractPropertyData($context['parent'], $context);
$property = $this
->extractPropertyData($entity, $context);
$target_type = $entity
->getConstraint('EntityType');
$target_bundles = isset($context['settings']['handler_settings']['target_bundles']) ? $context['settings']['handler_settings']['target_bundles'] : [];
// Build the relation URI, which is used as the property key.
$field_uri = $this->linkManager
->getRelationUri($context['entityTypeId'], isset($context['bundleId']) ? $context['bundleId'] : $context['entityTypeId'], $context['name'], $context);
// From the root of the schema object, build out object references.
$normalized = [
'_links' => [
$field_uri => [
'$ref' => '#/definitions/linkArray',
],
],
'_embedded' => [
$field_uri => [
'type' => 'array',
'items' => [],
],
],
];
// Add title and description to relation definition.
if (isset($parentProperty['title'])) {
$normalized['_links'][$field_uri]['title'] = $parentProperty['title'];
$normalized['_embedded'][$field_uri]['title'] = $parentProperty['title'];
}
if (isset($parentProperty['description'])) {
$normalized['_links'][$field_uri]['description'] = $parentProperty['description'];
}
// Add Schema resource references.
$item =& $normalized['_embedded'][$field_uri]['items'];
if (empty($target_bundles)) {
$generated_url = SchemaUrl::fromOptions($this->format, $this->describedFormat, $target_type)
->toString(TRUE);
$item['$ref'] = $generated_url
->getGeneratedUrl();
}
elseif (count($target_bundles) == 1) {
$generated_url = SchemaUrl::fromOptions($this->format, $this->describedFormat, $target_type, reset($target_bundles))
->toString(TRUE);
$item['$ref'] = $generated_url
->getGeneratedUrl();
}
elseif (count($target_bundles) > 1) {
$refs = [];
foreach ($target_bundles as $bundle) {
$generated_url = SchemaUrl::fromOptions($this->format, $this->describedFormat, $target_type, $bundle)
->toString(TRUE);
$refs[] = [
'$ref' => $generated_url
->getGeneratedUrl(),
];
}
$item['anyOf'] = $refs;
}
return [
'properties' => $normalized,
];
}
}
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 |
The formats that the Normalizer can handle. Overrides JsonNormalizerBase:: |
|
DataReferenceDefinitionNormalizer:: |
protected | property | EntityTypeManager. | |
DataReferenceDefinitionNormalizer:: |
protected | property |
The formats that the Normalizer can handle. Overrides JsonNormalizerBase:: |
|
DataReferenceDefinitionNormalizer:: |
protected | property | The hypermedia link manager. | |
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 DataReferenceDefinitionNormalizer:: |
|
DataReferenceDefinitionNormalizer:: |
protected | function | Ensure the entity type is one we support for schema reference. | |
DataReferenceDefinitionNormalizer:: |
public | function |
Constructs an DataReferenceDefinitionNormalizer object. Overrides DataReferenceDefinitionNormalizer:: |
|
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 |