backdrop / backdrop-issues

Issue tracker for Backdrop core.
144 stars 40 forks source link

[DX][D8] Allow entity types to specify canonical and additional URIs #5212

Open klonos opened 3 years ago

klonos commented 3 years ago

At the very minimum, we should add canonical URIs to all core-provided entity types.

This is basically a request to add the same feature as https://www.drupal.org/project/drupal/issues/1970360

Problem/Motivation

Currently, there is no central definition of the path to an entity. Sure there's its route/menu item, but we can't reverse associate from that.

Sure entities now have a uri() method, but you need an entity object for that. Also, the default implementation still uses a uri callback function, for no reason I can fathom.

...

Proposed resolution

Bite the bullet. What we really need here is to define a canonical path TEMPLATE for an entity type. When I say template, I mean the uri-template specification, RFC 6570:

http://tools.ietf.org/html/rfc6570

...

In particular, we should define multiple such patterns. One for each operation, as identified by a link. ... What does "identified by a link" mean? I mean using one of the IANA standardized link relationships (which collects relationships defined by IETF RFCs as well as the W3C):

http://www.iana.org/assignments/link-relations/link-relations.xml

It's possible to define your own, but there's plenty there we can already use.

...nodes might look something like:

links = {
  "canonical" = "/node/{node}",
  "edit-form" = "/node/{node}/edit",
  "create-form" = "/node/add/{entity_subtype}",
  "version-history" = "/node/{node}/revisions"
}

D8 change record: https://www.drupal.org/node/2020491

klonos commented 3 years ago

Related: https://www.drupal.org/project/drupal/issues/2720215

How do i discover the canonical route for an entity type?

I do not have a ContentEntity instance, only an instance EntityTypeInterface. How do I discover the canonical Route for the entity type?

getCanonicalRoute() seems like a good contender, but for some reason it's protected and not implemented by NodeRouteProvider or UserRouteProvider

klonos commented 3 years ago

The inspiration for this request is this issue for the devel module: https://github.com/backdrop-contrib/devel/issues/115. The fix for that issue is simple, but:

If all core entities defined a canonical URI, then instead of requiring to manually account for any new core entity type or any custom one, Devel would be able to be more "dynamic", and do something like this:

$entity_types = entity_get_info();
foreach ($entity_types as $entity_type) {
  $canonical_link = $entity_type->get_links('canonical');
  [... add devel-provided links ...]
}

...or take advantage of a hook, and add these to every entity type:

links = {
  "devel" = "/node/{node}/devel",
  "devel-load" = "/node/{node}/devel/load",
  "devel-render" = "/node/{node}/devel/render",
}