Filter by bundle in EntityConverter route param converter

Created on 29 June 2020, over 4 years ago
Updated 11 April 2024, 9 months ago

Problem/Motivation

I want to be able to tell the entity param converter (EntityConverter) which bundles to be converted on route where a param is an entity ID. For instance, I want to be able to create such a route definition:

some_route:
  path: /node/{node}/foo
  defaults:
    _controller: ...
  ...
  options:
    parameters:
      node:
        type: entiity:node
        bundle:
          - blog
          - article   

With this definition I would expect that passing a node ID of a node of type 'blog' or 'article' to get the {node} param converted into a node entity but when I pass a node ID of a 'page' node to get a 404, as this route business purpose is to deal only with those node types and the route should "not exist" for other kind of nodes.

Proposed resolution

I've implemented this in our project by swapping the class of the paramconverter.entity service but I see here some value that can be added to Drupal core.

Remaining tasks

None.

User interface changes

None.

API changes

Route parameters of type entity:* accept a new bundle definition property of type array.

Data model changes

None.

Release notes snippet

In order to limit the route entity parameters to certain bundles, a 'bundle' property, of type sequence, may be added to the parameter's definition.

✨ Feature request
Status

Fixed

Version

9.2

Component
RoutingΒ  β†’

Last updated 3 days ago

Created by

πŸ‡·πŸ‡΄Romania claudiu.cristea Arad πŸ‡·πŸ‡΄

Live updates comments and jobs are added and updated live.
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.

  • πŸ‡©πŸ‡ͺGermany donquixote

    The solution implemented here does not work if we have multiple routes with same path, but for different bundles.

    The ParamConverter is only called _after_ the route was already picked in Router::matchCollection().
    So it is too late at that point.

  • πŸ‡©πŸ‡ͺGermany donquixote

    I found that even the old mechanism would not have worked.
    So nothing was made worse in this issue.
    Sorry for the noise.

    The Router::matchCollection() does not call the access checks.
    It only calls $route->getCondition() which seems to provide some additional regex checks, but this does not really help to distinguish bundles.

    So this is a dead end: We should not have multiple routes with the same path.

Production build 0.71.5 2024