API page: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21...

The documentation on this page related to routing appears to have some errors in it. I can tell you that it's wrong, but I'm not quite sure what it should be yet. If someone can help me figure out what it should say I'm happy to update things.

Right now, the documentation says this:

For content entities that are displayed on their own pages, the annotation will refer to a 'uri_callback' function, which takes an object of the entity interface you have defined as its parameter, and returns routing information for the entity page; see node_uri() for an example. You will also need to add a corresponding route to your module's routing.yml file; see the entity.node.canonical route in node.routing.yml for an example, and see Entity routes below for some notes.

Optionally, instead of defining routes, routes can be auto generated by providing a route handler. See Entity routes. Otherwise, define routes and links for the various URLs associated with the entity. These go into the 'links' annotation, with the link type as the key, and the path of this link template as the value. The corresponding route requires the following route name: "entity.$entity_type_id.$link_template_type". See Entity routes below for some routing notes. Typical link types are:

The node.routing.yml does not have an entry for the entity.node.canonical route, and the annotation for \Drupal\node\Entity\Node does not have a "uri_callback" key. I believe this is related to #2010184: [meta] convert ‘uri_callback’ entities param to EntityInterface::uri() method. The entity.node.canonical route is not dynamically generated, as are most other similar styles routes. It does look forum, and comment entities have not been updated to the new pattern yet.

The newer, and from what I can tell recommended, pattern is the 2nd one described in the documentation above. Providing a route handler.

Is the use of "uri_callback" in an annotation deprecated? Is it removed and we're just figuring out how to deal with comment/forum entities still? We should either remove that block, or update it so that it references a module like comment that actually makes use of the system being described.

Issue fork drupal-2667040

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

eojthebrave created an issue. See original summary.

jhodgdon’s picture

Title: Update documentation about providing routes for entities » Update documentation about uri_callback in routes for entities
Parent issue: » #2010184: [meta] convert ‘uri_callback’ entities param to EntityInterface::uri() method
Related issues: -#2010184: [meta] convert ‘uri_callback’ entities param to EntityInterface::uri() method

Um.

OK, so the second paragraph does say that you have the option of providing the routes in a routing.yml file, or using a route handler class. That is correct -- you can do either one. Node, and I think all the rest of the core entities, use route handlers.... so I think the 2nd paragraph is fine.

So... it looks like just the first paragraph is wrong. You don't have to use uri_callback any more, and in fact it seems to be kind of deprecated.

It looks like it was removed as part of #2010184: [meta] convert ‘uri_callback’ entities param to EntityInterface::uri() method or a related issue, but the docs were unfortunately not updated and there is also not a change notice. :(((((

I think we should make this a child issue of that other one. I'll comment over there too. Not having a change record and having docs like this is Not Really OK.

longwave’s picture

Status: Active » Needs review
StatusFileSize
new3.48 KB

An attempt at improving this. It does appear that uri_callback is effectively deprecated now, though it is still used in some edge cases.

jhodgdon’s picture

Status: Needs review » Needs work

Thanks! Most of this seems like a great improvement.

One thing I found confusing though:

+++ b/core/lib/Drupal/Core/Entity/entity.api.php
@@ -714,7 +713,8 @@ function hook_entity_view_mode_info_alter(&$view_modes) {
  *   - uri_callback: The same as the 'uri_callback' key defined for the entity
  *     type in the EntityManager, but for the bundle only. When determining
  *     the URI of an entity, if a 'uri_callback' is defined for both the
- *     entity type and the bundle, the one for the bundle is used.
+ *     entity type and the bundle, and the one for the entity type would
+ *     normally be used, the one for the bundle is used instead.

What does this really mean, about using the uri callback for the bundle? I don't understand it. Actually, I don't understand the previous docs either, but the added text 'and the one for the entity type would normally be used' makes it even more confusing, in my opinion.

longwave’s picture

Status: Needs work » Needs review
StatusFileSize
new3.67 KB
new1.12 KB

Let's try a simpler approach to that section.

jhodgdon’s picture

+++ b/core/lib/Drupal/Core/Entity/entity.api.php
@@ -711,10 +710,8 @@ function hook_entity_view_mode_info_alter(&$view_modes) {
+ *   - uri_callback: (optional) Overrides the 'uri_callback' key defined in the
+ *     entity type annotation, for the bundle only.

Ah, that is definitely way more clear!!

Nitpick: maybe replace "the" with "this" at the end? "... for this bundle only".

longwave’s picture

StatusFileSize
new3.67 KB
new761 bytes

Yep, that reads better.

jhodgdon’s picture

Status: Needs review » Reviewed & tested by the community

OK, looks good to me, thanks!

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 7: 2667040-uri_callback-docs.patch, failed testing.

longwave’s picture

Status: Needs work » Reviewed & tested by the community

Random fail in Drupal\views_ui\Tests\OverrideDisplaysTest, back to RTBC

berdir’s picture

Status: Reviewed & tested by the community » Needs work

Thanks for working on this.

I think we should officially deprecate uri_callback and this documentation should clarify that a) it is deprecated and b) it only works when *not* using link templates. I even wondered if we should go as far as removing any documentation about it. The only reason to not remove the code right now is because that would be an API change and we have in fact one working implementation of it in core. So we could even argue that we shouldn't document a feature that you're not supposed to use.

