Score:0

How can I get a route's name if I have the route object?

in flag

I have a route object (which may not be the current route's object) and I need to get the route's name, e.g. entity.node.canonical. I don't see any method to get the name directly from the object, nor do I see it anywhere when I inspect the object via Xdebug.

I know that I can easily get the name of the current route with $current_route_name = \Drupal::routeMatch()->getRouteName(), but how can I get the name of an arbitrary route, if I have the route object.

4uk4 avatar
cn flag
You probably mean by not the current route object the route is outbound. You can't get the route name from the route object. This is why they added the route name to [RouteProcessorManager::processOutbound](https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21RouteProcessor%21RouteProcessorManager.php/function/RouteProcessorManager%3A%3AprocessOutbound/9.2.x). You can, however, use a route processor to store the route name in the route object to retrieve it later.
sonfd avatar
in flag
Thank you. I am trying to use `hook_domain_source_alter()` which is called from an outbound processor in the domain_source module. Maybe it's best that I create a patch to add route_name to the data passed to the alter hook.
sonfd avatar
in flag
Alternatively, I did find another post https://drupal.stackexchange.com/questions/272190/get-route-name-from-path-with-wildcard that might work.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.