Score:1

Is it possible to send an argument to a title callback?

us flag

I am trying to figure out how to set page titles on various pages. I have seen a lot of posts suggesting preprocess functions. These are not correctly setting the route's page title but only setting the value for the html template.

Another solution is closer in that it suggests using an EventSubscriber triggering off the KernelEvents::REQUEST event. This is the correct approach. Some routes have a default title and can easily be set using:

$route->setDefault('_title', $title);

while others use a title callback. This callback can be overridden using:

$route->setDefault('_title_callback', 'set_page_title_callback');

but I can't figure out how to pass an argument ($title) to this callback.

Jaypan avatar
de flag
The arguments passed to the title callback will be the route parameters, if any exist. They are automatically passed to the title callback.
4uk4 avatar
cn flag
You wouldn't use an EventSubscriber, though. It's a RouteSubscriber. Where is $title coming from?
liquidcms avatar
us flag
Hmm, RouteSubscriber, haven't heard of that. Will take a look. Thanks. My guess is it will have the same issue though, that I can alter _title_callback but can't pass parameters to the callback. The title comes from code which determines the title vs path (i.e. a value set in a config variable). At the moment I have this working with a &drupal_static var.
in flag
What are you trying to pass into the callback? Curious if this information doesn't need to be passed in, but rather retrieved by the callback itself (e.g. context values).
Jaypan avatar
de flag
The short answer is that you would add the parameter you need to the path as a route parameter, so that it can be retrieved in the title callback and used to build the title.
apaderno avatar
us flag
The question is then: Can the code that determines the title be placed in a controller method? A controller can access configuration objects; that is not an issue.
liquidcms avatar
us flag
Jaypan - this isnt a custom route so not sure controller additions help here.
liquidcms avatar
us flag
Joseph - hmm, possibly, all I am trying to pass is a string (i.e. what the page title should be); but this comes from values in the route/request (and other code which looks at config settings) so likely just available in the custom callback - code is a bit messier then; but doable. Ideal way would be to do what i was hoping.. pass a variable to callback.
liquidcms avatar
us flag
Apaderno, and i think maybe Jaypan - not sure i get the controller direction here. A controller to me feels like something that requires a route. Where would that come in here?
apaderno avatar
us flag
@liquidcms You have a route: the one for which you want to change the title callback.
liquidcms avatar
us flag
Well, a dozen or so, but yes. so a bunch of controller overrides (is that a thing?) for each of them? Seems like a lot of work when the 10 lines or so now that i have covers all of them. But maybe i am missing the direction here.
liquidcms avatar
us flag
Hmm, perhaps my issue is simply that some of these routes use a title callback? I never even tried just unsetting that and setting _title.
Score:1
cn flag

Controller and title methods are often in the same controller class, but they don't have to. They can be in different classes, which also can be a service class, which is preferable, if you have a lot of dependencies and the class is shared by multiple routes.

In a RouteSubscriber:

$route->setDefault('_title_callback', 'my_service:my_title_callback');

See Override specific routing from a core module

When you set a title callback it always has priority, so this works in both cases, no matter if the title was set before statically or dynamically.

The title callback receives the same arguments as the main controller. The title callback has then to calculate and return the dynamic title from these arguments and the context, like the request, which you can pull from the callback's arguments as well. See How do I pass parameters from a form redirect to a controller?

liquidcms avatar
us flag
Thanks, i'll give this a shot. Will need to rearrange some code and see if the request is available to me (as i look up page title based on path or path queries); but sounds like this would work.
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.