protected static function ResourceObject::buildLinksFromEntity in Drupal 10
Same name and namespace in other branches
- 8 core/modules/jsonapi/src/JsonApiResource/ResourceObject.php \Drupal\jsonapi\JsonApiResource\ResourceObject::buildLinksFromEntity()
- 9 core/modules/jsonapi/src/JsonApiResource/ResourceObject.php \Drupal\jsonapi\JsonApiResource\ResourceObject::buildLinksFromEntity()
Builds a LinkCollection for the given entity.
Parameters
\Drupal\jsonapi\ResourceType\ResourceType $resource_type: The JSON:API resource type of the given entity.
\Drupal\Core\Entity\EntityInterface $entity: The entity for which to build links.
\Drupal\jsonapi\JsonApiResource\LinkCollection $links: (optional) Any extra links for the resource object, if a `self` link is not provided, one will be automatically added if the resource is locatable and is not an internal entity.
Return value
\Drupal\jsonapi\JsonApiResource\LinkCollection The built links.
File
- core/
modules/ jsonapi/ src/ JsonApiResource/ ResourceObject.php, line 262
Class
- ResourceObject
- Represents a JSON:API resource object.
Namespace
Drupal\jsonapi\JsonApiResourceCode
protected static function buildLinksFromEntity(ResourceType $resource_type, EntityInterface $entity, LinkCollection $links) {
if ($resource_type
->isLocatable() && !$resource_type
->isInternal()) {
$self_url = Url::fromRoute(Routes::getRouteName($resource_type, 'individual'), [
'entity' => $entity
->uuid(),
]);
if ($resource_type
->isVersionable()) {
assert($entity instanceof RevisionableInterface);
if (!$links
->hasLinkWithKey('self')) {
// If the resource is versionable, the `self` link should be the exact
// link for the represented version. This helps a client track
// revision changes and to disambiguate resource objects with the same
// `type` and `id` in a `version-history` collection.
$self_with_version_url = $self_url
->setOption('query', [
JsonApiSpec::VERSION_QUERY_PARAMETER => 'id:' . $entity
->getRevisionId(),
]);
$links = $links
->withLink('self', new Link(new CacheableMetadata(), $self_with_version_url, 'self'));
}
if (!$entity
->isDefaultRevision()) {
$latest_version_url = $self_url
->setOption('query', [
JsonApiSpec::VERSION_QUERY_PARAMETER => 'rel:' . VersionByRel::LATEST_VERSION,
]);
$links = $links
->withLink(VersionByRel::LATEST_VERSION, new Link(new CacheableMetadata(), $latest_version_url, VersionByRel::LATEST_VERSION));
}
if (!$entity
->isLatestRevision()) {
$working_copy_url = $self_url
->setOption('query', [
JsonApiSpec::VERSION_QUERY_PARAMETER => 'rel:' . VersionByRel::WORKING_COPY,
]);
$links = $links
->withLink(VersionByRel::WORKING_COPY, new Link(new CacheableMetadata(), $working_copy_url, VersionByRel::WORKING_COPY));
}
}
if (!$links
->hasLinkWithKey('self')) {
$links = $links
->withLink('self', new Link(new CacheableMetadata(), $self_url, 'self'));
}
}
return $links;
}