You are here

protected static function ResourceObject::buildLinksFromEntity in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/jsonapi/src/JsonApiResource/ResourceObject.php \Drupal\jsonapi\JsonApiResource\ResourceObject::buildLinksFromEntity()
  2. 10 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 236

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