Problem/Motivation

Link templates on entity types allows the definition of permanent relationships for entities of that type. In #2113345: Define a mechanism for custom link relationships this was expanded to potentially include ad hoc relationships, seeded with IANA relationships. Such relationships are fantastic for developers and for building hypermedia APIs. However, core currently supports only a single URI template per relationship.

In practice, HTTP Link headers and HAL link entries both make a special point of accepting multiple URLs, because for many of these relationships there is not a unique, single URL that represents the only instance of that relation.

  • Belongs to multiple collections for it's multiple topics.
  • References multiple specifications for DTD and JSON-schema descriptions of the entity type
  • Composite "remix" work derived from multiple sources
  • Games with next & previous relationships for a choose-your-own adventure
  • Language-specific variants for, well, anything.

Part of how multiple URIs are differentiated is support in LINK headers for Target Attributes (RFC 5988 which brings in "hreflang", "media", "title", "title*", "type" for a start, but does not limit it to those. The same properties are supported in HAL links. It should be considered whether expanding to support multiple links might instead be interpreted as expanded to support a general broader range of the link specification.

Proposed resolution

As an alternative to the current annotation:

   links = {
     "canonical" = "/node/{node}",
     "delete-form" = "/node/{node}/delete",
     "edit-form" = "/node/{node}/edit",
     "version-history" = "/node/{node}/revisions",
     "revision" = "/node/{node}/revisions/{node_revision}/view",
   }

we might alternatively support any relationship to somehow reference a multi-dimensional array, indicating the more complete version of link templates at play and preserving backwards compatibility.

Remaining tasks

Discuss if this should be pursued.

User interface changes

None.

API changes

Potential changes to link template declaration for entities.

Data model changes

None.

Comments

Grayside created an issue. See original summary.

dawehner’s picture

Thank you for creating the issue!

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Wim Leers’s picture

Component: rest.module » entity system
Category: Feature request » Task
Issue tags: -REST +API-First Initiative, +BC break, +API change, +Contributed project blocker
Related issues: +#2878463: [PP-1] Define a JSON API link relation for entities

Hah, actually, I needed exactly this in #2878463: [PP-1] Define a JSON API link relation for entities! I need it for the JSON API "related" and "relationships", both of which can have many URLs.

Unfortunately, to make this happen, we'd also need to change \Drupal\Core\Entity\Entity::toUrl(). Which is definitely a BC break.

So I don't see how we can do this before D9 without introducing a parallel API, i.e. yet another way to generate a URL for entities. Perhaps a new getLinks() method, combined with disallowing $entity->toUrl('some-relationship-that-returns-multiple-urls')?

Wim Leers’s picture

Quoting myself from #2878463-9: [PP-1] Define a JSON API link relation for entities:

#7: no, that's the entity type ID, not the entity ID!

#8: Perhaps. What are the link relation names you suggest?

  1. jsonapi-individual
  2. jsonapi-collection
  3. jsonapi-relationship
  4. jsonapi-related

but… jsonapi-relationship and jsonapi-related links don't entirely make sense, because you'd need to add a URL for every related/relationship thing for that entity type + bundle! And that's something that Entity::toUrl() simply does not support: it hardcodes the assumption that for every relationship there is only one URL.

Therefore only jsonapi-individual and jsonapi-collection are ever possible. Which means that we might as well simplify it to jsonapi and jsonapi-collection.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

geek-merlin’s picture

What about something like

links = {
     "canonical" = "/node/{node}",
...
     "canonical:uuid" = "/node/{node_uuid}",
     "canonical:foo" = "/foo/{node_foo}",
...
   }

and then Entity::toUrl('canonical') and Entity::toUrl('canonical:uuid') will return a string, while something like Entity::toUrl('canonical*')does magick and returns a string array. Won't win us a design price but hey, pure API addition.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

andypost’s picture

Is it still makes sense?

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.