You are here

protected function EntityResource::addLinkHeaders in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/rest/src/Plugin/rest/resource/EntityResource.php \Drupal\rest\Plugin\rest\resource\EntityResource::addLinkHeaders()
  2. 10 core/modules/rest/src/Plugin/rest/resource/EntityResource.php \Drupal\rest\Plugin\rest\resource\EntityResource::addLinkHeaders()

Adds link headers to a response.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity.

\Symfony\Component\HttpFoundation\Response $response: The response.

See also

https://tools.ietf.org/html/rfc5988#section-5

1 call to EntityResource::addLinkHeaders()
EntityResource::get in core/modules/rest/src/Plugin/rest/resource/EntityResource.php
Responds to entity GET requests.

File

core/modules/rest/src/Plugin/rest/resource/EntityResource.php, line 441

Class

EntityResource
Represents entities as resources.

Namespace

Drupal\rest\Plugin\rest\resource

Code

protected function addLinkHeaders(EntityInterface $entity, Response $response) {
  foreach ($entity
    ->uriRelationships() as $relation_name) {
    if ($this->linkRelationTypeManager
      ->hasDefinition($relation_name)) {

      /** @var \Drupal\Core\Http\LinkRelationTypeInterface $link_relation_type */
      $link_relation_type = $this->linkRelationTypeManager
        ->createInstance($relation_name);
      $generator_url = $entity
        ->toUrl($relation_name)
        ->setAbsolute(TRUE)
        ->toString(TRUE);
      if ($response instanceof CacheableResponseInterface) {
        $response
          ->addCacheableDependency($generator_url);
      }
      $uri = $generator_url
        ->getGeneratedUrl();
      $relationship = $link_relation_type
        ->isRegistered() ? $link_relation_type
        ->getRegisteredName() : $link_relation_type
        ->getExtensionUri();
      $link_header = '<' . $uri . '>; rel="' . $relationship . '"';
      $response->headers
        ->set('Link', $link_header, FALSE);
    }
  }
}