diff --git a/core/core.link_relation.yml b/core/core.link_relation.yml index cccf9e5..2181573 100644 --- a/core/core.link_relation.yml +++ b/core/core.link_relation.yml @@ -1,3 +1,15 @@ +# This are Drupal specific link relations. +add-form: + relationship: https://drupal.org/link-relations/add-form + description: A form where a resource of this type can be created +edit-form: + relationship: https://drupal.org/link-relations/edit-form + description: A form where a resource of this type can be created +create: + relationship: https://drupal.org/link-relations/create + description: A REST resource URL where a resource of this type can be created + +# These are the one from IANA itself. about: description: 'Refers to a resource that is the subject of the link''s context.' reference: '[RFC6903], section 2' diff --git a/core/lib/Drupal/Core/Http/LinkRelation.php b/core/lib/Drupal/Core/Http/LinkRelation.php index 6fa2622..39a937b 100644 --- a/core/lib/Drupal/Core/Http/LinkRelation.php +++ b/core/lib/Drupal/Core/Http/LinkRelation.php @@ -23,8 +23,14 @@ public function getName() { /** * {@inheritdoc} */ + public function getRelationshipUrl() { + return isset($this->pluginDefinition['relationship']) ? $this->pluginDefinition['relationship'] : ''; + } + + /** + * {@inheritdoc} + */ public function getDescription() { - return isset($this->pluginDefinition['description']) ? $this->pluginDefinition['description'] : ''; } /** diff --git a/core/lib/Drupal/Core/Http/LinkRelationInterface.php b/core/lib/Drupal/Core/Http/LinkRelationInterface.php index 62dc923..a6f8738 100644 --- a/core/lib/Drupal/Core/Http/LinkRelationInterface.php +++ b/core/lib/Drupal/Core/Http/LinkRelationInterface.php @@ -17,6 +17,13 @@ public function getName(); /** + * If the relationship is not a IANA one, provide the URL to the definition. + * + * @return string + */ + public function getRelationshipUrl(); + + /** * Returns the link relation description. * * @return string diff --git a/core/modules/rest/src/Plugin/rest/resource/EntityResource.php b/core/modules/rest/src/Plugin/rest/resource/EntityResource.php index a41354f..f678386 100644 --- a/core/modules/rest/src/Plugin/rest/resource/EntityResource.php +++ b/core/modules/rest/src/Plugin/rest/resource/EntityResource.php @@ -376,7 +376,12 @@ protected function addLinkHeaders(EntityInterface $entity, CacheableResponseInte ->toString(TRUE); $response->addCacheableDependency($generator_url); $uri = $generator_url->getGeneratedUrl(); - $link_header = '<' . $uri . '>; rel="' . $relation_name . '"'; + $relationship = $relation_name; + if (!empty($definition['relationship'])) { + $relationship = $definition['relationship']; + } + + $link_header = '<' . $uri . '>; rel="' . $relationship . '"'; $response->headers->set('Link', $link_header, FALSE); } } diff --git a/core/modules/rest/src/Tests/NodeTest.php b/core/modules/rest/src/Tests/NodeTest.php index bf0d89b..0e411dc 100644 --- a/core/modules/rest/src/Tests/NodeTest.php +++ b/core/modules/rest/src/Tests/NodeTest.php @@ -83,7 +83,7 @@ public function testNodes() { $this->httpRequest($node->urlInfo()->setRouteParameter('_format', $this->defaultFormat), 'GET'); $this->assertResponse(200); $this->assertHeader('Content-type', $this->defaultMimeType); - $this->assertLinkHeader($node, ['canonical', 'edit-form', 'version-history']); + $this->assertLinkHeader($node, ['edit-form' => 'https://drupal.org/link-relations/edit-form', 'canonical' => 'canonical', 'version-history' => 'version-history']); // Also check that JSON works and the routing system selects the correct // REST route. @@ -91,7 +91,7 @@ public function testNodes() { $this->httpRequest($node->urlInfo()->setRouteParameter('_format', 'json'), 'GET'); $this->assertResponse(200); $this->assertHeader('Content-type', 'application/json'); - $this->assertLinkHeader($node, ['canonical', 'edit-form', 'version-history']); + $this->assertLinkHeader($node, ['edit-form' => 'https://drupal.org/link-relations/edit-form', 'canonical' => 'canonical', 'version-history' => 'version-history']); // Check that a simple PATCH update to the node title works as expected. $this->enableNodeConfiguration('PATCH', 'update'); diff --git a/core/modules/rest/src/Tests/RESTTestBase.php b/core/modules/rest/src/Tests/RESTTestBase.php index 080c5a4..c03d4d9 100644 --- a/core/modules/rest/src/Tests/RESTTestBase.php +++ b/core/modules/rest/src/Tests/RESTTestBase.php @@ -544,13 +544,13 @@ protected function assertLinkHeader($entity, array $link_relationships = [ ]) { // Add expected Link Headers. $link_headers = []; - foreach ($link_relationships as $link) { - if ($entity->hasLinkTemplate($link)) { - $canonical_url = $entity->toUrl($link) + foreach ($link_relationships as $relation_name => $relationship) { + if ($entity->hasLinkTemplate($relation_name)) { + $canonical_url = $entity->toUrl($relation_name) ->setAbsolute(TRUE) ->toString(TRUE) ->getGeneratedUrl(); - $link_headers[] = '<' . $canonical_url . '>; rel="' . $link . '"'; + $link_headers[] = '<' . $canonical_url . '>; rel="' . $relationship . '"'; } } return $this->assertHeader('Link', implode(',', $link_headers));