Route providers should fetch route paths from entity definitions

Created on 4 August 2025, about 1 month ago

Problem/Motivation

Currently, route providers for some core entity types hardcode route paths, which makes them difficult to alter since you need to change the paths in multiple places.

Example:

class FileRouteProvider implements EntityRouteProviderInterface {

  /**
   * {@inheritdoc}
   */
  public function getRoutes(EntityTypeInterface $entity_type) {
    $route_collection = new RouteCollection();

    $route = (new Route('/file/{file}/delete'))
      ->addDefaults([
        '_entity_form' => 'file.delete',
        '_title' => 'Delete',
      ])
      ->setRequirement('file', '\d+')
      ->setRequirement('_entity_access', 'file.delete')
      ->setOption('_admin_route', TRUE);
    $route_collection->add('entity.file.delete_form', $route);

    return $route_collection;
  }

}

Proposed resolution

Use entity definitions as the single source of truth. For the above example, it should look like this:

class FileRouteProvider implements EntityRouteProviderInterface {

  /**
   * {@inheritdoc}
   */
  public function getRoutes(EntityTypeInterface $entity_type) {
    $route_collection = new RouteCollection();

    $path = $entity_type->getLinkTemplate('delete-form');
    if ($path) {
      $route = (new Route($path))
        ->addDefaults([
          '_entity_form' => 'file.delete',
          '_title' => 'Delete',
        ])
        ->setRequirement('file', '\d+')
        ->setRequirement('_entity_access', 'file.delete')
        ->setOption('_admin_route', TRUE);
      $route_collection->add('entity.file.delete_form', $route);
    }

    return $route_collection;
  }

}

This approach will allow developers to easily alter the link template route path using just hook_entity_type_alter().

📌 Task
Status

Active

Version

11.2 🔥

Component

routing system

Created by

🇷🇺Russia Chi

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024