2

Is it possible to modify a page title after a controller sets #title in the main element?

Normally you can modify the page title by setting _title or _title_callback in the route. However it seems that you cannot alter the page title if a controller sets #title.

(A correct answer would affect both the HTML <title> and the system title block.)

  • Specifically, my problem is Page Manager sets a title, and it seems I cannot modify page title with custom code. – Daniel Jun 17 '17 at 7:55
2

Yes, #title from the controller overrides the title set in the route. There doesn't seem to be another override to override #title.

Though you could try to alter the controller output in a KernelEvents::VIEW with a priority higher than 0 like this:

$result = $event->getControllerResult();
// change the controller output
$event->setControllerResult($result);

Metatag

Working on a project I found out, that the metatag module in default configuration gets the title with the token [current-page:title], which retrieves the title from the route, ignoring #title set in the controller. Most times this would be considered as an unwanted side effect, but in your case you get the title you want from the route title callback in HTML <title> and only have to take care of the page title block, which should be no problem in a preprocess hook.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.