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
().