function ResourceObject::buildLinksFromEntity

Same name in other branches
  1. 9 core/modules/jsonapi/src/JsonApiResource/ResourceObject.php \Drupal\jsonapi\JsonApiResource\ResourceObject::buildLinksFromEntity()
  2. 8.9.x core/modules/jsonapi/src/JsonApiResource/ResourceObject.php \Drupal\jsonapi\JsonApiResource\ResourceObject::buildLinksFromEntity()
  3. 11.x 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.

2 calls to ResourceObject::buildLinksFromEntity()
LabelOnlyResourceObject::createFromEntity in core/modules/jsonapi/src/JsonApiResource/LabelOnlyResourceObject.php
Creates a new ResourceObject from an entity.
ResourceObject::createFromEntity in core/modules/jsonapi/src/JsonApiResource/ResourceObject.php
Creates a new ResourceObject from an entity.

File

core/modules/jsonapi/src/JsonApiResource/ResourceObject.php, line 262

Class

ResourceObject
Represents a JSON:API resource object.

Namespace

Drupal\jsonapi\JsonApiResource

Code

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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.