Score:0

How can I make route optional parameter "dynamic"?

br flag

I have a route which need to have the language code as optional parameter:

my_module.my_route:
  path: '/custom-link/{langcode}'
 defaults: 
    _controller: '\Drupal\my_module\Controller\MyController:render'

I know from Optional parameters how to make a static optional parameter.

However, I need to determine the parameter dynamically: if it is not provided in the url, it should be the site's default language.

How can I implement that?

4uk4 avatar
cn flag
You could use a static default value `not-defined` and when your controller receives this value it replaces it dynamically.
Jaypan avatar
de flag
You should make that an answer.
4uk4 avatar
cn flag
It's a bit too simple, just an alternative if there is no dynamic default route parameter within the routing system, a callback for example.
Giuseppe avatar
br flag
@4uk4: thanks for the suggestion. Alas, I don't understand "just an alternative if there is no dynamic default route parameter within the routing system, a callback for example". Is there that possibility, or is a comment about the lack of that feature in Drupal?
4uk4 avatar
cn flag
I don't know if there is that possibility. If not it could be a valid feature request in cases where the controller code is not custom.
I sit in a Tesla and translated this thread with Ai:

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.