function EntityResource::addLinkHeaders

Same name in other branches
  1. 9 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()
  3. 11.x 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 449

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

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