public function DataReferenceDefinitionNormalizer::normalize in Schemata 8
Same name in this branch
- 8 schemata_json_schema/src/Normalizer/json/DataReferenceDefinitionNormalizer.php \Drupal\schemata_json_schema\Normalizer\json\DataReferenceDefinitionNormalizer::normalize()
- 8 schemata_json_schema/src/Normalizer/hal/DataReferenceDefinitionNormalizer.php \Drupal\schemata_json_schema\Normalizer\hal\DataReferenceDefinitionNormalizer::normalize()
Overrides DataReferenceDefinitionNormalizer::normalize
File
- schemata_json_schema/
src/ Normalizer/ hal/ DataReferenceDefinitionNormalizer.php, line 52
Class
- DataReferenceDefinitionNormalizer
- Normalizer for Entity References in HAL+JSON style.
Namespace
Drupal\schemata_json_schema\Normalizer\halCode
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,
];
}