public function RelationshipNormalizer::normalize in JSON:API 8
Helper function to normalize field items.
Parameters
\Drupal\jsonapi\Normalizer\Relationship|object $relationship: The field object.
string $format: The format.
array $context: The context array.
Return value
\Drupal\jsonapi\Normalizer\Value\RelationshipNormalizerValue The array of normalized field items.
File
- src/
Normalizer/ RelationshipNormalizer.php, line 76
Class
- RelationshipNormalizer
- Normalizes a Relationship according to the JSON API specification.
Namespace
Drupal\jsonapi\NormalizerCode
public function normalize($relationship, $format = NULL, array $context = []) {
/* @var \Drupal\jsonapi\Normalizer\Relationship $relationship */
$normalizer_items = [];
foreach ($relationship
->getItems() as $relationship_item) {
// If the relationship points to a disabled resource type, do not add the
// normalized relationship item.
if (!$relationship_item
->getTargetResourceType()) {
continue;
}
$normalizer_items[] = $this->serializer
->normalize($relationship_item, $format, $context);
}
$cardinality = $relationship
->getCardinality();
$link_context = [
'host_entity_id' => $relationship
->getHostEntity()
->uuid(),
'field_name' => $relationship
->getPropertyName(),
'link_manager' => $this->linkManager,
'resource_type' => $context['resource_type'],
];
// If this is called, access to the Relationship field is allowed. The
// cacheability of the access result is carried by the Relationship value
// object. Therefore, we can safely construct an access result object here.
// Access to the targeted related resources will be checked separately.
// @see \Drupal\jsonapi\Normalizer\EntityReferenceFieldNormalizer::normalize()
// @see \Drupal\jsonapi\Normalizer\RelationshipItemNormalizer::normalize()
$relationship_access = AccessResult::allowed()
->addCacheableDependency($relationship);
return new RelationshipNormalizerValue($relationship_access, $normalizer_items, $cardinality, $link_context);
}