Entities should not assume what route parameters to provide

Created on 1 June 2015, about 10 years ago
Updated 14 May 2025, 18 days ago

Problem/Motivation

When generating an entity link, the entity will assume that the route takes a parameter named after the entity type by default. This may not always be true.

<!--break-->

Example use case:

Assume that a module wants to provide a path such as: '/example/{custom}' where the placeholder is upcast to a different entity type based on site settings. This requires a custom EntityRouteProviderInterface implementation because the placeholder name must match the callback argument name, and a type hint in the route options. So far, so good.

Unfortunately, when creating a link to the entity, Entity::link() generates URL info under the assumption that non-collection links always contain a placeholder named after the entity type. This results in a MissingMandatoryParametersException in UrlGenerator::doGenerate().

Proposed resolution

There are several possibilities.

  1. Do nothing. The current workaround is to make developers override Entity::urlRouteParameters() in all subclasses.

    Pros: It's easy.
    Cons: Does nothing to solve the underlying issue, which is described in the third option.

  2. Make the entity class look up the route information. If there is no route parameter named after the entity type, check if a type hint is provided for the other parameters and if one matches the entity type, use that parameter name.

    Pros: This is a relatively simple change that improves the DX for module maintainers while not breaking the current implementation.

  3. Do what should have been done a long time ago. Decouple and remove link/route handling code from the entity class. Right now, entities are implementing and providing routing data, despite not knowing anything about the routing system. Its primary concern should be content storage and extensibility (fields). The addition of routing and display code is resulting in additional complexity generated from things like link relationships and confusion/irregularities from having the entity system caught between things like:
    #2133469: Replace path-based entity links with route names
    #2150747: Switch back to URI templates in entity annotations
    Pros: We removed routing from D7's menu system, now lets move routing out of D8's entity system...
    Cons: This would probably involve non-trivial changes to the existing code base. It is the nuclear option.

Remaining tasks

Figure out how to proceed.

User interface changes

None.

API changes

Dependent on solution. Likely none for first and second options, but major for the last one.

Feature request
Status

Postponed: needs info

Version

11.0 🔥

Component

entity system

Created by

🇺🇸United States MattA

Live updates comments and jobs are added and updated live.
  • stale-issue-cleanup

    To track issues in the developing policy for closing stale issues, [Policy, no patch] closing older issues

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇺🇸United States smustgrave

    Thank you for sharing your idea for improving Drupal.

    We are working to decide if this proposal meets the Criteria for evaluating proposed changes. There hasn't been any discussion here for over 8 years which suggests that this has either been implemented or there is no community support. Your thoughts on this will allow a decision to be made.

    Since we need more information to move forward with this issue, the status is now Postponed (maintainer needs more info). If we don't receive additional information to help with the issue, it may be closed after three months.

    Thanks!

Production build 0.71.5 2024