Active
Project:
Drupal core
Version:
main
Component:
routing system
Priority:
Normal
Category:
Feature request
Assigned:
Unassigned
Reporter:
Created:
5 Nov 2026 at 16:37 UTC
Updated:
6 Nov 2026 at 14:55 UTC
Jump to comment: Most recent
A comment permalink URL is like this:
https://example.com/comment/CID#comment-CID
The page at this URL contains the node and the comments. Because it contains the node, Drupal sees this as an internal redirect to the node route. So, functions like \Drupal::routeMatch() and \Drupal::request()->getRequestUri() return information about the node. The only way I have found to get the comment ID is to directly access $_SERVER['REQUEST_URI']. There should be an easier way.
Create a service that returns information about the actual request made by the browser without considering internal redirects.
Decide what to do. Implement.
None.
None.
New service.
None.
Comments
Comment #2
shank115 commentedI had a quick look at this on 11.x with a comment permalink (/comment/1). I might be wrong here, but sharing in case it helps.
It looks like the original request is still available on the request stack. Both of these returned the comment in my test:
\Drupal::requestStack()->getMainRequest()\Drupal::service('current_route_match')->getMasterRouteMatch()->getParameter('comment')One thing I noticed, though: when I used that value during rendering (in a node view hook), it got cached and reused across pages. The value rendered for /comment/1 showed again on /comment/2. Adding the route or url cache context didn't seem to help, and /comment/2 still got the value from /comment/1. My guess is that these contexts read the node sub-request rather than the main request, but I am not fully sure. Setting max-age: 0 on the element avoided it, though that's probably not ideal.
So maybe only a service wouldn't be enough, and something like a cache context based on the main request could also be needed? Just a thought. There may well be a better existing approach I am not aware of.