As mentioned in other issues, the definition on the term entity is bogus, it's not used because it has link templates. The only case where it is in fact used is aggregator items. And there it's IMHO bad usage because it's really the URL to the external source, *not* the entity in Drupal. We should have just added a getSourceUrl() or so method. Similar for the file entity, which does an even weirder thing by overwriting the url() method (which nobody thought to update when toUrl() was added).

jhodgdon’s picture

Title: Update documentation about uri_callback in routes for entities » Deprecate uri_callback in routes for entities
Component: documentation » entity system

Deprecating it seems like a good idea. In which case this is not just a Documentation issue any more.

longwave’s picture

Status: Needs work » Needs review
StatusFileSize
new5.33 KB
new3.13 KB

Added deprecation warnings to the interface and member variable, and reworded the existing uses to make the warning even stronger.

berdir’s picture

I doubt this will get into 8.0.x, so we should probably say deprecated in 8.1.x

jhodgdon’s picture

Version: 8.0.x-dev » 8.2.x-dev

Or more likely, 8.2.

jhodgdon’s picture

Status: Needs review » Needs work
  1. +++ b/core/lib/Drupal/Core/Entity/EntityType.php
    @@ -189,6 +189,9 @@ class EntityType implements EntityTypeInterface {
    +   * @deprecated in Drupal 8.0.x-dev and will be removed before Drupal 9.0.0.
    

    Also I think most of our deprecation messages say something like:

    @deprecated in 8.1.0...

    not

    ... in Drupal 8.0.x-dev ...

  2. +++ b/core/lib/Drupal/Core/Entity/EntityType.php
    @@ -189,6 +189,9 @@ class EntityType implements EntityTypeInterface {
    +   * @deprecated in Drupal 8.0.x-dev and will be removed before Drupal 9.0.0.
    +   *   Use Drupal\Core\Entity\EntityInterface::toUrl() for URL generation.
        */
       protected $uri_callback = NULL;
    

    Really though... I don't think that saying to use toUrl() for URL generation is all that relevant here. The $uri_callback() was more of a substitute for links annotation, from a developer perspective (the developer who is creating the entity).

    So ... how about also saying to define the URLs for entities using the links annotation or a route handler, and maybe linking to the entity API topic for more information?

  3. +++ b/core/lib/Drupal/Core/Entity/EntityType.php
    @@ -189,6 +189,9 @@ class EntityType implements EntityTypeInterface {
    +   *   Use Drupal\Core\Entity\EntityInterface::toUrl() for URL generation.
    

    Whenever you have a class with namespace in docs, start it with \

longwave’s picture

Status: Needs work » Needs review
StatusFileSize
new5.45 KB
new2.01 KB

I cheated by basically copying the deprecation notice from label_callback, hopefully improved in this version.

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

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.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.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.

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.

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.

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.

longwave’s picture

Version: 9.5.x-dev » 10.1.x-dev
Category: Bug report » Task
Status: Needs review » Postponed
Issue tags: +Bug Smash Initiative

This came up as a random triage bug in #bugsmash.

I think is more of a task than a bug if it is just something we want to deprecate, however we can only deprecate critical things in 9.5.x for removal in 10.0.x, so postponing until 10.1.x now.

quietone’s picture

Status: Postponed » Active

No longer postponed

catch’s picture

Status: Active » Needs work
Related issues: +#2252411: Entity uri callback does not receive relationship argument

I think we need to detect when the key is set when collecting entity info and trigger a deprecation then. If we did that we'd get errors for comment and forum. #2010202: Deprecate comment_uri().

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.

longwave’s picture

Status: Needs work » Needs review

Moved the patch to an MR, added deprecation notices on getUriCallback() and setUriCallback(), maybe we need more?

longwave’s picture

Deprecated some more.

longwave’s picture

Issue tags: +Needs change record

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

godotislate’s picture

Coming here from #3301682-46: Define bundle classes via attributes, where I discovered that in the conversion from annotations to attributes, the uri_callback parameter was typed as string|null , which made it impossible to specify class methods as the uri_callback, and that since that issue has never come up, pretty reasonable to conclude that it's almost never used.

Anyway, MR looks good other needing a CR URL in all the deprecation messages. Good for RTBC once the CR is created and URLs are updated.

godotislate’s picture

Status: Needs review » Needs work

Actually, caught one documentation reference in entity.api.php for hook_entity_bundle_info() that probably should have a (deprecated) note on there somewhere. So back to NW for that and the CR.

longwave’s picture

Status: Needs work » Needs review
Issue tags: -Needs change record

Added a CR, updated all links. Also decided just to remove that reference in the API docs as nobody should be relying on uri_callback being returned any more anyway.

godotislate’s picture

Didn't notice before, but the MR target branch is 11.x, not main, so there are 1000 file changes showing.

godotislate’s picture

Status: Needs review » Reviewed & tested by the community

MR and CR lgtm.

  • catch committed 14408034 on 11.x
    task: #2667040 Deprecate uri_callback in routes for entities
    
    By:...

  • catch committed 8ca8a904 on main
    task: #2667040 Deprecate uri_callback in routes for entities
    
    By:...
catch’s picture

Version: main » 11.x-dev
Status: Reviewed & tested by the community » Fixed

Committed/pushed to main and 11.x, thanks!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.